Solved

Problem with code

Posted on 2014-03-28
8
215 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
WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

 
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

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…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.

623 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