Solved

Whats' wrong with this query?

Posted on 2011-09-15
5
288 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 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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
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…

777 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