andy
andy

Reputation: 5

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.

Upvotes: 0

Views: 953

Answers (3)

Learner
Learner

Reputation: 221

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

Upvotes: 0

miltos
miltos

Reputation: 1019

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

Upvotes: 0

msg7086
msg7086

Reputation: 461

// Free result set
mysqli_free_result($result);

Hope you read this in your code (twice).

Upvotes: 2

Related Questions