Solved

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

Posted on 2007-11-20
11
3,046 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
[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
  • 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
Technology Partners: 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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

696 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