I am trying to Query a MySQL table to to bring any result that matches data that the user has input. The database,table and column names are also dynamically stored in variables. var_dump produces a bool(false) which means my query is wrong.
My Code
if (isset ( $_POST ['name'] )) {
$name = trim ( $_POST ['name'] );
$tblName = $_REQUEST ['tbl'];
$colqry = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '$dbName' AND TABLE_NAME = '$tblName'";
echo "<ul>";
$col_names = mysqli_query ( $link, $colqry );
while ( $col = mysqli_fetch_array ( $col_names, MYSQL_ASSOC ) ) {
$colName = $col ['COLUMN_NAME'];
$tblQry = "SELECT * FROM $tblName WHERE $colName=$name LIMIT 10";
$query2 = mysqli_query ($link, $tblQry);
echo $query2;
while ( $query3 = mysqli_fetch_array ( $query2 ) ) {
echo "<li onclick=fill'" . $query3 [0] . "'>" . $query3 [0] . "</li>";
}
}
}
What I want to achieve is list a table where the search terms matches something on the table either the column name or the data inside the columns
<?php error_reporting(E_ALL); ini_set('display_errors', 1);then the rest of your code, to see if it yields anything, as well asor die(mysqli_error($link))tomysqli_query(). You'll see errors in your query.WHERE '$colName'='%$name%'you're looking to useLIKEwhen using%signs. I.e.:WHERE '$colName' LIKE '%$name%'. Otherwise, doWHERE '$colName'='$name'for an exact match.LIKEit will list all the data in that column, but I want to specific data in the column not the actual column name.SELECT * FROM '$tblName' WHERE '$colName'='%$name%'