Solved

Problem with code

Posted on 2014-03-28
8
211 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 109

Expert Comment

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

Expert Comment

by:Dave Baldwin
ID: 39962507
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 109

Expert Comment

by:Ray Paseur
ID: 39962527
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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 39962660
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
 
LVL 109

Expert Comment

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

Expert Comment

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

Author Comment

by:breeze351
ID: 39967231
I figured it out.  It was the single quotes
0
 
LVL 109

Expert Comment

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
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…
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.

770 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