Link to home
Start Free TrialLog in
Avatar of Dhanpal N
Dhanpal N

asked on

How to get only the search result?

<?php
//Step1
 $db = mysqli_connect('localhost','username','','db_name')
 or die('Error connecting to MySQL server.');
?>

<html>
 <head>
 </head>
 <body>
 <h1>PHP connect to MySQL</h1>
 
<?php
//Step2
$query = "SELECT * FROM imabangalore";
mysqli_query($db, $query) or die('Error querying database.');
$result = mysqli_query($db, $query);
$row = mysqli_fetch_array($result);

while ($row = mysqli_fetch_array($result)) {
 echo $row['Name'] . ' ' . $row['IMA Memb No'] . ' ' . $row['Spouse Name'] . ' ' . '<br />';
}
mysqli_close($db);
?>

</body>

</html>

Open in new window


Results shows all the data, but I want only the searched Name and their details
Avatar of Leonidas Dosas
Leonidas Dosas
Flag of Greece image

At $query = "SELECT * FROM imabangalore"; add what exaclty want to search ie
$query = "SELECT * FROM imabangalore WHERE 'Name'=some search name you want";
Results shows all the data, but I want only the searched Name and their details
Search based on ...?

If you want to filter your query you do it like this

$query = "SELECT * FROM imabangalore WHERE `Name`='{$searchvalue}'";

Open in new window


Where searchvalue comes from depends on your application - it might be sent to the page as a POST or a GET

www.yourdomain.com/yourpage.php?Name=John

Open in new window


In which case you could add this line to the start of your code to get the value from the URL
$searchvalue = isset($_GET['Name']) ? $_GET['Name'] : '';

Open in new window

This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.