2

Hi i'm trying to load database from Mysql serveer and keep getting this error now mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\Users\Mirne\Desktop\Server\root\funkcie.php on line 15 . I have checked quite few posts but i don't see to find my mistake . Ty for help

<?php
global $X,$Y,$height,$weight,$key,$type,$text,$position;

function load() {
$servername = "localhost";
$username = "root";
$password = "xxxx";
$dbname = "web builder";
$link = mysqli_connect($servername, $username, $password, $dbname); 
if (!$link) {
die("Connection failed: " . mysqli_connect_error());
  }
    $sql = "SELECT x, y, height, weight, key , type , text , position FROM suradnice";
$result = mysqli_query($link, $sql);
if (mysqli_num_rows($result) > 0) { ////////line15///////
        while($row = mysqli_fetch_assoc($result)) {             
        $X=$row["x"] ;
    $Y=$row["y"] ;
    $height=$row["height"] ;
    $weight=$row["weight"];
    $key=$row["key"];
    $type=$row["type"] ;
    $text=$row["text"] ;
    $position=$row["position"]  ;
        }  
    } else {
        echo "error";
    }   
    mysqli_close($link);
}

?>
potashin
  • 44,205
  • 11
  • 83
  • 107
Mirne
  • 75
  • 7

2 Answers2

9

Your query should not work and its failing. You are using a reserved word key and this needs to be wrapped in backticks ` in the query:

SELECT x, y, height, weight, `key`, type

List of MySQL reserved words

potashin
  • 44,205
  • 11
  • 83
  • 107
Abhik Chakraborty
  • 44,654
  • 6
  • 52
  • 63
  • 1
    Alternatively use the table name with the column; e.g. `SELECT suradnice.x, suradnice.y, ... suradnice.key, suradnice.type ...` – CD001 Apr 16 '15 at 12:32
3

While Abhik is absolutely correct, I would also suggest you add Error catching to your SQL with something like this:

$result = mysqli_query($link, $sql) or die("line ".__LINE__.":".mysqli_error($link));

Which will give you a clear output to help you see what went wrong and why.

(not for production environment, or for production instead of a die statement write the error to the PHP error log file.)

Martin
  • 22,212
  • 11
  • 70
  • 132