search mysql database for records
Moderator: General Moderators
- launchcode
- Forum Contributor
- Posts: 401
- Joined: Tue May 11, 2004 7:32 pm
- Location: UK
- Contact:
Here is the whole script:
Im not exactly sure on how to echo out the $db_query sorry ...
Thanks heaps for your help & patients
]{ronic
Code: Select all
<?php
$db_server = "#####";
$user = "#####";
$pass = "#####";
$db_private = "#####";
//They are searching...do that for them (we know this from the hidden "go" field in the table)
if ($_POSTї'go'] == "True") {
//Open database server connection
$db_connect = mysql_connect($db_server, $user, $pass, $db);
if (! $db_connect){
Echo "Couldn't connect";
exit;
}
//Open database
if (! mysql_select_db($db_private, $db_connect)){
echo "Error selecting database to use " . mysql_error();
exit;
}
//Build the SQL statement based on POST options
//The basic SQL will select all records
$db_query = "SELECT C_Memory.*, C_VGA.* FROM C_Memory,C_VGA WHERE C_Memory.description LIKE '%whatever%' OR C_VGA.description LIKE '%whatever%'";
//depending on which of the following are filled in (none, some, all)
//the resultset returned will be filtered down
$desc = $_POSTї'description'];
if (strlen($desc) > 0) {
$db_query .= " WHERE description LIKE '%$desc%'";
}
//Run our query
$result1 = mysql_query($db_query, $db_connect);
if (! $result1){
echo mysql_error();
exit;
}
//loop through all the results and display them
echo "<table cellpadding="2" border="1"><tr><td colspan=5>Search Results</td>\r\n";
while($row = mysql_fetch_array($result1)) {
echo "<tr><td>$rowїdescription]</td><td>$rowїprice]</td></tr>\r\n";
} // while
echo "</table>\r\n\r\n";
echo "</body></html>";
//exit out
exit;
}
?>
<table width="500">
<tr>
<td width="500">Search Products </td>
</tr>
</table>
<table>
<form name="mysearch" action="search.php" method="post">
<tr>
<td>Product Description</td><td><input type="text" size="20" value="" name="description"></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="hidden" name="go" value="True">
<input type="submit" value="Search">
</td>
</tr>
</table>
</form>Im not exactly sure on how to echo out the $db_query sorry ...
Thanks heaps for your help & patients
]{ronic
- launchcode
- Forum Contributor
- Posts: 401
- Joined: Tue May 11, 2004 7:32 pm
- Location: UK
- Contact:
Just add the following line after:
$result1 = mysql_query($db_query, $db_connect);
!! 
$result1 = mysql_query($db_query, $db_connect);
Code: Select all
echo $db_query;I placed the $db_query here:
and got this result when I executed the script:
Is that what you were after?
Cheers
Code: Select all
$result1 = mysql_query($db_query, $db_connect);
echo $db_query;
if (! $result1){
echo mysql_error();
exit;
}and got this result when I executed the script:
SELECT C_Memory.*, C_VGA.* FROM C_Memory,C_VGA WHERE C_Memory.description LIKE '%whatever%' OR C_VGA.description LIKE '%whatever%' WHERE description LIKE '%samsung%'You have an error in your SQL syntax near 'WHERE description LIKE '%samsung%'' at line 1
Is that what you were after?
Cheers
- launchcode
- Forum Contributor
- Posts: 401
- Joined: Tue May 11, 2004 7:32 pm
- Location: UK
- Contact:
arrg .. I have tried lots of scenarios but no luck 
My lack of knowladge I guess ..
If I comment out:
I get the following error:
Also I want to add price and link so I can have that information displayed with the results but not as searchable fields .. I had it sorted before linking tables ...
Sorry for the lamness .. Im have no idea what is causing the error
]{ronic
My lack of knowladge I guess ..
If I comment out:
Code: Select all
$desc = $_POSTї'description'];
if (strlen($desc) > 0) {
//$db_query .= " WHERE description LIKE '%$desc%'";Altho it does still echo "<table cellpadding=\"2\" border=\"1\"><tr><td colspan=5>Search Results</td>\r\n"; as well ...SELECT C_Memory.*, C_VGA.* FROM C_Memory,C_VGA WHERE C_Memory.description LIKE '%whatever%' OR C_VGA.description LIKE '%whatever%'
Also I want to add price and link so I can have that information displayed with the results but not as searchable fields .. I had it sorted before linking tables ...
Sorry for the lamness .. Im have no idea what is causing the error
]{ronic
- launchcode
- Forum Contributor
- Posts: 401
- Joined: Tue May 11, 2004 7:32 pm
- Location: UK
- Contact:
The SQL you posted above is perfectly valid - I mean sure, it will only ever search for "whatever" because you haven't put your variable ($desc) in there, but it's still valid and will not error.
Grab me on MSN/ICQ if you want, it'll be quicker to show you how to do this via your actual code than here.
Grab me on MSN/ICQ if you want, it'll be quicker to show you how to do this via your actual code than here.