Managing State Information

Hi,
I am following a problem step by step in my PHP book and not able to get the code to work.  It may be depreciated, but I need to get this to work.    The error message I am getting is:
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\XAMPPII\htdocs\WEBWIREpub.com\CWB208\Chapter.09\ZIP\AvailableOpportunities.php on line 94
I am on a Windows 7 machine using a Xampp localhost.  I can send the code, at this time it consists of
AvailableOpportunities.php
CancelSelection.php
InternLogin.php
Opportunities.txt
RegisterIntern.php and
VerifyLogin.php
These are all straight from the book, I am a student and do not expect answers, just pointers or hints if possible.

Thank you,
Deanna
Deanna AndruAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Deanna AndruAuthor Commented:
Sorry,
Line 90:
if (mysql_num_rows($QueryResult) > 0) {
      while (($Row = mysql_fetch_assoc($QueryResult))
      !== FALSE)
            $Opportunities[] = $Row;
            mysql_free_result($QueryResult);
}
0
Deanna AndruAuthor Commented:
94, line 94.  sorry typo
0
Paul MacDonaldDirector, Information SystemsCommented:
It would appear that $QueryResult is empty in
if (mysql_num_rows($QueryResult) > 0) {

Can you verify that $QueryResult has a value at that point?
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Deanna AndruAuthor Commented:
Opportunities loaded into the table from from opportunities.txt.  Three tables were created; assigned_opportunities, interns and opportunities.  There are 5 Rows in the opportunities table, all populated with the information from the text. (company, city, start date, position and description.  Hmmm, but why no query results...  
Providing the code will keep troubleshooting.
AvailableOpportunities.php
InternLogin.php
RegisterIntern.php
opportunities.txt
CancelSelection.php
0
Deanna AndruAuthor Commented:
appreciate the assist!:-)
0
Ray PaseurCommented:
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given...
This almost always means the query failed.  Often this is caused by a syntax error in the query string, but MySQL is not a black box.  It can and will fail for reasons that are outside of your control.  Your scripts need to test the results of the queries in order to detect errors and visualize the error messages.  All of these functions are documented in the online PHP man pages.  For example, see this one:
http://php.net/manual/en/function.mysql-num-rows.php

You probably noticed the large red warning box that tells you to get rid of MySQL!

Rather than write all of this out over and over, I wrote an article about how to make the move away from MySQL and onto one of the currently supported extensions.  It contains code examples that map the familiar but obsolete MySQL extension to the MySQLi and PDO extensions.  It also shows how to test queries for success or failure and how to visualize any error messages associated with the query failures.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
Ray PaseurCommented:
Now that I've looked at some of the code, there may be some other things to consider.  Since you're relatively new to PHP, you might find it worth avoiding certain practices that can make your life difficult.  One of these is the use of the @ prepended to a function call.  That's a practice that can lead to silent and unexplained failures in PHP scripts and it should be avoided unless you know that you're willing to accept a functional failure.  Some other "anti-practices" are explained here:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12293-AntiPHPatterns-and-AntiPHPractices.html
0
Deanna AndruAuthor Commented:
Sorry, I am just able to get back.  I have searched and am not able to find an @ prepended to a function call in this script anywhere.  I am following step-by-step, like I said.  Some of the code was prepared for the assignment in advance.  What document are you seeing this on?  There is one @ symbol on the RegisterIntern.php page, but that is in the preg_match on  the e-mail verification pattern...
will get back with what I can deduce here...
0
Deanna AndruAuthor Commented:
change the parameter maybe?..
0
Ray PaseurCommented:
From AvailableOpportunities.php, please see line 18 which is the first of several uses of the @ on function calls.  This is a code smell, and is to be avoided.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
<!-- Deanna L. Andru, November 3rd, 2014-->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>College Internship Available Opportunities</title>
</head>
<body>
<h1>College Internship</h1>
<h2>Available Opportunities</h2>
<?php
if (isset($_REQUEST['internID']))
	$InternID = $_REQUEST['internID'];
else
	$InternID = −1;
$errors = 0;
$DBConnect = @mysql_connect("localhost", "root","JOBO");
if ($DBConnect === FALSE) {
	echo "<p>Unable to connect to the database server. " . "Error code " . mysql_errno() . ": " . mysql_error() . "</p>\n";
	++$errors;
}
else {
	$DBName = "internships";
	$result = @mysql_select_db($DBName,
	$DBConnect);
	if ($result === FALSE) {
		echo "<p>Unable to select the database. " . "Error code " . mysql_errno($DBConnect) . ": " . mysql_error($DBConnect) . "</p>\n";
		++$errors;
	}
}
$TableName = "interns";
if ($errors == 0) {
	$SQLstring = "SELECT * FROM $TableName WHERE " .
		" internID='$InternID'";
	$QueryResult = @mysql_query($SQLstring, $DBConnect);
	if ($QueryResult === FALSE) {
		echo "<p>Unable to execute the query. " . "Error code " . mysql_errno($DBConnect) . ": " . mysql_error($DBConnect) . "</p>\n";
		++$errors;
	}
	else {
		if (mysql_num_rows($QueryResult) == 0) {
			echo "<p>Invalid Intern ID!</p>";
			++$errors;
		}
	}
}
if ($errors == 0) {
	$Row = mysql_fetch_assoc($QueryResult);
	$InternName = $Row['first'] . " " . $Row['last'];
} else
	$InternName = "";
$TableName = "assigned_opportunities";
$ApprovedOpportunities = 0;
$SQLstring = "SELECT COUNT(opportunityID) FROM
$TableName " .
	" WHERE internID='$InternID' " .
	" AND date_approved IS NOT NULL";
$QueryResult = @mysql_query($SQLstring, $DBConnect);
if (mysql_num_rows($QueryResult) > 0) {
	$Row = mysql_fetch_row($QueryResult);
	$ApprovedOpportunities = $Row[0];
	mysql_free_result($QueryResult);
}
$SelectedOpportunities = array();
$SQLstring = "SELECT opportunityID FROM $TableName " .
	" WHERE internID='$InternID'";
$QueryResult = @mysql_query($SQLstring, $DBConnect);
if (mysql_num_rows($QueryResult) > 0) {
	while (($Row = mysql_fetch_row($QueryResult))
	!== FALSE)
		$SelectedOpportunities[] = $Row[0];
	mysql_free_result($QueryResult);
}
$AssignedOpportunities = array();
$SQLstring = "SELECT opportunityID FROM $TableName " .
	" WHERE date_approved IS NOT NULL";
$QueryResult = @mysql_query($SQLstring, $DBConnect);
if (mysql_num_rows($QueryResult) > 0) {
	while (($Row = mysql_fetch_row($QueryResult))
	!== FALSE)
		$AssignedOpportunities[] = $Row[0];
mysql_free_result($QueryResult);
}
$TableName = "opportunities";
$Opportunities = array();
$SQLstring = "SELECT opportunityID, company, city, " .
	" start_date, end_date, position,
	description " .
	" FROM $TableName";
$QueryResult = @mysql_query($SQLstring, $DBConnect);
$QueryResult = @mysql_query($SQLstring, $DBConnect);
if (mysqli_num_rows($QueryResult) > 0) {
    while (($Row = mysqli_fetch_assoc($QueryResult)) !== FALSE)
	      $Opportunities[] = $Row;
		  mysqli_free_result($QueryResult);
}
mysql_close($DBConnect);
echo "<table border='1' width='100%'>\n";
echo "<tr>\n";
echo "
<th style='background-color:cyan'>Company</
th>\n";
echo "
<th style='background-color:cyan'>City</th>\n";
echo " <th style='background-color:cyan'>Start
Date</th>\n";
echo " <th style='background-color:cyan'>End
Date</th>\n";
echo "
<th style='background-color:cyan'>Position</
th>\n";
echo "
<th style='background-color:cyan'>Description</
th>\n";
echo " <th style='background-color:cyan'>Status</
th>\n";
echo "</tr>\n";
foreach ($Opportunities as $Opportunity) {
	if (!in_array($Opportunity['opportunityID'],
			$AssignedOpportunities)) {
		echo "<tr>\n";
	echo " <td>" .
		htmlentities($Opportunity['company']) .
		"</td>\n";
	echo " <td>" .
		htmlentities($Opportunity['city']) .
		"</td>\n";
	echo " <td>" .
		htmlentities($Opportunity
		['start_date']) .
		"</td>\n";
	echo " <td>" .
		htmlentities($Opportunity['end_date']) .
		"</td>\n";
	echo " <td>" .
		htmlentities($Opportunity['position']) .
		"</td>\n";
	echo " <td>" .
		htmlentities($Opportunity
		['description']) .
		"</td>\n";
	echo " <td>";
	if (in_array($Opportunity['opportunityID'],
			$SelectedOpportunities))
		 echo "Selected<br />" .
"<a href='CancelSelection.php?" .
SID . "&opportunityID=" .
$Opportunity['opportunityID'] .
"'>Cancel Selection</a>";
	else {
		if ($ApprovedOpportunities>0)
			echo "Open";
		else
			echo "<a href=
			'RequestOpportunity.php?" .
			"internID=$InternID&" .
			"opportunityID=" .
			$Opportunity['opportunityID'] .
			"'>Available</a>";
		}
	echo "</td>\n";
	echo "</tr>\n";
	}
}
echo "</table>\n";
echo "<p><a href='InternLogin.php'>Log Out</a></
p>\n";
?>
</body>
</html>

Open in new window

0
Deanna AndruAuthor Commented:
OK, I appreciate the pointers.  After checking text corrections, it seems the text has quite a few errata's this chapter,   Resolved this error, now getting another error, so new issue.  I thank you for the assistance once again.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
Not that it matters very much to me because I have enough points to orbit Saturn, but when you ask a question and get an answer it's traditional to award points to the experts who tried to help you.  Points are the only rewards the Experts receive.  You might find that simple points-based economics influences the effort of the Experts who try to help.
0
Deanna AndruAuthor Commented:
Very good advice but not specific to the issue.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.