Solved

Problem with code

Posted on 2014-03-28
8
209 Views
Last Modified: 2014-03-31
I keep getting an error on the last line of this code.  I'm sure it's something stupid that I can't see.

The error is:

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /home/langsyst/public_html/Walk_Map_Display.php on line 79

Line 79 is the last line in this code.

// ****************************************************
// * Get north and south from mapfile record.         *
// ****************************************************

$SqlString1 = "SELECT * FROM mapfile WHERE 
					`SEQ` LIKE \"$work\"
				and 
					`BNMB` = \"$address\"";	

$mapfile = mysql_query($SqlString1,$conn);
$ROW = Mysql_fetch_assoc($mapfile, MYSQL_BOTH);
$north = $ROW['NOR'];
$south = $ROW['SOU'];
$_SESSION['SOUTH'] = $north;
$_SESSION['NORTH'] = ($north - 1);

			
// ****************************************************
// * Get new address from north and south             *
// ****************************************************
$SqlString2 = "SELECT * FROM mapfile WHERE 
					`NOR= \"$north\"
				and 
					`SOU` = \"$south\"";
					echo "Str2 = ";
					echo $SqlString2;	
$mapfile2 = mysql_query($SqlString2,$conn);		
$ROW1 = Mysql_fetch_assoc($mapfile2, MYSQL_BOTH);

Open in new window


Thanks
Glenn
0
Comment
Question by:breeze351
  • 4
  • 3
8 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
The usual cause of this error is a failure in the query.  MySQL_Query() has return values and the script must test these to know if the function worked or failed.  Relying on the function to work without testing the return value is AntiPractice #24.

Examples showing how to test the return values from SQL functions are shown in this article.  Scan for Run the CREATE TABLE Query and Handle Errors/Exceptions and look for the way the sample scripts handle the return value from the query functions.
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 82

Expert Comment

by:Dave Baldwin
Comment Utility
A couple of errors.  Missing a 'backtik' after `NOR and I believe that data in MySQL statements is normally enclosed in single quotes.  Try this.
$SqlString2 = "SELECT * FROM mapfile WHERE 
	`NOR` =  '$north' AND `SOU` =  '$south'";

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Dave: The missing backtick is a sure problem, and when Glenn adds the error visualization code after the query, that will jump out in a way that gives immediate feedback.  I think the embedded quotes can be either kind, so long as they are escaped, if necessary.  And the backticks are probably not needed at all, unless you've named a column using blanks or SQL reserved words.  NOR and SOU are not reserved words.
0
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 500 total points
Comment Utility
You're right about the quotes for strings.  https://dev.mysql.com/doc/refman/5.5/en/string-literals.html   I tend to prefer the single quotes in SQL statements in PHP because it makes it easier for me to read by avoiding all the \" escapes.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Yep, me too.  Just makes for fewer typographical errors, and anyone who knows me knows that I am the acknowledged King of Typos.
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
Glenn, the reason that line is an error is because query on the line before failed.
0
 

Author Comment

by:breeze351
Comment Utility
I figured it out.  It was the single quotes
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
You won't have to "figure it out" if you write the code following the standard best practices -- PHP will tell you!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now