-2

In my PHP code I try to get data from my database but I get

"Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given"

as an error.

I found on stackoverflow that this indicates that my SQL query is getting back false. if I copy my sql query into the phpmyadmin sql field everything works just fine.

I tried various different notations (giving 1 and true for the boolean "fährt")

$sql = "SELECT * FROM `$params` WHERE `fährt` = 1" ;
$result=mysqli_query($conn,$sql);
if(mysqli_num_rows($result) != 0){
    while($row = mysqli_fetch_array($result)){
        echo "<br>".$row[0]."-".$row[1]."-".$row[2]."-".$row[3]."-".$row[4]."-".$row[5];
    }
}else{
    echo "Fehler";
}

Expected: One entry from my database

Actual output: Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given

UPDATE

So I tried what was said in the comments but it for me it does not explain why it works when I copy it to phpmyadmin. Furthermore it does work without the where clause.

tiko_2302
  • 101
  • 8
  • 2
    Your query is wrong; make sure you have defined `$params` – Abderrahim Soubai-Elidrisi Dec 20 '18 at 21:47
  • $params is defined, if I try it without the WHERE clause it works – tiko_2302 Dec 20 '18 at 21:48
  • just follow troubleshooting steps in linked question, like check mysqli_error – Iłya Bursov Dec 20 '18 at 21:49
  • ok I'll try thank you – tiko_2302 Dec 20 '18 at 21:49
  • Note: The object-oriented interface to `mysqli` is significantly less verbose, making code easier to read and audit, and is not easily confused with the obsolete `mysql_query` interface. Before you get too invested in the procedural style it’s worth switching over. Example: `$db = new mysqli(…)` and `$db->prepare("…")` The procedural interface is an artifact from the PHP 4 era when `mysqli` API was introduced and should not be used in new code. – tadman Dec 20 '18 at 21:58
  • A lot of problems can be detected and resolved by [enabling exceptions in `mysqli`](https://stackoverflow.com/questions/14578243/turning-query-errors-to-exceptions-in-mysqli) so mistakes aren't easily ignored. – tadman Dec 20 '18 at 21:59
  • but why does the connection fail with the WHERE clause but not without it? – tiko_2302 Dec 20 '18 at 22:00
  • Sorry for me being that dumb... – tiko_2302 Dec 20 '18 at 22:00
  • Whats about utf_8? fährt needs utf8 and it can be that it is automatically detected by phpmyadmin but the connection needs the parameter utf8. See here: http://php.net/manual/de/mysqli.set-charset.php – DS87 Dec 20 '18 at 22:00
  • The utf_8 thing solved it, thank you @DS87 – tiko_2302 Dec 20 '18 at 22:02

1 Answers1

0

You need to use utf_8 for the MySQL Connection. See php.net/manual/de/mysqli.set-charset.php

DS87
  • 536
  • 3
  • 9
  • 29