homepage Welcome to WebmasterWorld Guest from 23.20.19.131
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / WebmasterWorld / New To Web Development
Forum Library, Charter, Moderators: brotherhood of lan & mack

New To Web Development Forum

    
SQl query search form
form doesn't work
gajendrasaini 06




msg:4026498
 1:02 pm on Nov 17, 2009 (gmt 0)

Hello ,

i am trying to create a search php page. i had make a search page like this But when i search Nothing will happen on the page only search box will be empty.

please correct it.

<?php
if (!$conn = mysql_connect('localhost', 'gks', '123456'))
die("Error: Cannot Establish Connection to Database");

if (!mysql_select_db('search'))
die("Error: Database doesn't exist");

?>

<form method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">

<label>Enter First Name : <input type="text" name="firstname" /></label>
<input type="submit" value="Search" />
</form>
<br /><br />

<?php

if (isset($_get['name']) && $_get['name'] != NULL)

{
$result = mysql_query("SELECT * from user WHERE name LIKE " . $_get['firstname']);
if ($result)
{
echo "<table><tr><th>ID</th><th>First Name</th><th>Id</th></tr>";

while ($row = mysql_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "</tr>";

}

echo "</table>";
}
else
{
echo "Error: Something wrong with query";
}
}

mysql_close($conn);
?>

 

rocknbil




msg:4027787
 4:32 am on Nov 19, 2009 (gmt 0)

Looks like you're coming along gajendrasaini_06, sorry I missed this . . . here is most likely your problem:

$result = mysql_query("SELECT * from user WHERE name LIKE " . $_get['firstname']);

Queries on numeric fields do not need to be quoted, text fields do. try

$query = "SELECT * from user WHERE name LIKE '" . $_get['firstname'] . "'";

$result = mysql_query($query);

Next, the like operator is usually used in conjunction with "wild cards", %, which mean " anything before"

.... LIKE '%" . $_get['firstname'] . "'";

or anything after

LIKE '" . $_get['firstname'] . "%'";

so your query would be fine with just = instead of like.

Last, you should always error check your queries:

$result = mysql_query($query) or die("cannot execute query");

This would have potentially revealed your problem.

To get at the specific mysql error,

$result = mysql_query($query) or die("cannot execute query " . mysql_error());

But be sure to remove the mysql_error from any public scripts, it will reveal info about your database.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / WebmasterWorld / New To Web Development
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved