简体   繁体   中英

Results not displaying from MySQL query with PHP

I have been racking my brain with this problem for 2-3 days so a fresh set of eyes will be appreciated.

I am querying my database to get specific set of results.

Code

<?php

$_POST['custid'];
$db=mysqli_connect("***","***","***","***");
// Check connection
if (mysqli_connect_errno($db))
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql="SELECT DISTINCT duedate FROM invoicequote WHERE customeraccountnumber='1000001'";
echo $sql;

$result=mysqli_query($db,$sql) or die(mysqli_error($db));
if ($result)
{
    // Return the number of rows in result set
    $rowcount=mysqli_num_rows($result);
    printf("Result set has %d rows.\n",$rowcount);
    // Free result set
    mysqli_free_result($result);
}

while ($num = mysqli_fetch_array($result)) {
    echo $num;
    $duedate=$num['duedate'];
    echo $num['duedate'];
    print $num['duedate'];
    mysqli_free_result($result);
}

mysqli_close($db);

?>

echo sql gives

SELECT DISTINCT duedate FROM invoicequote WHERE     
customeraccountnumber='1000001'

Printing row count gives

Result set has 1 rows.

I know it the PHP while loop but I think because I been working on it to long and tried to convert it to function I can not see what i have done wrong.

The above code has been run separately to the main page code because the problem is with this part. The code is not the function i have stripped right back to the bare bones of the code to find the error.

Database details removed.

SQL query details are just a dummy account not real.

// Free result set
mysqli_free_result($result);

Hope you read this in your code (twice).

The mysqli_free_result() function frees the memory associated with the result.

you can't run

while($num = mysqli_fetch_array($result)){
}

with empty $result

It's as simple as removing mysqli_free_result($result); from your if block.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM