Jump to content

Simple Search


seandarcy

Recommended Posts

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.

Link to comment
Share on other sites

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>"; 

 

?>

 

 

Link to comment
Share on other sites

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>"; 

 

?>

 

Link to comment
Share on other sites

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?

 

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.