Solved

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

Posted on 2007-11-20
11
3,033 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 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
please try this:

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

Expert Comment

by:Beverley Portlock
Comment Utility
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
Comment Utility
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
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:adr2205
Comment Utility
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
Comment Utility
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
Comment Utility
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 142

Accepted Solution

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


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

0
 

Author Comment

by:adr2205
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
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…
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…

772 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

12 Experts available now in Live!

Get 1:1 Help Now