Solved

Whats' wrong with this query?

Posted on 2011-09-15
5
291 Views
Last Modified: 2012-05-12
$ins_result = mysql_query("SELECT * FROM users WHERE guest_faculty!='1' AND (faculty_courses LIKE '%,".$currUserCourse.",%' OR instructor_courses LIKE '%,".$currUserCourse.",%') ORDER by instructor_courses DESC");

while ($row = mysql_fetch_array($ins_result)) {
  $row['ID'];
}


I'm getting this error:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /var/www/vhosts/******/******/*****/httpdocs/sidebar_instructor.php on line 18

Line 18 is the "while()" line.

Thanks!
0
Comment
Question by:tonyhhisc
[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
  • 2
  • 2
5 Comments
 
LVL 1

Expert Comment

by:rerard
ID: 36544123
The query is surrounnded by double quotes, and then you also have double quotes in the query itself.
0
 
LVL 1

Expert Comment

by:rerard
ID: 36544138
escape the quotes with a \ ...  check this out:

http://php.net/manual/en/language.types.string.php
0
 
LVL 31

Accepted Solution

by:
Frosty555 earned 500 total points
ID: 36544187
You don't have an issue with escaping doublequotes. The PHP syntax of the line is fine. Your script inserts values for the PHP variable $currUserCourse into the query in a couple different places.

There is a syntax error with the mysql query that you are not handling. You need to do two things to debug it:

1) Assemble your SQL query separately so that you can print it and examine it.
2) Catch the MySQL error and print the error message.

Implementing the above two points your code should look like this:

$sql="SELECT * FROM users WHERE guest_faculty!='1' AND (faculty_courses LIKE '%,".$currUserCourse.",%' OR instructor_courses LIKE '%,".$currUserCourse.",%') ORDER by instructor_courses DESC";

echo "sql executed was:<br/><br/>" . $sql . "<br/><br/>";

$ins_result = mysql_query($sql) or die(mysql_error());

while ($row = mysql_fetch_array($ins_result)) {
  $row['ID'];
}

Open in new window


Now when you run it, you will print out the SQL that was executed as well as the MySQL error message that was causing the script to fail.

From there you should be able to see an obvious reason why your SQL statement is invalid.
0
 

Author Closing Comment

by:tonyhhisc
ID: 36544280
Thanks a lot Frosty you rock!!!

(by the way, it was just a stupid error on my part.... guest_courses was not a field in the database -- I'm in panic mode here as we did an upgrade and working through PHP errors everywhere!!)

Cheers!
0
 
LVL 31

Expert Comment

by:Frosty555
ID: 36917007
I can't count the number of times this has happened to me. Dying properly on error, and printing out your SQL statements always makes the debugging process much easier. Glad to help :)
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

628 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