seandarcy Posted June 28, 2007 Share Posted June 28, 2007 Hi Im trying to create a simlpe mysql database search. I have a database called "lever" and then a table called "faults" Inside the table i have the following titles: Incident School Person Summary I want a search field that uses the "incident" filed to display data only in that row. I would like it to be displayed on a html page. Is anyone able to help me with the code. Ive tried using some that have been suggested on the web but I cant seem to configure them correctly. Hope someone can help. Quote Link to comment Share on other sites More sharing options...
clanstyles Posted June 28, 2007 Share Posted June 28, 2007 some like.. SELECT * from `table` where incedent like $_POST['Searchresult'] And it should return. Quote Link to comment Share on other sites More sharing options...
seandarcy Posted June 29, 2007 Author Share Posted June 29, 2007 ive got this but it doenst work: $db_host = 'localhost'; $db_user = 'MYUSERNAME'; $db_pass = 'MYPASSWORD'; $db_name = 'lever'; $db_table = 'faults'; $conn = mysql_connect($db_host,$db_user,$db_pass); mysql_select_db($db_name,$conn); // ---- END CONFIGURATIONS ------------------------------------------// if ($incident == "") {$incident = '%';} $result = mysql_query ("SELECT * FROM $dbase_name WHERE incident LIKE '%$incident%' ",$conn); $row = mysql_fetch_array($result) or die(mysql_error()); do { PRINT "Our database contains the following records: <br><br>"; PRINT "<b>incident: </b> "; print $row["incident"]; print (" "); print ("<br>"); PRINT "<b>summary: </b> "; print $row["summary"]; print ("<p>"); print ("<p>"); } while($row = mysql_fetch_array($result)); print "</table>"; ?> Quote Link to comment Share on other sites More sharing options...
waz Posted June 29, 2007 Share Posted June 29, 2007 '%".$incident."%' change to that Quote Link to comment Share on other sites More sharing options...
seandarcy Posted June 29, 2007 Author Share Posted June 29, 2007 have i changed the right bit as i now get the following error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/darcynet/public_html/code/results.php on line 19 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE incident LIKE '%%%'' at line 2 <? $db_host = 'localhost'; $db_user = 'username'; $db_pass = 'password'; $db_name = 'lever'; $db_table = 'faults'; $conn = mysql_connect($db_host,$db_user,$db_pass); mysql_select_db($db_name,$conn); // ---- END CONFIGURATIONS ------------------------------------------// if ($incident == "") {$incident = '%';} $result = mysql_query ("SELECT * FROM $dbase_name WHERE incident LIKE '%".$incident."%' ",$conn); $row = mysql_fetch_array($result) or die(mysql_error()); do { PRINT "Our database contains the following records: "; PRINT "incident: "; print $row["incident"]; print (" "); print (" "); PRINT "summary: "; print $row["summary"]; print ("<p>"); print ("<p>"); } while($row = mysql_fetch_array($result)); print "</table>"; ?> Quote Link to comment Share on other sites More sharing options...
waz Posted June 29, 2007 Share Posted June 29, 2007 you are passing an invaliid argument caused by {$incident = '%';} that is not need as u have hardcoded the % values allready so set it as nothing {$incident = '';} again u need something for the query to work looks like ur not passing a value into $incident what is $incident surpose to be a value of? Quote Link to comment Share on other sites More sharing options...
joshgarrod Posted June 29, 2007 Share Posted June 29, 2007 I have a simple working search script you can have if you like? Quote Link to comment Share on other sites More sharing options...
waz Posted June 29, 2007 Share Posted June 29, 2007 as i have said ur issue is LIKE '%%%'' fact this is happening you should have this LIKE '%(whatever)%' try and hard code the value to search on and then u will see where your problems lies so set $incident ="something" and try it Quote Link to comment Share on other sites More sharing options...
seandarcy Posted June 29, 2007 Author Share Posted June 29, 2007 joshgarrod, yes please Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.