Tempremental Script

Hi There,

I have a script that is exceedingly tempremental, some days it works, some days it doesn’t and some days I don’t even think it knows what it wants to do. The script is shown below, can any one help me please on this as it’s very strange how one day it works and the next it doesn’t?

Here are the two errors:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\Program Files\xampp\htdocs\co_recentpics.php on line 24

Warning: mysql_free_result() expects parameter 1 to be resource, boolean given in C:\Program Files\xampp\htdocs\co_recentpics.php on line 28

And below is the script

<?php
	include("Connections/ilikeyours.php");

	// initialization
	$result_array = array();
	$counter = 0;

	$cid = (int)($_GET['cid']);
	$pid = (int)($_GET['pid']);

	// Category Listing

	if( empty($cid) && empty($pid) )
	{
		$number_of_categories_in_row = 4;

		$result = mysql_query( "SELECT c.category_id,c.category_name,COUNT(photo_id)
						FROM gallery_category as c
						LEFT JOIN gallery_photos as p ON p.photo_category = c.category_id
						GROUP BY c.category_id" );
		while( $row = mysql_fetch_array( $result ) )
		{
			$result_array[] = "<a href='co_recentpics.php?cid=".$row[0]."'>".$row[1]."</a> "."(".$row[2].")";
		}
		mysql_free_result( $result );	

		$result_final = "<tr>\
";

		foreach($result_array as $category_link)
		{
			if($counter == $number_of_categories_in_row)
			{	
				$counter = 1;
				$result_final .= "\
</tr>\
<tr>\
";
			}
			else
			$counter++;

			$result_final .= "\	<td>".$category_link."</td>\
";
		}

		if($counter)
		{
			if($number_of_categories_in_row-$counter)
			$result_final .= "\	<td colspan='".($number_of_categories_in_row-$counter)."'>&nbsp;</td>\
";

			$result_final .= "</tr>";
		}
	}


	// Thumbnail Listing

	else if( $cid && empty( $pid ) )
	{
		$number_of_thumbs_in_row = 5;

		$result = mysql_query( "SELECT photo_id,photo_caption,photo_filename FROM gallery_photos WHERE photo_category='".addslashes($cid)."'" );
		$nr = mysql_num_rows( $result );

		if( empty( $nr ) )
		{
			$result_final = "\	<tr><td>No Category found</td></tr>\
";
		}
		else
		{
			while( $row = mysql_fetch_array( $result ) )
			{
				$result_array[] = "<a href='co_recentpics.php?cid=$cid&pid=".$row[0]."'><img src='".$images_dir."/tb_".$row[2]."' border='0' alt='".$row[1]."' /></a>";
			}
			mysql_free_result( $result );	

			$result_final = "<tr>\
";
	
			foreach($result_array as $thumbnail_link)
			{
				if($counter == $number_of_thumbs_in_row)
				{	
					$counter = 1;
					$result_final .= "\
</tr>\
<tr>\
";
				}
				else
				$counter++;

				$result_final .= "\	<td>".$thumbnail_link."</td>\
";
			}
	
			if($counter)
			{
				if($number_of_photos_in_row-$counter)
			$result_final .= "\	<td colspan='".($number_of_photos_in_row-$counter)."'>&nbsp;</td>\
";

				$result_final .= "</tr>";
			}
		}
	}

	// Full Size View of Photo
	else if( $pid )
	{
		$result = mysql_query( "SELECT photo_caption,photo_filename FROM gallery_photos WHERE photo_id='".addslashes($pid)."'" );
		list($photo_caption, $photo_filename) = mysql_fetch_array( $result );
		$nr = mysql_num_rows( $result );
		mysql_free_result( $result );	

		if( empty( $nr ) )
		{
			$result_final = "\	<tr><td>No Photo found</td></tr>\
";
		}
		else
		{
			$result = mysql_query( "SELECT category_name FROM gallery_category WHERE category_id='".addslashes($cid)."'" );
			list($category_name) = mysql_fetch_array( $result );
			mysql_free_result( $result );	

			$result_final .= "<tr>\
\	<td>
						<a href='co_recentpics.php'>Categories</a> > 
						<a href='co_recentpics.php?cid=$cid'>$category_name</a></td>\
</tr>\
";

			$result_final .= "<tr>\
\	<td align='center'>
					<br />
					<img src='".$images_dir."/".$photo_filename."' border='0' alt='".$photo_caption."' />
					<br />
					$photo_caption
					</td>
					</tr>";
		}
	}

// Final Output
echo <<<__HTML_END

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

<head>
<title>ilikeyours | Recent Photos</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<meta name="author" content="" />
<link href="style/main.css" rel="stylesheet" title="main" type="text/css" 
media="screen" />
<link rel="shortcut icon" href="../favicon.ico">
<!--

-->
</head>
<body>
<div id="wrapper">
<div id="header">
  <div id="imagesearch">
    <form>
      <label>Search
        <input type="text" name="textfield" id="textfield" />
      </label>
      <label>
        <input type="submit" name="button" id="button" value="Search" />
      </label>
    </form>
    </div>
	<img title="I Like Yours.com" src="hold/images/sitelogo.jpg" width="343" height="100" alt="I Like Yours.com"  />
  </div>
    
    <div id="navigation">
    <ul>
    	<li><a href="index.php">home</a></li>
    	<li><a href="clientadmin/index.php">login</a></li>
        <li><strong><a href="clientadmin/register-form.php">sign-up</a></strong></li>
        <li><a href="clientadmin/co_search.php">search</a></li>
        <li><a href="co_recentpics.php">recent pictures</a></li></ul>
    </div>
    
    <div id="contentwrapper">
	<h1>Recent Pictures</h1>
<table width='100&#37;' border='0' align='center' style='width: 100%;'>
$result_final		
</table>
<p>&nbsp;</p>
  </div><div id="footer">
  <p>&copy; ### 2010<br />
	    <a href="about.php">About</a> | <a href="policy.php">Privacy Policy</a><br /></p></div>
</div>
</div>
</body>
</html>
__HTML_END;
?>


lol :slight_smile: code isn’t temperamental
the query is failing and returning false.

False as in there is nothing in the SQL table/database for it to find? or something else?

http://php.net/manual/en/function.mysql-query.php

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

If this is happening randomly, log the query if(!$result) and investigate.