PDA

View Full Version : [SOLVED] PHP Search function



Kyluke
July 28th, 2011, 08:10 PM
Hey I have a website with a mysql database.

Now I need to have a search function built in but for some reason my code only ever produces either the first line (no matter what) or the whole table.

Here is my code and i want it to return only one row with the correct data


<?php
mysql_connect ("localhost", "chaos_theory","12345") or die (mysql_error());
mysql_select_db ("students");

$term = $_GET['student_number'];

$sql = mysql_query("select * from students where student_number like '%$term%'");

while ($row = mysql_fetch_array($sql)){
echo 'ID: '.$row['id_number'];
echo '<br/> First Name: '.$row['student_number'];
echo '<br/> Last Name: '.$row['first_name'];
echo '<br/> Phone: '.$row['surname'];
echo '<br/><br/>';
}

?>

hooflung64
July 28th, 2011, 08:59 PM
Try:



<?php
mysql_connect ("localhost", "chaos_theory","12345") or die (mysql_error());
mysql_select_db ("students");

$term = $_GET['student_number'];

$query = sprintf("SELECT * from students where student_number like '%s'",
mysql_real_escape_string($term);

$result = mysql_query($query);

while($row = mysql_fetch_assoc($result))
echo 'ID: '.$row['id_number'];
echo '<br/> First Name: '.$row['student_number'];
echo '<br/> Last Name: '.$row['first_name'];
echo '<br/> Phone: '.$row['surname'];
echo '<br/><br/>';
}

?>


If that doesn't provide any different of outcome then you need to revisit your query. Possibly run it on the cli client for mysql until you get the desired results.

Kyluke
July 29th, 2011, 03:26 PM
<?php
mysql_connect ("localhost", "chaos_theory","12345") or die (mysql_error());
mysql_select_db ("students");

$term = $_GET['student_number'];

$sql = mysql_query("select * from students where student_number like '%$term%'");

while ($row = mysql_fetch_array($sql)){
echo 'ID: '.$row['id_number'];
echo '<br/> First Name: '.$row['student_number'];
echo '<br/> Last Name: '.$row['first_name'];
echo '<br/> Phone: '.$row['surname'];
echo '<br/><br/>';
}

?>

This doesn't work, the site does not even open, I will try to figure out why. The sql queries run fine in cli

Kyluke
July 29th, 2011, 04:34 PM
Got it working