user1532468
user1532468

Reputation: 1753

No results returned from query

I am trying to count how many records in a mysql db using a query with a where condition. Unfortunately, the code I am using is not producing any results.

If i enter the query directly into myphpadmin, it shows there are 78 records. I am confused as to why it is not working. I would be grateful if someone could point out my error. Many thanks

<?php
$result1 = mysqli_query("SELECT count(*) FROM act WHERE activity = 'New Intake' AND new = '1'");
$rows = mysqli_fetch_row($result1);
$num = $rows;
?>

Upvotes: 1

Views: 219

Answers (1)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72289

1.connection link is missing in query.(first parameter).

2.Use alias inside query.

3.Assign counted value to varibale.

4.echo the variable to see the result.

Do like below:-

<?php
 // provide db connection object as first parameter and use alias for count(*)
 $result1 = mysqli_query($connection,"SELECT count(*) as total FROM act WHERE activity = 'New Intake' AND new = '1'"); 
 $rows = mysqli_fetch_assoc($result1);
 $num = $rows['total']; // assign counted value to varibale

 echo $num; // echo the variable to see the result.
?>

Note:- Your code is wide-open for SQL INJECTION.You have to use prepared statements

Help reference:-

mysqli::prepare

PDO::prepare

Upvotes: 2

Related Questions