Solved

Problem with code

Posted on 2014-03-28
8
214 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
[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
  • 4
  • 3
8 Comments
 
LVL 110

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 110

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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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 110

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 110

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

739 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