MySQL count error - php

I am trying to count all rows within the column field_161. However, it just returns the value of 0. The connection to the database is successful and the table and row are spelled correctly.
Here's my code:
$conn->query("SELECT COUNT(*) FROM app_entity_21 WHERE field_161 = 30 as $mytotaltasks");
echo "test" . $mytotaltasks;

You can't just do AS $mytotaltasks in mysql, and turn it into a PHP variable. You'll need to get the result from the query. The simplest way is to use fetchColumn():
$query = $conn->query("SELECT COUNT(*) FROM app_entity_21 WHERE field_161 = 30");
$mytotaltasks = $query->fetchColumn();

I think you should add the Group By at then end of query string
$conn->query("SELECT COUNT(*) FROM app_entity_21 WHERE field_161 = 30 group by field_161");
echo "test" . $mytotaltasks;

Try this!
$result=$conn->query("SELECT COUNT(field_161)AS field_cont FROM app_entity_21 WHERE field_161 = 30 as $mytotaltasks");
$data_cont=mysqli_fetch_assoc($result);
echo "This is the number of rows". $data_cont['field_cont'];
or a little bit different:
$result=$conn->query("SELECT COUNT(field_161)AS field_cont FROM app_entity_21 WHERE field_161 = 30 as $mytotaltasks");
$data_cont=mysqli_fetch_object($result);
echo "This is the number of rows". $data_cont->field_cont;

Related

How to execute function only if MySQL table is empty? [duplicate]

I'm trying to count the number of rows in a table and thought that this was the correct way to do that:
$result = $db->query("SELECT COUNT(*) FROM `table`;");
$count = $result->num_rows;
But counts always returns (int)1. If I use the same query in phpMyAdmin I get the right result. It sits in a table so I tried testing $count[0] as well, but that returns NULL.
What is the right way to do this?
You have to fetch that one record, it will contain the result of Count()
$result = $db->query("SELECT COUNT(*) FROM `table`");
$row = $result->fetch_row();
echo '#: ', $row[0];
Always try to do an associative fetch, that way you can easy get what you want in multiple case result
Here's an example
$result = $mysqli->query("SELECT COUNT(*) AS cityCount FROM myCity")
$row = $result->fetch_assoc();
echo $row['cityCount']." rows in table myCity.";
I find this way more readable:
$result = $mysqli->query('select count(*) as `c` from `table`');
$count = $result->fetch_object()->c;
echo "there are {$count} rows in the table";
Not that I have anything against arrays...
$result->num_rows; only returns the number of row(s) affected by a query. When you are performing a count(*) on a table it only returns one row so you can not have an other result than 1.

Field from database not displaying in browser

Can anyone tell me why I am not getting the result of displaying the title on the browser when using the following script:
$sql =mysql_query( "SELECT * FROM 'Tour' WHERE 'Tour_No.'=1 LIMIT 0, 30 ");
echo $sql Title;
my connection is successful, but my desired result is not happening.
$sql = mysql_query( "SELECT * FROM `Tour` WHERE `Tour_No.`=1 LIMIT 0, 30 ");
while($row = mysql_fetch_object($sql))
{
echo $row->Title;
echo '<br />';
}
Maybe you can check this link for more example using mysql_query and mysql_fetch_object :
mysql_query: http://php.net/manual/en/function.mysql-query.php
mysql_fetch_object: http://www.php.net/manual/en/function.mysql-fetch-object.php
Your Query is invalid (single quotes are not used for tables / columns):
$result = mysql_query("SELECT Title FROM Tour WHERE Tour_No = 1 LIMIT 1");
You have to fetch the results:
$row = mysql_fetch_assoc($result);
Output the title:
echo $row['Title'];
Table and field names can be put in backtics, not in single quotes.
SELECT * FROM `Tour` WHERE `Tour_No.`=1 LIMIT 0, 30 // correct
SELECT * FROM 'Tour' WHERE 'Tour_No.'=1 LIMIT 0, 30 // wrong
if your getting 30 results you will need to loop through $sql, try the following.
$sql = mysql_query("SELECT * FROM `Tour` WHERE `Tour_No.` = 1 LIMIT 0, 30 ");
while($row = mysql_fetch_array($sql))
{
echo $row['Title'];
}
From http://php.net/mysql_query:
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error....
The returned result resource should be passed to mysql_fetch_array(), and other functions for dealing with result tables, to access the returned data."
Try this:
$sql =mysql_query( "SELECT * FROM 'Tour' WHERE 'Tour_No.'=1 LIMIT 0, 30 ");
$row = mysql_fetch_assoc($sql);
echo $row['Title'];
I'm not sure exactly what your columns are named, but that should get you on the right track.

mysql query written using php

