Solved

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

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

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 142

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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

24 Experts available now in Live!

Get 1:1 Help Now