Jump to content

Error: Warning: mysql_fetch_array() expects parameter 1 to be resource, null giv


Plagel

Recommended Posts

Full error:

Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in C:\Users\plagel\Desktop\local\movie_details.php on line 96

 

I know it's something so simple. Here's the code:

As an aside, do you coder-folks get heart pains when you drink 24 diet cokes and stay up for 36 hours trying to find the semi-colon? :(

 

<?php 
ini_set ("display_errors", "1"); 
error_reporting(E_ALL & ~E_NOTICE);
$link = mysql_connect("localhost","root","pass")
or die(mysql_error());
mysql_select_db("moviesite")
or die(mysql_error());

/*Function to calculate if a movie made a profit, loss or broke even */
function calculate_differences($takings, $cost) {
$difference = $takings - $cost;

if ($difference < 0) {
	$difference = substr($difference, 1);
	$font_color = 'red';
	$profit_or_loss = "$" . $difference . "m";
}elseif ($difference > 0) {
	$font_color = 'green';
	$profit_or_loss = "$" . $difference . "m";
}else {
	$font_color = 'blue';
	$profit_or_loss = "Broke even";
}
return "<font color=\"$font_color\">" . $profit_or_loss . "</font>";
}

/* Function to get the director's name from the people table */
function get_director() {
global $movie_director;
global $director;

$query_d = "SELECT people_fullname " .
	   "FROM people " .
	   "WHERE people_id='$movie_director'";
$results_d = mysql_query($query_d)
	or die(mysql_error());
$row_d = mysql_fetch_array($results_d);
extract($row_d);
$director = $people_fullname;
}

/*Function to get the lead actor's name from the people table */
function get_leadactor() {
global $movie_leadactor;
global $leadactor;



$query_a = "SELECT people_fullname " .
	   "FROM people " .
	   "WHERE people_id='$movie_leadactor'";
$results_a = mysql_query($query_a)
	or die(mysql_error());
$row_a = mysql_fetch_array($results_a);
extract($row_a);
$leadactor = $people_fullname;

}

/*Function to generate ratings*/
function generate_ratings($review_rating) {
			$movie_rating = '';
			for($i=1; $i<$review_rating; $i++) {
							$movie_rating .= "<img src=\"http://www.best-of-web.com/_images/070731-210357.jpg\"> ";
			}
			return $movie_rating;
}

$movie_query = "SELECT * FROM movie " .
 "WHERE movie_id ='" . $_GET['movie_id'] . "'";

$movie_result = mysql_query($movie_query, $link)
or die(mysql_error());

$movie_table_headings=<<<EOD
<tr>
	<th>Movie Title</th>
	<th>Year of Release</th>
	<th>Movie Director</th>
	<th>Movie Lead Actor</th>
	<th>Movie Running Time</th>
	<th>Movie Health</th>
</tr>
EOD;
$review_table_headings=<<<EOD
<tr>
	<th>Date of Review</th>
	<th>Review Title</th>
	<th>Reviewer Name</th>
	<th>Movie Review Comments</th>
	<th>Rating</th>
</tr>
EOD;


while($review_row = mysql_fetch_array($review_result)) {
$review_flag =1;
$review_title[] = $review_row['review_name'];
$reviewer_name[] = ucwords($review_row['review_reviewer_name']);
$review[] = $review_row['review_comment'];
$review_date[] = $review_row['review_date'];
$review_rating[] = generate_ratings($review_row['review_rating']);
}

$i = 0;
$review_details = '';
while ($i<sizeof($review)) {
			$review_details .=<<<EOD
			<tr>
				<td width="15%" valign="top" align="center">$review_date[$i]</td>
				<td width="15%" valign="top">$review_title[$i]</td>
				<td width="10%" valign="top">$reviewer_name[$i]</td>
				<td width="50%" valign="top">$review[$i]</td>
				<td width="10%" valign="top" align="center">$review_rating[$i]</td>
			</tr>
EOD;
			$i++;
}
while ($row = mysql_fetch_array($movie_result)) {
$movie_name = $row['movie_name'];
$movie_director = $row['movie_director'];
$movie_leadactor = $row['movie_leadactor'];
$movie_year = $row['movie_year'];
$movie_running_time = $row['movie_running_time']." mins";
$movie_takings = $row['movie_takings'];
$movie_cost = $row['movie_cost'];

//get director's name from people table
get_director();

//get lead actor's name from people table
get_leadactor();

}

$review_query = "SELECT * FROM reviews " .
							"WHERE review_movie_id = '" . $_GET['movie_id'] . "' " .
							"ORDER BY review_date DESC";
$review_result = mysql_query($review_query, $link)
			or die(mysql_error());

$movie_health = calculate_differences($movie_takings, $movie_cost);
$page_start =<<<EOD
<html>
<head>
<title>Details and Reviews for: $movie_name</title>
</head>
<body>
EOD;

$movie_details =<<<EOD
<table width="70%" border="0" cellspacing="2" cellpadding="2" align="center">
<tr>
	<th colspan="6"><u><h2>$movie_name: Details</h2></u></th>
</tr>
$movie_table_headings
<tr>
	<td width="33%" align="center">$movie_name</td>
	<td align="center">$movie_year</td>
	<td align="center">$director</td>
	<td align="center">$leadactor</td>
	<td align="center">$movie_running_time</td>
	<td align="center">$movie_health</td>
</tr>
</table>
<br>
<br>
EOD;
if ($review_flag) {
			$movie_details .=<<<EOD
<table width="95%" border="0" cellspacing="2" cellpadding="20" align="center">
$review_table_headings
$review_details
</table>
EOD;
}
$page_end =<<<EOD
</body>
</html>
EOD;
$detailed_movie_info =<<<EOD
$page_start
$movie_details
$page_end
EOD;

echo $detailed_movie_info;
mysql_close($link);

?>

Link to comment
Share on other sites

Actually, upon further review (you must have created this code on a MAC because copy/pasting it results in a mess of tab/new-lines) the two functions I mentioned as a possible cause are present in the source code and aren't the cause of the problem.

 

Your query that is setting $review_result is located after the code that is using $review_result, so yes, the value you are passing mysql_fetch_array() does not exist and is a null value.

Link to comment
Share on other sites

Well, I'm definitely not on a MAC..it MAY be because I'm using VIM for Windows..

I moved the query to before I use it, lol duh thanks..but now I get:

"Query was empty"

 

<?php 
ini_set ("display_errors", "1"); 
error_reporting(E_ALL & ~E_NOTICE);
$link = mysql_connect("localhost","root","pass")
or die(mysql_error());
mysql_select_db("moviesite")
or die(mysql_error());

/*Function to calculate if a movie made a profit, loss or broke even */
function calculate_differences($takings, $cost) {
$difference = $takings - $cost;

if ($difference < 0) {
	$difference = substr($difference, 1);
	$font_color = 'red';
	$profit_or_loss = "$" . $difference . "m";
}elseif ($difference > 0) {
	$font_color = 'green';
	$profit_or_loss = "$" . $difference . "m";
}else {
	$font_color = 'blue';
	$profit_or_loss = "Broke even";
}
return "<font color=\"$font_color\">" . $profit_or_loss . "</font>";
}
$review_result = mysql_query($review_query, $link)
			or die(mysql_error());
/* Function to get the director's name from the people table */
function get_director() {
global $movie_director;
global $director;

$query_d = "SELECT people_fullname " .
	   "FROM people " .
	   "WHERE people_id='$movie_director'";
$results_d = mysql_query($query_d)
	or die(mysql_error());
$row_d = mysql_fetch_array($results_d);
extract($row_d);
$director = $people_fullname;
}

/*Function to get the lead actor's name from the people table */
function get_leadactor() {
global $movie_leadactor;
global $leadactor;



$query_a = "SELECT people_fullname " .
	   "FROM people " .
	   "WHERE people_id='$movie_leadactor'";
$results_a = mysql_query($query_a)
	or die(mysql_error());
$row_a = mysql_fetch_array($results_a);
extract($row_a);
$leadactor = $people_fullname;

}

/*Function to generate ratings*/
function generate_ratings($review_rating) {
			$movie_rating = '';
			for($i=1; $i<$review_rating; $i++) {
							$movie_rating .= "<img src=\"http://www.best-of-web.com/_images/070731-210357.jpg\"> ";
			}
			return $movie_rating;
}

$movie_query = "SELECT * FROM movie " .
 "WHERE movie_id ='" . $_GET['movie_id'] . "'";

$movie_result = mysql_query($movie_query, $link)
or die(mysql_error());

$movie_table_headings=<<<EOD
<tr>
	<th>Movie Title</th>
	<th>Year of Release</th>
	<th>Movie Director</th>
	<th>Movie Lead Actor</th>
	<th>Movie Running Time</th>
	<th>Movie Health</th>
</tr>
EOD;
$review_table_headings=<<<EOD
<tr>
	<th>Date of Review</th>
	<th>Review Title</th>
	<th>Reviewer Name</th>
	<th>Movie Review Comments</th>
	<th>Rating</th>
</tr>
EOD;


while($review_row = mysql_fetch_array($review_result)) {
$review_flag =1;
$review_title[] = $review_row['review_name'];
$reviewer_name[] = ucwords($review_row['review_reviewer_name']);
$review[] = $review_row['review_comment'];
$review_date[] = $review_row['review_date'];
$review_rating[] = generate_ratings($review_row['review_rating']);
}

$i = 0;
$review_details = '';
while ($i<sizeof($review)) {
			$review_details .=<<<EOD
			<tr>
				<td width="15%" valign="top" align="center">$review_date[$i]</td>
				<td width="15%" valign="top">$review_title[$i]</td>
				<td width="10%" valign="top">$reviewer_name[$i]</td>
				<td width="50%" valign="top">$review[$i]</td>
				<td width="10%" valign="top" align="center">$review_rating[$i]</td>
			</tr>
EOD;
			$i++;
}
while ($row = mysql_fetch_array($movie_result)) {
$movie_name = $row['movie_name'];
$movie_director = $row['movie_director'];
$movie_leadactor = $row['movie_leadactor'];
$movie_year = $row['movie_year'];
$movie_running_time = $row['movie_running_time']." mins";
$movie_takings = $row['movie_takings'];
$movie_cost = $row['movie_cost'];

//get director's name from people table
get_director();

//get lead actor's name from people table
get_leadactor();

}

$review_query = "SELECT * FROM reviews " .
							"WHERE review_movie_id = '" . $_GET['movie_id'] . "' " .
							"ORDER BY review_date DESC";


$movie_health = calculate_differences($movie_takings, $movie_cost);
$page_start =<<<EOD
<html>
<head>
<title>Details and Reviews for: $movie_name</title>
</head>
<body>
EOD;

$movie_details =<<<EOD
<table width="70%" border="0" cellspacing="2" cellpadding="2" align="center">
<tr>
	<th colspan="6"><u><h2>$movie_name: Details</h2></u></th>
</tr>
$movie_table_headings
<tr>
	<td width="33%" align="center">$movie_name</td>
	<td align="center">$movie_year</td>
	<td align="center">$director</td>
	<td align="center">$leadactor</td>
	<td align="center">$movie_running_time</td>
	<td align="center">$movie_health</td>
</tr>
</table>
<br>
<br>
EOD;
if ($review_flag) {
			$movie_details .=<<<EOD
<table width="95%" border="0" cellspacing="2" cellpadding="20" align="center">
$review_table_headings
$review_details
</table>
EOD;
}
$page_end =<<<EOD
</body>
</html>
EOD;
$detailed_movie_info =<<<EOD
$page_start
$movie_details
$page_end
EOD;

echo $detailed_movie_info;


mysql_close($link);

?>

Link to comment
Share on other sites

  • 4 months later...

I have the same problem can anyone explain me why this error is coming??

Warning: mysql_fetch_array() expects parameter 1 to be resource, null given

 

this is my code..

 

public static function cal_total_price($cid){

 

global $database;

$query=sprintf("SELECT product_id FROM tblcartitem WHERE cart_id=%s",

mysql_real_escape_string($cid)

);

$result=mysql_query($query);

echo("no of rows : ".mysql_num_rows($result));

while($record=mysql_fetch_array($record))

{

echo $record[0];

}

 

}

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.