Solved

Managing State Information

Posted on 2014-11-05
13
136 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 

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 110

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 110

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 110

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 110

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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

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…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

728 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