Solved

Managing State Information

Posted on 2014-11-05
13
119 Views
Last Modified: 2014-11-13
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
0
Comment
Question by:Deanna Andru
  • 8
  • 4
13 Comments
 

Author Comment

by:Deanna Andru
ID: 40424363
Sorry,
Line 90:
if (mysql_num_rows($QueryResult) > 0) {
      while (($Row = mysql_fetch_assoc($QueryResult))
      !== FALSE)
            $Opportunities[] = $Row;
            mysql_free_result($QueryResult);
}
0
 

Author Comment

by:Deanna Andru
ID: 40424366
94, line 94.  sorry typo
0
 
LVL 34

Expert Comment

by:Paul MacDonald
ID: 40424379
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:Deanna Andru
ID: 40424488
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
 

Author Comment

by:Deanna Andru
ID: 40424500
appreciate the assist!:-)
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40424549
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
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40424565
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
 

Author Comment

by:Deanna Andru
ID: 40429494
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
 

Author Comment

by:Deanna Andru
ID: 40429521
change the parameter maybe?..
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40429523
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
 

Accepted Solution

by:
Deanna Andru earned 0 total points
ID: 40430645
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
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40430706
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
 

Author Closing Comment

by:Deanna Andru
ID: 40439495
Very good advice but not specific to the issue.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question