Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2007-11-20
11
Medium Priority
?
3,050 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 100 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 400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

596 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