New to PHP

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
passthebuck
Forum Newbie
Posts: 1
Joined: Mon Jan 21, 2008 11:55 am

New to PHP

Post by passthebuck »

Ok first if I have posted in wrong section sorry.... Hi new to php and looking for some general help with code I am writing for a project I have to do and I have ran into some problems with the code and have been getting errors.. and well dont know were to turn so looking for any help to point out the fault in my code. Here is the code I have written....errors are happening on line 24 and if I change the the code I then get error on line 33 at the echo any help please.

Code: Select all

 
<?php
 
 
    
$dbconnection = mysql_connect("","","");
 
if (!$dbconnection)
{
    die('Unable to connect … ' . mysql_error());
}
 
mysql_select_db("car_dealer", $dbconnection);
 
 
/*//This is the original code*/
/*//$query = mysql_query("SELECT * FROM cars where make = 'ford'");
$query = mysql_query("SELECT * FROM cars where make LIKE '".$_POST['frmmake']."' AND price <=".$_POST['frmprice']." AND enginesize >= ".$_POST['frmenginesize']." AND transmision ='".$_POST[frmtransmission]."' AND fuel ='". $_POST['frmfuel']."'"); */
 
 
 
 
//$query = mysql_query("SELECT * FROM cars where make = 'ford'");
$query = mysql_query("SELECT * FROM cars where make LIKE '".$_POST['frmmake']."' AND price <=".$_POST['frmprice']."'AND engine_capacity >=".$_POST['frmengine_capacity'].AND "transmission ='".$_POST[frmtransmission]."'AND fuel ='".$_POST['frmfuel']."'");
 
//$query = mysql_query("SELECT * FROM cars");
echo $query;
 
 
while($row = mysql_fetch_array($query))
{
 
 //echo $row['make']. " " .$row['model'] . "£" . $row['price'];
  echo "<br />";
 
}
 
mysql_close($dbconnection);
 
?>
 
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: New to PHP

Post by califdon »

Are you passing $_POST parameters to this script?
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: New to PHP

Post by markusn00b »

Would also help if we knew what the errors were ;)
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: New to PHP

Post by Jonah Bron »

I don't think you can just echo $query...
Ollie
Forum Newbie
Posts: 4
Joined: Sun Jan 20, 2008 5:46 pm
Location: Nottingham, UK

Re: New to PHP

Post by Ollie »

Instead of line 30, you'll need:

Code: Select all

...
$result = mysql_query($query);
 
while($row = mysql_fetch_array($result))
{
... 
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Re: New to PHP

Post by anjanesh »

passthebuck - $query is assigned to a resource returned by mysql_query - its not the SQL statement itself.
Are you sure your SQL statement is valid ? Change line 24 to and see if any error occurs.

Code: Select all

$sql = "SELECT * FROM cars where make LIKE '".$_POST['frmmake']."' AND price <=".$_POST['frmprice']."'AND engine_capacity >=".$_POST['frmengine_capacity'].AND "transmission ='".$_POST[frmtransmission]."'AND fuel ='".$_POST['frmfuel']."'";
$query = mysql_query($sql) or die(mysql_error()."<hr>".$sql);
tili
Forum Newbie
Posts: 3
Joined: Wed Jan 23, 2008 2:27 am

Re: New to PHP

Post by tili »

try this

$query = mysql_query("SELECT * FROM cars where make LIKE '".$_POST['frmmake']."' AND price <= '".$_POST['frmprice']."' AND engine_capacity >='".$_POST['frmengine_capacity']."'AND transmission ='".$_POST[frmtransmission]."'AND fuel ='".$_POST['frmfuel']."'");
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Re: New to PHP

Post by Chalks »

incidentally, it would be a good idea to make sure that the $_POST variables contain what you expect. Also, using mysql_real_escape_string() before you put data into an sql query is good.
Post Reply