D
D
davc2018-06-08 11:38:12
MySQL
davc, 2018-06-08 11:38:12

How to fix mysqli_num_rows error?

Hello, I have this code,

$result = mysqli_query($link, "SELECT * FROM `id` ORDER BY `data` DESC WHERE id = '1'");
     $row_cnt = mysqli_num_rows($result);

and gives the following error Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in
When "delete where id ='1'" then everything becomes normal
Please tell me how to fix it?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
Nick Sdk, 2018-06-08
@davc

in the query, first there should be WHERE , and then ORDER / GROUP BY
you have the opposite

V
Vadim, 2018-06-08
@vadimek

You have an error in your request. From documentation:
Swap ORDER BY ... and WHERE ...
You can also add a check: after the first line, write if ($result) { ... }. Better yet, use PDO instead of mysqli, because the former can throw exceptions.

A
Alexander, 2018-06-08
@AleksandrB

$result = mysqli_query($link, "SELECT * FROM `id` ORDER BY `data` DESC WHERE id = '1'");
     $row_cnt = mysqli_num_rows($dbc, $result);

Where $dbc is a database connection (mysqli_connect())

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question