php - 使用通配符在多个表 php 中搜索

标签 php mysql

请帮助我,我想进行搜索查询,但表列表未在 php 中显示,但它在 mysql 中工作。这是我的代码

//$sql = "SELECT * FROM table_name WHERE 1=1" work perfectly
$sql = "SELECT a.* FROM (SELECT table_name FROM information_schema.tables WHERE table_schema like 'rft%') a where 1=1";

if( !empty($requestData['search']['value']) ) 
{   // if there is a search parameter, $requestData['search']['value'] contains search parameter

//search based on input 
$sql.=" AND ( Code LIKE '".$requestData['search']['value']."%' ";    
$sql.=" OR Year LIKE '".$requestData['search']['value']."%' )";
}
 //start search after input 
 $query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");

 $totalFiltered = mysqli_num_rows($query); 

在这里继续

 // when there is a search parameter then we have to modify total number filtered rows as per search result. 
 $sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]." ".$requestData['order'][0]['dir']."  LIMIT ".$requestData['start']." ,".$requestData['length']."   ";

 /* $requestData['order'][0]['column'] contains colmun index, $requestData['order'][0]['dir'] contains order such as asc/desc  */   
 $query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");

$data = array();
while( $row=mysqli_fetch_array($query) ) 
{  // preparing an array
$nestedData=array(); 

$nestedData[] = $row["Type"];
$nestedData[] = $row["Code"];
$nestedData[] = $row["Year"];

$data[] = $nestedData;
}

最佳答案

您只需从 information_schema.tables 中选择 table_name,然后执行 WHERE code=...。您的选择结果中没有 Code,因此这不起作用。

如果我正确地理解了您想要做的事情,您必须分两步进行:

// get all table names:
$tquery = mysqli_query($conn, "SELECT table_name FROM information_schema.tables WHERE table_schema like 'rft%'");
while ($table = mysqli_fetch_array($tquery)) {
    // do your query per table
    $sql  = "SELECT * FROM " . $table['table_name'] . " WHERE ( Code LIKE '".$requestData['search']['value']."%' ";
    $sql .=" OR Year LIKE '".$requestData['search']['value']."%' )";
    $query = mysqli_query($conn, $sql);    
    while ( $row = mysqli_fetch_array($query) ) {
        // do what you want with $row
    }
}

关于php - 使用通配符在多个表 php 中搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36053437/

相关文章:

mysql - MYSQL有什么独特之处?我可以放置 unique 以防止重复输入吗?

php - 使用 Laravel Auth 伪造登录

php - WordPress 按距离和特色帖子查询排序

mysql - 获取两个时间戳之间的范围

MySQL - 使用触发器复制表

php - MySQL SQL 获取表中的最新记录?

php - mysql在查询时间戳间隔时出错

php - 更改具有相同 ID joomla 的模块标题之一的 ID

处理奇怪字符时的php wordwrap cut参数

mysql - 检查数据库中任何不同表中是否存在记录