Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to MySQL database error [duplicate]

Tags:

sql

php

mysql

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);
}

?>
like image 509
Mirne Avatar asked Mar 08 '26 14:03

Mirne


1 Answers

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

like image 88
Abhik Chakraborty Avatar answered Mar 10 '26 05:03

Abhik Chakraborty