Page 2 of 2

Posted: Thu May 13, 2004 6:54 am
by launchcode
Yes but what's the Query? Post the SQL man :)

Posted: Thu May 13, 2004 4:39 pm
by ]{ronic
Here is the whole script:

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&#1111;'go'] == "True") &#123;
//Open database server connection
$db_connect = mysql_connect($db_server, $user, $pass, $db);
if (! $db_connect)&#123;
Echo "Couldn't connect";
exit;
&#125;

//Open database
if (! mysql_select_db($db_private, $db_connect))&#123;
echo "Error selecting database to use " . mysql_error();
exit;
&#125;

//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&#1111;'description']; 
if (strlen($desc) > 0) &#123; 
$db_query .= " WHERE description LIKE '%$desc%'";

&#125; 

//Run our query
$result1 = mysql_query($db_query, $db_connect);
if (! $result1)&#123;
echo mysql_error();
exit;
&#125;

//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)) &#123;
echo "<tr><td>$row&#1111;description]</td><td>$row&#1111;price]</td></tr>\r\n";
&#125; // while

echo "</table>\r\n\r\n";
echo "</body></html>";
//exit out
exit;
&#125;
?>
<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

Posted: Thu May 13, 2004 5:51 pm
by launchcode
Just add the following line after:

$result1 = mysql_query($db_query, $db_connect);

Code: Select all

echo $db_query;
!! :)

Posted: Thu May 13, 2004 6:04 pm
by ]{ronic
I placed the $db_query here:

Code: Select all

$result1 = mysql_query($db_query, $db_connect);
echo $db_query;
if (! $result1)&#123;
echo mysql_error();
exit;
&#125;

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

Posted: Thu May 13, 2004 6:21 pm
by launchcode
Yes - spot the SQL syntax error! (hint: it's the 2nd WHERE part - you can only have 1 in SQL).

I suggest you comment out the part of your code that is adding that 2nd where query for now and take it from there.

Posted: Thu May 13, 2004 10:34 pm
by ]{ronic
arrg .. I have tried lots of scenarios but no luck :(

My lack of knowladge I guess ..

If I comment out:

Code: Select all

$desc = $_POST&#1111;'description']; 
if (strlen($desc) > 0) &#123; 
//$db_query .= " WHERE description LIKE '%$desc%'";
I get the following error:
SELECT C_Memory.*, C_VGA.* FROM C_Memory,C_VGA WHERE C_Memory.description LIKE '%whatever%' OR C_VGA.description LIKE '%whatever%'
Altho it does still echo "<table cellpadding=\"2\" border=\"1\"><tr><td colspan=5>Search Results</td>\r\n"; as well ...

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

Posted: Fri May 14, 2004 5:54 am
by launchcode
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.