mysql syntax error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jamal
Forum Commoner
Posts: 57
Joined: Sat Oct 26, 2002 7:53 pm
Location: London

mysql syntax error

Post by jamal »

Hi can anyone tell me why i am having an error in this code.
The error is mysql_fetch_array(): supplied argument is not a valid MySQL result resource.
Thanks



<html>
<body>





Code: Select all

mysql_connect ("localhost","myusername","mypass");
   
mysql_select_db ("aboutDB");

if ($first_name == "")
{$first_name = '%';}

if ($last_name == "")
{$last_name = '%';}

$result = mysql_query ("SELECT * FROM abouttbl
                         WHERE first_name LIKE '$first_name%'
                         AND last_name LIKE '$last_name%'
                       ");

if ($row = mysql_fetch_array($result)) {

do {
  print $row&#1111;"first_name"];
  print (" ");
  print $row&#1111;"last_name"];
  print ("&lt;p&gt;");
} while($row = mysql_fetch_array($result));

} else {print "Sorry, no records were found!";}

</body>
</html>
I have tried this also but it did not work.

Code: Select all

if (mysql_numrows($result) &gt; 0) {

do {
  print $row&#1111;"first_name"];
  print (" ");
  print $row&#1111;"last_name"];
  print ("&lt;p&gt;");
} while($row = mysql_fetch_array($result));

} else {print "Sorry, no records were found!";}
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

there must be a problem with your query, like one of the variables does not contain a value. try this and show us what you see:

Code: Select all

$quesry = "SELECT * FROM abouttbl
                         WHERE first_name LIKE '$first_name%'
                         AND last_name LIKE '$last_name%'"
$result = mysql_query ($query); 
print $quesry;
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

or

Code: Select all

$query = "SELECT * FROM abouttbl
                         WHERE first_name LIKE '$first_name%'
                         AND last_name LIKE '$last_name%'"
$result = mysql_query($query) or die($query.' :'.mysql_error()); 
print $query;
to see mysql's problem with the query
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

mysql_numrows

Post by phpScott »

your have a lttle syntax error.
In your second attempt your use mysql_numrows to see if there is a result.
Try using mysql_num_rows instead.

phpScott
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Hi can anyone tell me why i am having an error in this code.
It would have made it easier for everybody to help you if you actually said what the error was.

Mac
Post Reply