...

View Full Version : Warning: mysql_fetch_array() expects parameter 1 to be resource



jarv
01-24-2012, 06:10 PM
Please help?

I have the same site in 2 places but it's not working properly in one, I get the error: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in D:\aandheurope.com\wwwroot\index.php on line 275

Line 275:



while($row4 = mysql_fetch_array($result4))
{
echo '<a href="index.php?PageID=3&amp;CategoryID='.$CategoryID.'&amp;ProductID='.$row4['ProductID'].'"><img src="product-thumbs/'.$row4['PicturePath'].'" alt="'.$row4['$ProductName'].'" /><h4>'.$row4['ProductName'].'</h4></a>';
}


other relevant code


if($CategoryID >= 1){
$tableName="ah_products";
$limit = 10;
$targetpage = "index.php";
$query = "SELECT COUNT(*) as num FROM $tableName WHERE CategoryID=$CategoryID";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages['num'];

$stages = 3;
$page = mysql_escape_string($_REQUEST['pagec']);
if( isset($_REQUEST['pagec']) && ctype_digit($_REQUEST['pagec']) ) {
$page = (int) $_GET['pagec'];
$start = ($page - 1) * $limit;
}else{
$start = 0;
}




$query4 = "SELECT * FROM ah_products INNER JOIN ah_productpics ON ah_products.ProductID=ah_productpics.ProductID WHERE CategoryID ='$CategoryID' LIMIT $start, $limit";
$result4 = mysql_query($query4);


here are the sites:

erroring site (http://www.aandheurope.com/index.php?PageID=3&CategoryID=2)


Working site (http://www.jbiddulph.com/aandh/index.php?PageID=3&CategoryID=2)

BluePanther
01-24-2012, 06:24 PM
Please help?

I have the same site in 2 places but it's not working properly in one, I get the error: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in D:\aandheurope.com\wwwroot\index.php on line 275

Line 275:



while($row4 = mysql_fetch_array($result4))
{
echo '<a href="index.php?PageID=3&amp;CategoryID='.$CategoryID.'&amp;ProductID='.$row4['ProductID'].'"><img src="product-thumbs/'.$row4['PicturePath'].'" alt="'.$row4['$ProductName'].'" /><h4>'.$row4['ProductName'].'</h4></a>';
}


other relevant code


if($CategoryID >= 1){
$tableName="ah_products";
$limit = 10;
$targetpage = "index.php";
$query = "SELECT COUNT(*) as num FROM $tableName WHERE CategoryID=$CategoryID";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages['num'];

$stages = 3;
$page = mysql_escape_string($_REQUEST['pagec']);
if( isset($_REQUEST['pagec']) && ctype_digit($_REQUEST['pagec']) ) {
$page = (int) $_GET['pagec'];
$start = ($page - 1) * $limit;
}else{
$start = 0;
}




$query4 = "SELECT * FROM ah_products INNER JOIN ah_productpics ON ah_products.ProductID=ah_productpics.ProductID WHERE CategoryID ='$CategoryID' LIMIT $start, $limit";
$result4 = mysql_query($query4);


here are the sites:

erroring site (http://www.aandheurope.com/index.php?PageID=3&CategoryID=2)


Working site (http://www.jbiddulph.com/aandh/index.php?PageID=3&CategoryID=2)

The problem is that your query has failed, so you're passing FALSE into mysql_fetch_array. You're not doing any error reporting on mysql_query, so we don't really know what's wrong - add an 'or die(mysql_error())' to mysql_query or an if condition.


$result4 = mysql_query($query4) or die(mysql_error());

// OR
$result4 = mysql_query($query4);
if(!$result){
die(mysql_error());
}

Fou-Lu
01-24-2012, 06:42 PM
You seem to have posted quite a few issues identical to this. The cause is always the same; you have not added appropriate error handling to your code to determine if your query is successful. This can be done with an or die() call or by using if statements to determine the results (since a failure is a boolean false).

jarv
01-24-2012, 06:46 PM
thanks, it said: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

I think my problem is in the following piece of code:

if($CategoryID >= 1){
$tableName="ah_products";
$limit = 10;
$targetpage = "index.php";
$query = "SELECT COUNT(*) as num FROM $tableName WHERE CategoryID=$CategoryID";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages['num'];

$stages = 3;
$page = mysql_escape_string($_REQUEST['pagec']);
if( isset($_REQUEST['pagec']) && ctype_digit($_REQUEST['pagec']) ) {
$page = (int) $_GET['pagec'];
$start = ($page - 1) * $limit;
}else{
$start = 0;
}

Fou-Lu
01-24-2012, 06:55 PM
It could very well be. What's the parsed result of $query?

jarv
01-24-2012, 06:59 PM
the parsed result of $query?

$query counts the number of pages for the pagination. the piece of code above comes from a separate page

Fou-Lu
01-24-2012, 07:03 PM
the parsed result of $query?

$query counts the number of pages for the pagination. the piece of code above comes from a separate page

I know what the intention of it is. What's the parsed result of that string?

jarv
01-24-2012, 07:05 PM
how do I find out the parsed result of the string?

Also, JQuery is not working this is an exact copy of the working site on the same server?!

Fou-Lu
01-24-2012, 07:07 PM
jQuery is beyond the scope of this issue.
To find the parsed results, you would use print $query;.

jarv
01-24-2012, 07:11 PM
that doesn't work?!

Why doesn't this work an the same server?! hmm

BluePanther
01-24-2012, 07:20 PM
that doesn't work?!

Why doesn't this work an the same server?! hmm

What do you mean it doesn't work? We're trying to find out what $query is before it goes into the mysql_query(). The error implies that it's empty, or at least, there's an empty string in it somewhere.
I think it's actually $query4 we need to see

jarv
01-25-2012, 01:56 AM
$query4 = SELECT * FROM ah_products INNER JOIN ah_productpics ON ah_products.ProductID=ah_productpics.ProductID WHERE CategoryID ='2' LIMIT ,

tangoforce
01-25-2012, 02:09 AM
$query4 = SELECT * FROM ah_products INNER JOIN ah_productpics ON ah_products.ProductID=ah_productpics.ProductID WHERE CategoryID ='2' LIMIT ,

Now run that query in your phpmyadmin and see what it says.

Note that you have nothing after 'LIMIT' which could be a problem.

jarv
01-25-2012, 02:20 AM
yes, I got: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

but it works on my other site on the same server?!

hmm

so the problem lies here:




if($CategoryID >= 1){
$tableName="ah_products";
$limit = 10;
$targetpage = "index.php";
$query = "SELECT COUNT(*) as num FROM $tableName WHERE CategoryID=$CategoryID";

$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages['num'];

$stages = 3;
$page = mysql_escape_string($_REQUEST['pagec']);
if( isset($_REQUEST['pagec']) && ctype_digit($_REQUEST['pagec']) ) {
$page = (int) $_GET['pagec'];
$start = ($page - 1) * $limit;
}else{
$start = 0;
}

// Initial page num setup
if ($page == 0){$page = 1;}
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total_pages/$limit);
$LastPagem1 = $lastpage - 1;
$paginateproducts = '';
if($lastpage > 1)
{

$paginateproducts .= "<div id='paginate'>";
$paginateproducts .= "<div class='pagination'>";
// Previous
if ($page > 1){
$paginateproducts.= "<a href='$targetpage?pagec=$prev&pageu=1&CategoryID=$CategoryID' title='Previous Page'>&laquo; Previous</a>";
}else{
$paginateproducts.= "<span class='disabled' title='Previous page'>&laquo; Previous</span>"; }



// Pages
if ($lastpage < 7 + ($stages * 2)) // Not enough pages to breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginateproducts.= "<a href='#' class='number current'>$counter</a>";
}else{
$paginateproducts.= "<a href='$targetpage?pagec=$counter&pageu=1&CategoryID=$CategoryID' class='number'>$counter</a>";}
}
}
elseif($lastpage > 5 + ($stages * 2)) // Enough pages to hide a few?
{
// Beginning only hide later pages
if($page < 1 + ($stages * 2))
{
for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
{
if ($counter == $page){
$paginateproducts.= "<a href='#' class='number current'>$counter</a>";
}else{
$paginateproducts.= "<a href='$targetpage?pagec=$counter&pageu=1&CategoryID=$CategoryID' class='number'>$counter</a>";}
}
$paginateproducts.= "...";
$paginateproducts.= "<a href='$targetpage?pagec=$LastPagem1&pageu=1&CategoryID=$CategoryID' class='number'>$LastPagem1</a>";
$paginateproducts.= "<a href='$targetpage?pagec=$lastpage&pageu=1&CategoryID=$CategoryID' class='number'>$lastpage</a>";
}
// Middle hide some front and some back
elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
{
$paginateproducts.= "<a href='$targetpage?pagec=1&pageu=1&CategoryID=$CategoryID' class='number'>1</a>";
$paginateproducts.= "<a href='$targetpage?pagec=2&pageu=1&CategoryID=$CategoryID' class='number'>2</a>";
$paginateproducts.= "...";
for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
{
if ($counter == $page){
$paginateproducts.= "<span class='number current'>$counter</span>";
}else{
$paginateproducts.= "<a href='$targetpage?pagec=$counter&pageu=1&CategoryID=$CategoryID' class='number'>$counter</a>";}
}
$paginateproducts.= "...";
$paginateproducts.= "<a href='$targetpage?pagec=$LastPagem1&pageu=1&CategoryID=$CategoryID' class='number'>$LastPagem1</a>";
$paginateproducts.= "<a href='$targetpage?pagec=$lastpage&pageu=1&CategoryID=$CategoryID' class='number'>$lastpage</a>";
}
// End only hide early pages
else
{
$paginateproducts.= "<a href='$targetpage?pagec=1&pageu=1&CategoryID=$CategoryID' class='number'>1</a>";
$paginateproducts.= "<a href='$targetpage?pagec=2&pageu=1&CategoryID=$CategoryID' class='number'>2</a>";
$paginateproducts.= "...";
for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginateproducts.= "<a href='#' class='number current'>$counter</a>";
}else{
$paginateproducts.= "<a href='$targetpage?pagec=$counter&pageu=1&CategoryID=$CategoryID' class='number'>$counter</a>";}
}
}
}

// Next
if ($page < $counter - 1){
$paginateproducts.= "<a href='$targetpage?pagec=$next&pageu=1&CategoryID=$CategoryID' title='Next Page'>Next Page &raquo;</a>";
}else{
$paginateproducts.= "<span class='disabled' title='Next Page'>Next Page &raquo;</span>";
}

$paginateproducts.= "</div>";
$paginateproducts.= "</div>";
}
}

tangoforce
01-25-2012, 02:29 AM
but it works on my other site on the same server?!


Doesn't mean a thing. Code that works fine on one website will work differently on another. Different things such as domains, file paths, database logins etc all come in to play.

Have you even tried debugging this code line by line to see where its going wrong? - We keep telling you about this and you continue to ignore it and then post large chunks of code. Learn to debug Jarv, you'll be such a better programmer for it.

jarv
01-25-2012, 02:34 AM
I just had a brainwave and sorted it, I put the pagination code on the same page so all on one page now!

thanks!

J



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum