php - Why does mysqli_fetch_assoc generate an error -
this question has answer here:
this question has been asked, , i've followed answers i'm still getting error 'mysqli_fetch_assoc() expects parameter 1 mysqli_result'
my code
$host = "localhost"; $user = "my_user"; $user_pass = "my_password"; $dbase = "my_database"; $conn = mysqli_connect($host, $user, $user_pass, $dbase ); if (!$conn) { die("connection failed: " . mysqli_connect_error()); } $sql="select id, customer_name, date_ordered, amount orders status_id=5 , date_ordered >= '2016-01-01 00:00:00'"; $result = mysqli_query($conn, $sql); if($result === false) { die(mysqli_error($mysqli)); } var_dump($result); echo 'test = '.mysqli_num_rows($result).'<br />'; if(mysqli_num_rows($result) > 0){ while($row = mysqli_fetch_assoc($result)){ //rest of code goes here } }
the var_dump line returns:
object(mysqli_result)#2 (5) { ["current_field"]=> int(0) ["field_count"]=> int(4) ["lengths"]=> null ["num_rows"]=> int(329) ["type"]=> int(0) }
the while($row = mysqli_fetch_assoc($result)) line returns
warning: mysqli_fetch_assoc() expects parameter 1 mysqli_result
i don't know i'm doing wrong.
thanks
silly me!
i reset variable $result within while loop. made adjustments , code working
Comments
Post a Comment