Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Whats' wrong with this query?

Posted on 2011-09-15
5
Medium Priority
?
294 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
  • 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 2000 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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

971 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