...

View Full Version : searching mysql DB using PHP



steviecee
06-27-2005, 12:01 PM
I'm using php to search a mysql db. The code is as follows:
<?php
require_once("C:\Server\Apache2\mysql_connect_york.inc");

$typ=$_POST['type'];
$code=$_POST['pcode'];
$cit=$_POST['city'];
$count=$_POST['county'];
$ncit=$_POST['ncity'];


$query_result=mysql_query( 'select location_id, owner_id, loc_name, type, address_1, address_2, city, county, post_code from location where type = $typ');

while($row=mysql_fetch_array($query_result))
{
echo "<tr>";
echo "<td>.$row['location_id'].</td>"; echo "<td>.$row['owner_id'].</td>";
echo "<td>.$row['loc_name'].</td>";
echo "<td>.$row['type'].</td>";
echo "<td>.$row['address_1'].</td>";
echo "<td>.$row['address_2'].</td>";
echo "<td>.$row['city'].</td>";
echo "<td>.$row['county'].</td>";
echo "<td>.$row['post_code'].</td>";
echo "</tr>";


mysql_free_result ($query_result);
mysql_close();


?>

the code is falling down at the highlighted echo with he error:
Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in C:\Server\Apache2\local-html\search_york.php on line 230

any ideas on the problem? obviously it some sort of data mismatch , but how can i put it right?
Cheers
Steve

missing-score
06-27-2005, 12:43 PM
Try either:



echo "<td>".$row['location_id']."</td>";
// With extra quotes
// or...
echo "<td>{$row['location_id']}</td>";
// putting the variable in curly brackets


you will need to change all of your <td> elements to look like this...
eg:

echo "<td>".$row['owner_id']."</td>";

and so on...

steviecee
06-27-2005, 01:18 PM
Hi, thanks for the reply. I'd already tried the 1st way, but that didn't wrk, it just bought up all the echos onto the screen. the second way seems to work, although now i get the same error on a line which contains
</hmtl>!! what do you think?
cheers
steve

missing-score
06-27-2005, 01:29 PM
i think you may have forgotten to close your while loop too... try this:



<?php
require_once("C:\Server\Apache2\mysql_connect_york.inc");

$typ=$_POST['type'];
$code=$_POST['pcode'];
$cit=$_POST['city'];
$count=$_POST['county'];
$ncit=$_POST['ncity'];


$query_result=mysql_query( 'select location_id, owner_id, loc_name, type, address_1, address_2, city, county, post_code from location where type = $typ');

while($row=mysql_fetch_array($query_result))
{
echo "<tr>";
echo "<td>".$row['location_id']."</td>";
echo "<td>".$row['owner_id']."</td>";
echo "<td>".$row['loc_name']."</td>";
echo "<td>".$row['type']."</td>";
echo "<td>".$row['address_1']."</td>";
echo "<td>".$row['address_2']."</td>";
echo "<td>".$row['city']."</td>";
echo "<td>".$row['county']."</td>";
echo "<td>".$row['post_code']."</td>";
echo "</tr>";
}

mysql_free_result ($query_result);
mysql_close();

?>


also, if the $_POST['type'] ($typ) variable is a string and not a number, you should put it in quotes in the query, eg:



$query_result = mysql_query("select location_id, owner_id, loc_name, type, address_1, address_2, city, county, post_code from location where type = '".$typ."'");


Edit:
You could use mysql_fetch_assoc() rather than mysql_fetch_array(). mysql_fetch_assoc() only returns associative array values (eg: $row['name']) whereas fetch_array returns associative ($row['name']) and indexed ($row[0]). mysql_fetch_assoc() is more efficient if you are only using associative array values. You can replace the function name 'mysql_fetch_array' with 'mysql_fetch_assoc' in the code above and it will still work the same, just be slightly faster.

steviecee
06-27-2005, 01:35 PM
first thing, not closing the loop...doh!! got it working perfectly now, thanks a lot for your time mate
steve

missing-score
06-27-2005, 01:43 PM
cool. be sure to try out the mysql_fetch_assoc() method i mentioned.

steviecee
06-27-2005, 01:50 PM
got it in there now
cheers



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum