Page 1 of 1

I need help with MySQL strings

Posted: Sat Sep 29, 2007 5:28 pm
by firephoenix
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


okay so I am dealing with a mysql DB and I am making it for car(make, model, year, price and so on)  now here is part of the scrip I am using to filter by make....

Code: Select all

<?php
      mysql_connect($dbhost,$dbuser,$dbpass) or die("Unable to connect");
			@mysql_select_db($dbname) or die ("Unable to select database");
			$query="SELECT * FROM $tbl_listings WHERE make = 'chevy'";
			$result=mysql_query($query);
	
			$num=mysql_numrows($result);

      $i=0;
			while ($i < $num) {

			$id=mysql_result($result,$i,"id");
      $make=mysql_result($result,$i,"make");
			$model=mysql_result($result,$i,"model");
      $year=mysql_result($result,$i,"year");
      $price=mysql_result($result,$i,"price");
			
       

      ?>

//Display information code

      <?
	    $i++;
	    }
      mysql_close(mysql_connect($dbhost,$dbuser,$dbpass));
      ?>
Now I am sure of the spelling and that there is a field "make" I can do it like this

Code: Select all

$query="SELECT * FROM $tbl_listings";
or like this

Code: Select all

$query="SELECT * FROM $tbl_listings WHERE model = 'Civic'";
and it works fine but I cant filter by make it gives me this error

Code: Select all

Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /home/sitename/public_html/test/admin/admincp.php on line 64
Now I am new to PHP and I am still learning so can someone please help me figure this one out?
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:2. Use descriptive subjects when you start a new thread. Vague titles such as "Help!", "Why?" are misleading and keep you from receiving an answer to your question.

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Sat Sep 29, 2007 6:04 pm
by volka
try

Code: Select all

$query="SELECT * FROM $tbl_listings WHERE make = 'chevy'";
$result=mysql_query($query);
if ( false===$result ) {
	die(mysql_error().': '.htmlentities($query));
}

thanks but it was a no-go

Posted: Sat Sep 29, 2007 6:14 pm
by firephoenix
it gave me this error

Code: Select all

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'make = 'chevy'' at line 1: SELECT * FROM listings WHERE make = 'chevy'

Posted: Sat Sep 29, 2007 6:22 pm
by volka
please try

Code: Select all

$query="SELECT
		*
	FROM
		$tbl_listings
	WHERE
		`make`='chevy'";

Posted: Sat Sep 29, 2007 7:00 pm
by jeffery
volka, care to explain why the back-ticks would fix the SQL error? I didn't see anything wrong with the previous SQL statement you had. :?

Code

Posted: Sat Sep 29, 2007 9:41 pm
by firephoenix
Thank you for that code it worked great and I am not sure when and where to use the back-ticks but they seem to help with quite a few things but anyway thanks again

Posted: Sat Sep 29, 2007 9:47 pm
by jeffery
ah I get it. Is "make" a reserved word ?

Posted: Sat Sep 29, 2007 11:59 pm
by firephoenix
Um yeah I am unsure but I know the code works lol