I have a question in relation to a MySQL table. Currently I am using the code below, in order to display a complete SQL table to a user
<?php;
try {
$con= new PDO('mysql:host=localhost;dbname=members1', "mcxjb", "password");
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = "SELECT * FROM users";
//first pass just gets the column names
print "<table>
";
$result = $con->query($query);
//return only the first row (we only need field names)
$row = $result->fetch(PDO::FETCH_ASSOC);
print " <tr>
";
foreach ($row as $field => $value){
print " <th>$field</th>
";
} // end foreach
print " </tr>
";
//second query gets the data
$data = $con->query($query);
$data->setFetchMode(PDO::FETCH_ASSOC);
foreach($data as $row){
print " <tr>
";
foreach ($row as $name=>$value){
print " <td>$value</td>
";
} // end field loop
print " </tr>
";
} // end record loop
print "</table>
";
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
} // end try
}
?>
And it displays the required information perfectly! However, I was wondering is there a way to implement a simple HTML form that allows the user to enter characters and it searches through the table, or perhaps narrows it down to the different items with them terms? I know it can be done with JavaScript but was wondering if it could be done with PHP.
Cheers, Mark
You can try Datatables, it's a js library, I recommend it :)
And using PHP, just add something like: $query = "SELECT * FROM users WHERE yourfield='".$_GET['search']."'";