Solved

Whats' wrong with this query?

Posted on 2011-09-15
5
278 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now