$u_id=$event_assoc['Uniqueid'];
echo $u_id."\n";
$result1 = mysql_query("SELECT * FROM eventdetail WHERE unique_id = '$u_id'", $con1);
while($row = mysql_fetch_array($result1))
{
echo 'in eventdetail'."\n";
$e_id= $row['event_id'];
$destination= $row['destination'];
$uniqueid= $row['unique_id'];
$call_num= $row['channelid'];
}
echo mysql_num_rows($result1);
echo $e_id."\n";
echo $destination."\n";
echo $call_num."\n";
echo $uniqueid."\n";
if(mysql_num_rows($result1)>0)
{
echo 'calculate'."\n";
$result= mysql_query("SELECT sum(billsec)
FROM cdr
WHERE uniqueid = '$uniqueid'", $con2);
$bil = mysql_fetch_array($result);
$bill= (float) $bil['sum(billsec)'];
echo $bill."\n";
this is my code..
whenever i try to execute sum function query it retruns top row's billsec instead of addition of all row's billsec
You are doing a where on an unique ID in the sum query. Remove the where and it'll work
You are using unique id in your query.. And in a table single row will have that id, thats why you are getting top row's billsec... Remove the where clause in second query, and then check.
$result= mysql_query("SELECT sum(billsec)
FROM cdr
WHERE uniqueid = '$uniqueid'", $con2);
i guess there is a bug in this area...look again
The PHP statement for the SQL SUM Query should be:-
$result= mysql_query("SELECT sum(`billsec`), `uniqueid` FROM `cdr` GROUP BY `uniqueid` HAVING `uniqueid` = '$uniqueid'", $con2);
You need to use the clause "GROUP BY" whenever you are using any MySQL Aggregate functions (if needed). Also if you are using any aggregate functions (like "SUM", "MAX" etc), then you cannot use "WHERE" clause on the field (in this case, the field is "uniqueid") which is being used in the "GROUP BY" clause.
Hope it helps.

my mysql query is resulting in 1 for number of rows regardless if value is there or not

here is my mysql statement:
$result = mysql_query("select count(*) from usersecurity where
email='".$_SESSION['username']."'");
echo mysql_num_rows($result);
Problem is, from my understanding, its always returning the value 1, even if the username doesn't exist in the table...why is that?
Because SELECT COUNT(*) ... with return a row with a single value containing zero.
COUNT(*) will always return a row, even if the result is zero. You need to examine the result of that column:
SELECT COUNT(*) AS count FROM t1 WHERE email = "SOME NON EXISTENT EMAIL";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
echo $row['count']; // 0
An alternative would be to select the count as the number of rows:
SELECT TRUE FROM t1 WHERE email = "SOME NON EXISTENT EMAIL";
$result = mysql_query($query);
echo mysql_num_rows($result); // 0
A COUNT(*) query always returns a single row. If you want the $ of rows then do one of the following.
$result = mysql_query("select count(*) as ROWS from usersecurity where
email='".$_SESSION['username']."'");
$rows = mysql_fetch_assoc($result);
echo $rows['ROWS'];
or
$result = mysql_query("select * as ROWS from usersecurity where
email='".$_SESSION['username']."'");
echo mysql_num_rows($result);
You will always get one row from the result query. That value will some value greater than or equal to zero. You need to inspect the actual count returned not the number of rows returned.
Your SELECT query is returning the following:
select count(*)...
----------------------
0
Thus, mysql_num_rows($result) is appropriately returning 1 as a result. If you're looking to get the value of your count, then you need to take an approach like the following:
$result = mysql_query("select count(*) from usersecurity where
email='".$_SESSION['username']."'");
$row = mysql_fetch_array($result);
echo $row[0];

MySQLi count(*) always returns 1

I'm trying to count the number of rows in a table and thought that this was the correct way to do that:
$result = $db->query("SELECT COUNT(*) FROM `table`;");
$count = $result->num_rows;
But counts always returns (int)1. If I use the same query in phpMyAdmin I get the right result. It sits in a table so I tried testing $count[0] as well, but that returns NULL.
What is the right way to do this?
You have to fetch that one record, it will contain the result of Count()
$result = $db->query("SELECT COUNT(*) FROM `table`");
$row = $result->fetch_row();
echo '#: ', $row[0];
Always try to do an associative fetch, that way you can easy get what you want in multiple case result
Here's an example
$result = $mysqli->query("SELECT COUNT(*) AS cityCount FROM myCity")
$row = $result->fetch_assoc();
echo $row['cityCount']." rows in table myCity.";
I find this way more readable:
$result = $mysqli->query('select count(*) as `c` from `table`');
$count = $result->fetch_object()->c;
echo "there are {$count} rows in the table";
Not that I have anything against arrays...
$result->num_rows; only returns the number of row(s) affected by a query. When you are performing a count(*) on a table it only returns one row so you can not have an other result than 1.

Categories