I am still new to PHP and I am trying to retrieve data from MySQL using PHP. I have a list of id stored in a csv file (first column of the csv). So I am trying to retrieve a data based on that id.
//MySQL Connection
$con=mysqli_connect($hostname,$username,$password,$dbName);
//CSV File
$file_handle = fopen($fileName, "r");
$sql = "SELECT count(*) AS total FROM user WHERE user_id = ";
$i = 0;
while ($i < 50) {
//Retrieving user_id from csv file
$file_line = fgetcsv($file_handle, 1024);
$query = $sql . $file_line[0];
//Retrieving data from mySQL
$result = mysqli_query($con,$query);
//Tested with single row but still giving me an error
//$row = $result->fetch_assoc();
$row = mysqli_fetch_array($result);
echo $row;
$i++;
}
//close file
fclose($file_handle);
//close the connection
mysqli_close($con);
?>
The error message I received:
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\wamp\www\.. on line 59
So, I am not sure what I did wrong. Any help would be great.
UPDATE:
Thanks to the help of everyone here I was able to figure out what was wrong. Here's an updated code of what I did.
$i = 0;
while ($i < 50) {
//Retrieving user_id from csv file
$file_line = fgetcsv($file_handle, 1024);
$user_id = $file_line[0];
$sql = "SELECT count(*) AS total FROM user WHERE user_id = $user_id ";
//Retrieving data from mySQL
$result = mysqli_query($con,$sql);
//Tested with single row but still giving me an error
$row = mysqli_fetch_array($result);
echo $row[0]; //Since this is an array I forgot to retrieve the first index of the array.
$i++;
}