V5Nathan February 2016

Display the amount of Paid items in my database using php

I am using php and mysql to create a page that displays all of the jobs we have in the database. The data is shown is a table and when a row is clicked a modal window triggers with the information of the clicked job inside. At the top of the page I want a simple counter that shows amount of paid jobs, invoiced jobs etc etc. I am using the code below but having no luck...

<?php
$con = mysql_connect("localhost","databaseusername","password");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("databasename", $con);

$result = mysql_query("select count(1) FROM jobslist");
$row = mysql_fetch_array($result);

$total = $row[0];


mysql_close($con);
?>

This code as far as I am aware is counting the amount of INT columns set to 1 rather than 0. No matter what I try I can't seem to get it to count the amount of 'paid' items in the database or 'invoiced' etc etc.

Once the count function is complete currently I am echoing out the outcome as below:

<?php echo "" . $total;?>

I am sure I am overlooking something simple, but any help is appreciated.

EDIT: TABLE STRUCTURE INCLUDED

http://i.stack.imgur.com/hcMJV.png

Answers


Tom Wright February 2016

When I do this I usually name the COUNT result. Try this out:

$result = mysql_query("SELECT COUNT(*) AS total_rows FROM jobslist;");
$row = mysql_fetch_array($result);

$total = $row['total_rows'];

If you do not want to name the COUNT result, then give the following a go:

$result = mysql_query("SELECT COUNT(*) FROM jobslist;");
$row = mysql_fetch_array($result);

$total = $row['COUNT(*)'];


Jay Blanchard February 2016

select count(1) FROM jobslist

This code as far as I am aware is counting the amount of INT columns set to 1 rather than 0.

No, this is just counting rows in your table and not filtering. If you want to count something with a specific filter you have to add that filter condition:

SELECT COUNT(*) AS `MyCount`
FROM `joblist`
WHERE `MyColumn` = 1; -- assuming MyColumn contains the INT you're looking for

You should stop using mysql_* functions. These extensions have been removed in PHP 7. Learn about prepared statements for PDO and MySQLi and consider using PDO, it's really pretty easy.


RamRaider February 2016

Assuming a column called paid you could restructure the query similar to the following. If you needed to sum the amounts involved that requires additional tweaking.

$result = mysql_query("select 
    ( select count(*) from `jobslist` where `paid`=1 ) as 'paid',
    ( select count(*) from `jobslist` where `paid`=0 ) as 'unpaid'
    from jobslist");

$rows   = mysql_num_rows( $result );

while( $rs=mysql_fetch_object( $result ) ){
    $paid=$rs->paid;
    $unpaid=$rs->unpaid;

    echo 'Total: '.$rows.'Paid: '. $paid.' Unpaid: '.$unpaid;
}


cFreed February 2016

First you should change deprecated mysql_... to mysqli_... (look here how to). But it's not the reason you fail.

Unlike you seem to suppose, COUNT(1) will not look for an INT column having value 1. Instead you must use COUNT(*) or COUNT(a_column_name) (same result), with adding a WHERE clause stating which condition is involved.

Here you seem wanting to count records where a given column (say the_column) has value 1. So you should:

SELECT COUNT(*)
FROM jobslist
WHERE the_column = 1

Last point: you don't need echo "" . in <?php echo "" . $total;?>.
Merely write <?php echo $total;?>.

Post Status

Asked in February 2016
Viewed 3,789 times
Voted 4
Answered 4 times

Search




Leave an answer