0

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.

3
  • 3
    Are these the evil rows? mysqli_free_result($result); Commented Jan 14, 2014 at 10:49
  • Two things jump out at first glance. You don't have an else clause on your if ($result) test so if you don't get a result the script will fail silently. Also, you're trying to use $result outside of the if ($result block) where if you didn't get a result it would fail. Commented Jan 14, 2014 at 10:51
  • Added in else statement and removed the free results from while loop. It is grabbing the data fine now, Thank you GordonM and mmiltos Commented Jan 14, 2014 at 10:56

3 Answers 3

2
// Free result set
mysqli_free_result($result);

Hope you read this in your code (twice).

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.