Rares Coco Rares Coco - 1 year ago 119
MySQL Question

PHP - Search Function for MySQL Database Doesn't Work

I need to retrieve something from database but the search does not work. When I input in search bar the folloowing:

, nothing happens, it does not return any results. Could someone point to me where the problem lies?

nume | prenume | id
stano bobo 1
alex bobo 2

Here is the code:

// conectare la baza de date
$db = mysqli_connect("localhost", "root", "", "inregistrare");
$output = '';
if (isset($_POST['cauta'])) {
$cauta1 = $_POST['cauta'];
$cauta1 = preg_replace("#[^0-9a-z]#i","",$cauta1);

$query = mysqli_query($db, "SELECT * FROM users WHERE nume LIKE '%$cauta1%' OR prenume LIKE '%$cauta1%'");
$count = mysqli_num_rows($db, $query);
if ($count == 0) {
$output = 'Nu a fost gasit elevul';
} else {
while ($row = mysqli_fetch_array($query)) {
$nume = $row['nume'];
$prenume = $row['prenume'];
$id = $row['id'];

$output .= '<div>'.$nume.' '.$prenume.'</div>';

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Cauta Elev</title>

<form method="post">
<input type="text" name="cauta" placeholder="Cauta Elevul"/>
<input type="submit" value="Cauta"/>
<?php print("$output"); ?>

Answer Source

You missed $ for nume and prenume vars in the $output. The correct one is:

$output .= '<div>'.$nume.' '.$prenume.'</div>';
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download