在从数据库搜索时我需要一些帮助。我曾使用嵌套循环在不同的表和列中进行搜索。
<?php
$mysqli = new mysqli("localhost", "root", "", "mobirise");
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
if(!empty ($_POST['stext'])){
$query = $_POST['stext'];
$min_length = 3;
if(strlen($query) >= $min_length){
$sql_r= $mysqli->query("show tables from mobirise;");
while($table = mysqli_fetch_array($sql_r)) {
$res=$table[0];
//print"$res"."<br>";
$sql_i= $mysqli->query("show columns from ".$res.";");
while($tables = mysqli_fetch_array($sql_i)) {
$reslt=$tables[0];
//print" ". " ". " "."$reslt"."<br>";
$raw_results = $mysqli->query("SELECT * FROM $res WHERE $reslt LIKE '%".$query."%'");
print_r($raw_results);?><br><?php
}
}
}
}
?>
那样的打印结果
{
mysqli_result Object (
[current_field] => 0
[field_count] => 8
[lengths] =>
[num_rows] => 0
[type] => 0
)
}
但是我想要表数据。
答案 0 :(得分:0)
我找到了答案。 我只是忘记了mysqli_fetch_array语句。 更新的代码>
<?php
$mysqli = new mysqli("localhost", "root", "", "mobirise");
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
if(!empty ($_POST['stext'])){
$query = $_POST['stext'];
$min_length = 3;
if(strlen($query) >= $min_length){
$sql_r= $mysqli->query("show tables from mobirise;");
while($table = mysqli_fetch_array($sql_r)) {
$res=$table[0];
$sql_i= $mysqli->query("show columns from ".$res.";");
while($tables = mysqli_fetch_array($sql_i)) {
$reslt=$tables[0];
$raw_results = $mysqli->query("SELECT * FROM $res WHERE $reslt LIKE '%".$query."%'");
print_r(mysqli_fetch_array($raw_results));
}
}
}
}
?>
现在得到我的精确输出。