simple sql search script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ferric
Forum Newbie
Posts: 16
Joined: Wed Mar 17, 2004 3:16 pm

simple sql search script

Post by ferric »

I cannot figure out why this isn't working- can someone shed some light please?


<?

$db = mysql_connect('localhost', $username, $password);
@mysql_select_db($database) or die("Unable to select database");

$sql = "SELECT name
FROM characters
WHERE name LIKE '$keys'
";

$sql_result = mysql_query($sql,$db) or die ("Couldn't execute SQL query");

if ($row = mysql_fetch_array($sql_result)) {
$id = $row["id"];
$charname = $row["name"];

echo $charname;

} else {
echo "not found in the database!";
}

mysql_free_result($sql_result);
mysql_close($db);
?>
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Try ...
$sql = "SELECT name FROM characters WHERE name LIKE '%$keys%'";

Though you'll have to define "isn't working" for a better answer if that doesn't solve it ;)
User avatar
werlop
Forum Commoner
Posts: 68
Joined: Sat Mar 22, 2003 2:50 am
Location: /dev/null

Post by werlop »

markl999 wrote:Try ...
$sql = "SELECT name FROM characters WHERE name LIKE '%$keys%'";

Though you'll have to define "isn't working" for a better answer if that doesn't solve it ;)
Yeah, are you getting an error, or just not the results that you expected.

Also, could you post using the php tags in this forum in the future, here is your code syntax highlighted:

Code: Select all

<?php

$db = mysql_connect('localhost', $username, $password); 
@mysql_select_db($database) or die("Unable to select database"); 

$sql = "SELECT name 
FROM characters 
WHERE name LIKE '$keys' 
"; 

$sql_result = mysql_query($sql,$db) or die ("Couldn't execute SQL query"); 

if ($row = mysql_fetch_array($sql_result)) { 
$id = $row["id"]; 
$charname = $row["name"]; 

echo $charname; 

} else { 
echo "not found in the database!"; 
} 

mysql_free_result($sql_result); 
mysql_close($db);  


?>
ferric
Forum Newbie
Posts: 16
Joined: Wed Mar 17, 2004 3:16 pm

Post by ferric »

thank-you, that fixed it. What do the %'s do?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

The &'s are mysql's wildcards, sorta like *foo*.
ferric
Forum Newbie
Posts: 16
Joined: Wed Mar 17, 2004 3:16 pm

Post by ferric »

Oh- good, thanks. Although it works, it will only pull the first result it finds out of the database and stops.. how would I make it continue searching even after it's found something?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Code: Select all

<?php

$db = mysql_connect('localhost', $username, $password) or die(mysql_error());
mysql_select_db($database) or die('Unable to select database');

$sql = "SELECT name FROM characters WHERE name LIKE '%$keys%'";

$sql_result = mysql_query($sql,$db) or die ("Couldn't execute SQL query");

if(mysql_num_rows($sql_result)){
  while ($row = mysql_fetch_array($sql_result)) {
    $id = $row["id"];
    $charname = $row["name"];
    echo $charname;
  }
} else {
  echo 'not found in the database!';
}

mysql_free_result($sql_result);
mysql_close($db);


?>
ferric
Forum Newbie
Posts: 16
Joined: Wed Mar 17, 2004 3:16 pm

Post by ferric »

thanks!
Post Reply