Solved

Whats' wrong with this query?

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
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 …

740 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