Reputation: 3247
I have a function. in that function in have a select query. another query should return the number of results that will be in the first query. I have this code:
function name ($f){
global $db;
...
$results = "SELECT `a` FROM `b` WHERE $where";
$results_num = ($query = mysqli_query($db, $results)) ? mysqli_num_rows($results) : 0;
echo $results;
echo $results_num;
}
will echo:
SELECT `a` FROM `b` WHERE `keywords` LIKE '%abc%'0
what is just $results
but not $results_num
? I do not understand why echo $results_num
wont be shown and why there is a 0
at the end of $results
so if there is someone who could give me advise to solve this I really would appreciate. thanks a lot.
Upvotes: 0
Views: 92
Reputation: 7157
Firstly, $results_num
is zero and is being output (that's why there's a 0
at the end of your output). That's because this code is wrong:
$results_num = ($query = mysqli_query($db, $results)) ? mysqli_num_rows($results) : 0;
Breaking it out a little:
$query = mysqli_query($db, $results);
$results_num = $query ? mysqli_num_rows($results) : 0;
And the second line should actually be:
$results_num = $query ? mysqli_num_rows($query) : 0;
You need to pass the query handle to mysqli_num_rows()
, not the SQL.
Upvotes: 1
Reputation: 12776
$results
is your query string. You can't pass a string to mysqli_num_rows()
like you're doing it, it expects a result set identifier returned by mysqli_query()
. In your case it's $query
(you might wanna change your variable names to make more sense, BTW).
Upvotes: 0
Reputation: 623
$results_num is 0. $results is - SELECT a
FROM b
WHERE keywords
LIKE '%abc%'
try this
echo "Results is $results <br />";
echo "Results_num is $results_num <br />";
and everything will reveal itself :)
Upvotes: 1