0

I wrote to code to fetch data from the mysql database,

<?php
$host="localhost";
$user="root";
$pass="";
$database="users";
$con=mysqli_connect($host,$user,$pass) or die ("connection failure");

$day="Mon";
$status="false";
$timeInterval="08-10";
mysqli_select_db($con,$database) or die("databaseconnection error");
$result=mysqli_query($con,"SELECT hallId FROM lectureHall WHERE status=$status AND day=$day AND timeInterval=$timeInterval");
while($row=mysqli_fetch_assoc($result)){
    $tmp[]=$row;

}


echo json_encode($tmp);
mysqli_close($con);


?>

this code gives me a warning, displaying:

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result null given

However, when I give values directly without variable passing like this

$result=mysqli_query($con,"SELECT hallId FROM lectureHall WHERE status='false' AND day='Mon' AND timeInterval='08-10'");

It gives results, why variable passing code doesn't work? is there is a special way to pass String values to a query. Variable passing is needed, as I am going to give values for these variables through Java HTTP post` please help me to fix this. Thanks in advance

Jaquarh
  • 6,493
  • 7
  • 34
  • 86
codsop
  • 9
  • 5

2 Answers2

1

You need to quote around the variables:

$result=mysqli_query($con,"SELECT hallId FROM lectureHall WHERE status='$status' AND day='$day' AND timeInterval='$timeInterval'");
while($row=mysqli_fetch_assoc($result)){
    $tmp[]=$row;

}

You should check for a mysql error as well. This would have shown you your error:

... your mysqli_query ...
if(!$result){
   die(mysqli_error($con));
}
... your while...
dmgig
  • 4,400
  • 5
  • 36
  • 47
0

Try this:

$result = mysqli_query($con,"SELECT hallId FROM lectureHall WHERE status= '".$status."' AND day='".$day."' AND timeInterval= '".$timeInterval."' ") or die(mysqli_error($con));

It's better to work around with single and double quotes.

You could also use this die(mysqli_error($con)) to ensure proper error handling in case something goes wrong with the syntax.

Hope this helps.

Peace! xD

Indrasis Datta
  • 8,692
  • 2
  • 14
  • 32