-2

Here's my code:

<?php
$product_id = $_GET['id'];

if($connect = mysqli_connect('localhost','root','','accounting')){

    $sql = "SELECT * FROM inventory WHERE product_id='$product_id'";
    $query = mysqli_query($connect,$sql);
    mysqli_free_result($query);
    mysqli_close($connect);
}else{
print "Connection failed";
}

while ( $fetch = mysqli_fetch_object($query) ) {
    print $fetch->id;
}

?>

i don't know why my code doesn't work !, it seems correct. help me guys :)

user3780717
  • 5
  • 1
  • 2
  • *help me guys* won't help you fetch answers... – Mr. Alien Jun 26 '14 at 19:29
  • There could be a multitude of problems: bad credentials, bad query syntax, querying for columns that don't exist, invalid/non-existent "`product_id`", improperly installed PHP, or maybe the fact that there's **no such function as [`mysqli_free_result()`](http://us2.php.net/manual-lookup.php?pattern=mysqli_free_result&scope=quickref)**. Without more context, nobody here could _possibly_ know what your specific problem is. – esqew Jun 26 '14 at 19:30
  • @esqew Apparently there is http://www.php.net/manual/en/mysqli-result.free.php – Funk Forty Niner Jun 26 '14 at 19:40
  • @Fred-ii- I stand corrected. I wonder why it doesn't show up in a search of the manual. – esqew Jun 26 '14 at 19:41
  • @esqew I wondered that too, till I found it in an SO question http://stackoverflow.com/q/14088155/ via Google. – Funk Forty Niner Jun 26 '14 at 19:42
  • @Fred-ii- Hmph. Manual bug? Maybe I'll submit something. – esqew Jun 26 '14 at 19:43
  • @esqew I thought that also, 2nd Google entry found is a link to http://www.php.net/mysqli_free_result and the 3rd being http://www.php.net//manual/en/mysqli-result.free.php when Googling "mysqli_free_result". Oh well. – Funk Forty Niner Jun 26 '14 at 19:45
  • 1
    Do `var_dump($product_id);` placed under `$product_id = $_GET['id'];` and `var_dump($sql);` after your query, see what that echos out. Plus, add error reporting to the top of your file(s) `error_reporting(E_ALL); ini_set('display_errors', 1);` see if that yields any errors. – Funk Forty Niner Jun 26 '14 at 19:47
  • 1
    @Fred-ii- not to clog up this comment thread anymore, but I [put in a bug for this](https://bugs.php.net/bug.php?id=67527). – esqew Jun 26 '14 at 19:50

1 Answers1

0
mysqli_free_result($query);

This discards the result, so it shouldn't be a surprise that you can't fetch from it after you free it!

http://www.php.net/manual/en/mysqli-result.free.php says:

You should always free your result with mysqli_free_result(), when your result object is not needed anymore.

(emphasis mine)

Move the call to mysqli_free_result() to after your while loop is done.

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828