php error in mysql execution

Go To StackoverFlow.com

-1

Ok I have this code:

//this is the hi.php//
<?php
//highlight items//
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("juliver", $con);

$result = mysql_query("SELECT * FROM hi WHERE pp='2'");

$hi = "";

while($row = mysql_fetch_array($result)) //<--- this is the line 13//
  {
  $hi .= "<div id='hicontainer'><a id='download_now' class='imgx' href='#?w=700' rel='popup'><img src='".$row['name']."' />";
  $hi .= "<p>".$row['title']."</p>";
  $hi .= "<a href='#?w=700' id='".$row['id']."' rel='popup' class='imgx'>View full</a>    </div>";
  }

//Lots of lots of code here I just specified those code which in error.//

mysql_close($con);

?>

and heres where im going to display the output actually.

<td>
<!--highlight items-->
<div id="tbtitle">
<img src="Images/galleryicon.png"/><p>Highlight items</p>
</div>
<div id="tblgal">
<? echo $hi; ?>
</div>
</td>

But i get an error saying: "Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\madeinusa\hi.php on line 13."

please, im stuck with this. Thank you in advance.

2012-04-04 04:45
by Juliver Galleto
Tip: always do a search for the exact error message. You'll find tons of results: http://stackoverflow.com/search?q=mysqlfetcharray%28%29+expects+parameter+1+to+be+resource%2C+boolean+give - deceze 2012-04-04 04:48
Also, echo mysql_error() can help solve most issues. Do so beneath your mysql_query(...) - MichaelRushton 2012-04-04 05:07


-1

You probably have an error in your query "SELECT * FROM hi WHERE pp='2'"
Try executing that query directly in mysql and see what happens. Make sure you have the table names and columns spelt right.

Also, try quoting the table names and columns in backtick ` characters to avoid accidentally using MySQL reserved words.

2012-04-04 04:50
by Paul
He should always throw an exception or a generic error message to the browse - Ryan 2012-04-04 05:12


1

$result has a boolean value - probably because your query failed.

You should validate the result, before trying to use it.

After your query, you could do something like the following to see why your query fails:

if(!$result)
  exit(mysql_error());
2012-04-04 05:34
by Repox


1

or, more simply, you can use

$result = mysql_query("SELECT * FROM hi WHERE pp='2'") or die(mysql_error());

2012-04-04 05:52
by Mukesh Soni


0

check to this:

mysql_connect("localhost","username","password");
2012-04-04 06:22
by HDK
if connection was not established, it would already exit the code with the error "'Could not connect: "... - Nishu Tayal 2012-04-04 06:25


0

First you should use proper exception handling in the code. After the line

$result = mysql_query("SELECT * FROM hi WHERE pp='2'");

you should check whether it returns resource id or any error in the way

if(!$result){
    // if you use try-catch, then use
   throw new Exception(mysql_error());
   // Or you can use
   die(mysql_error());
}else{
     while($row = mysql_fetch_array($result)) //<--- this is the line 13//
     {
        $hi .= "<div id='hicontainer'><a id='download_now' class='imgx' href='#?w=700'     rel='popup'><img src='".$row['name']."' />";
        $hi .= "<p>".$row['title']."</p>";
        $hi .= "<a href='#?w=700' id='".$row['id']."' rel='popup' class='imgx'>View    full</a>    </div>";
     }

}    
2012-04-04 06:23
by Nishu Tayal


-1

"SELECT * FROM hi WHERE pp='2'" query may return false means there are no records that full fill query.Try echo mysql_num_rows($result); in order to know no of rows return for query.

2012-04-04 05:40
by Hardeep Pandya
If there are no results, mysql_query will return an empty resultset, not false - deceze 2012-04-04 05:42
but value of variable will be false as there is empty resultset in quer - Hardeep Pandya 2012-04-04 06:48
No, an empty resultset is a mysql resource type. It is not a boolean false. mysql_query only returns a boolean false if something was wrong with the query and it could not be executed - deceze 2012-04-04 06:51
Ads