I need help with MySQL strings

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
firephoenix
Forum Newbie
Posts: 5
Joined: Sat Sep 29, 2007 5:20 pm

I need help with MySQL strings

Post 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]
Last edited by firephoenix on Sun Sep 30, 2007 12:01 am, edited 1 time in total.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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));
}
firephoenix
Forum Newbie
Posts: 5
Joined: Sat Sep 29, 2007 5:20 pm

thanks but it was a no-go

Post 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'
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

please try

Code: Select all

$query="SELECT
		*
	FROM
		$tbl_listings
	WHERE
		`make`='chevy'";
jeffery
Forum Contributor
Posts: 105
Joined: Mon Apr 03, 2006 3:13 am
Location: Melbourne, Australia
Contact:

Post 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. :?
firephoenix
Forum Newbie
Posts: 5
Joined: Sat Sep 29, 2007 5:20 pm

Code

Post 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
jeffery
Forum Contributor
Posts: 105
Joined: Mon Apr 03, 2006 3:13 am
Location: Melbourne, Australia
Contact:

Post by jeffery »

ah I get it. Is "make" a reserved word ?
firephoenix
Forum Newbie
Posts: 5
Joined: Sat Sep 29, 2007 5:20 pm

Post by firephoenix »

Um yeah I am unsure but I know the code works lol
Post Reply