Solved

Cant solve "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource"

Posted on 2007-11-20
11
3,044 Views
Last Modified: 2013-12-13
Hi
I am new to PHP and am having trouble with collecting the results of a MySQL Select statement. Basically on the html page a user enters a search keyword which is passed into the below code. I want to search my database for this keyword (or keywords) to find it and then output the relevant row data.

Trouble is no matter what I do, I keep getting the below error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

<?php

//include("config.php");
$host = "localhost";
$user = "username";
$password = "password";
$dbname = "db1";

$searchwords = $_GET['searchkeywords'];
$searchwords = $searchwords."%";
print $searchwords;

$link = mysql_connect ($host, $user, $password);

// Search for keywords
$query = "select spkname, spkcat from $dbname.speakers where spkKeyword1 like $searchwords";
$result = mysql_db_query ($dbname, $query, $link);

echo '<table border="0">';

while ($row = mysql_fetch_array($result)) {
      $spkname = $row[spkName];
      $spkcat  = $row[spkCategory];

   echo '<tr>';
   echo '<td width="200">';
   echo '<font size="-3" color="#643366" face="Helvetica, Geneva, Arial, SunSans-Regular, sans-serif"><a href="'.$spkname.'">'.$spkname.'</a></font>';
   echo '</td>';
   echo '<td width="200">';
   echo '<font size="-3" color="#643366" face="Helvetica, Geneva, Arial, SunSans-Regular, sans-serif"><I> ('.$spkcat.')</I>';
   echo '</td>';
   echo '</tr>';
}

echo '</table>';

mysql_close ($link);

?>
0
Comment
Question by:adr2205
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20322091
please try this:

$query = "select spkname, spkcat from $dbname.speakers where spkKeyword1 like '$searchwords' ";
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 20322094
This error is usually caused by a faulty query. Try adding quotes in your WHERE clause

select spkname, spkcat from $dbname.speakers where spkKeyword1 like '$searchwords'
0
 

Author Comment

by:adr2205
ID: 20322117
Thanks for looking into my problem - I have added the ' marks around $searchwords at the end of the query and still get the same error. Any other ideas? Thanks a lot!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20322156
then we need to get the actual error for the query:

$result = mysql_db_query ($dbname, $query, $link) or die(mysql_error());

Open in new window

0
 
LVL 7

Expert Comment

by:dansoto
ID: 20322171
As they said it's usually an error with the query.  The best thing to do is isolate the query from the PHP and troubleshoot from there.  I would run the query from a command line (query browser, phpmyadmin..etc..) and substitute real values for the variables to make sure it produces valid output...
0
 

Author Comment

by:adr2205
ID: 20322172
Is there an easier way to get at the data instead of doing it like this? The query works when I enter it directly into MySQL so thats fine - I guess I want to know what is the best way to get at each returned rows fields?
0
 
LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 25 total points
ID: 20322178
OK - it is still probably caused by the query failing to run. Change the code to

$query = "select spkname, spkcat from $dbname.speakers where spkKeyword1 like '$searchwords'";
echo $query;
$result = mysql_db_query ($dbname, $query, $link) or die( "The error was " . mysql_error() );


Tale the output from the "echo" and post into phpmyadmin's query window and run it. See what it complains about. Chances are

1. Mis-spelled field name
2. Mis-spelled table name
3. Database link not established
0
 

Author Comment

by:adr2205
ID: 20322233
Excellent - adding the die output showed me I had misspelt one of the column names! Thanks ..... but for further points can you please let me know if this is the best way to get at each field in the returned rows?

echo '<table border="0">';

while ($row = mysql_fetch_array($result)) {
      $spkname = $row[spkName];
      $spkcat  = $row[spkCategory];

   echo '<tr>';
   echo '<td width="200">';
   echo '<font size="-3" color="#643366" face="Helvetica, Geneva, Arial, SunSans-Regular, sans-serif"><a href="'.$spkname.'">'.$spkname.'</a></font>';
   echo '</td>';
   echo '<td width="200">';
   echo '<font size="-3" color="#643366" face="Helvetica, Geneva, Arial, SunSans-Regular, sans-serif"><I> ('.$spkcat.')</I>';
   echo '</td>';
   echo '</tr>';
}

echo '</table>';

I want to output the name and the category for each returned row using these loaded variables - right now my echo is displaying a '0' character for some reason and not the data for some reason!!!

Thanks a lot!
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 100 total points
ID: 20322245
you might consider doing like this:


while ($row = mysql_fetch_assoc($result)) {
      $spkname = $row["spkName"];
      $spkcat  = $row["spkCategory"];

0
 

Author Comment

by:adr2205
ID: 20322252
Dont worry Ive just sorted it! I just altered the query to read

select * from $dbname.speakers where spkKeyword1 like '$searchwords'

THanks for all your help tonight!
0
 
LVL 17

Expert Comment

by:nplib
ID: 20322266
try this
$query = "select spkname, spkcat from $dbname.speakers where spkKeyword1 like $searchwords";
mysql_select_db($dbanme, $link);
$result = mysql_query($query, $link);
 
echo '<table border="0">';
 
while ($row = mysql_fetch_array($result)) {

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
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 look for a specific file type in a local or remote server directory using PHP.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

749 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