Page 1 of 1

New to PHP

Posted: Mon Jan 21, 2008 12:16 pm
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);
 
?>
 

Re: New to PHP

Posted: Mon Jan 21, 2008 1:36 pm
by califdon
Are you passing $_POST parameters to this script?

Re: New to PHP

Posted: Mon Jan 21, 2008 1:37 pm
by markusn00b
Would also help if we knew what the errors were ;)

Re: New to PHP

Posted: Tue Jan 22, 2008 1:59 pm
by Jonah Bron
I don't think you can just echo $query...

Re: New to PHP

Posted: Tue Jan 22, 2008 6:37 pm
by Ollie
Instead of line 30, you'll need:

Code: Select all

...
$result = mysql_query($query);
 
while($row = mysql_fetch_array($result))
{
... 

Re: New to PHP

Posted: Tue Jan 22, 2008 9:34 pm
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);

Re: New to PHP

Posted: Wed Jan 23, 2008 2:52 am
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']."'");

Re: New to PHP

Posted: Wed Jan 23, 2008 9:59 am
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.