gpt4 book ai didi

php - Mysql 搜索所有列连同 WHERE 和 LIKE 子句

转载 作者:行者123 更新时间:2023-11-30 22:59:27 25 4
gpt4 key购买 nike

我有一个数据库,表中有四列 id,title,address,phone。当使用选定的表进行搜索时,查询将搜索该特定行中的术语并获取结果,但我想知道的是,如果没有选择表格,即 -

   <option value="">Select table</option>

我希望查询在每个表中搜索术语,我可以使用 WHERE row1 LIKE %$keyword% OR row2 LIKE %$keyword% 来实现,但我希望它搜索这个东西在一个查询中,就好像它选择所有行数据并用术语搜索它一样,首先它搜索 row 1 然后 row2 row3 一个一个接一个自己做。有什么办法吗

   <html>
<head>
</head>
<body>
<?php
if(isset($_POST['submit']))
{
$keyword= $_POST['keyword'];
$table= $_POST['table'];

mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("dbname") or die(mysql_error());

$data = mysql_query("SELECT * FROM information WHERE $table LIKE '%".$keyword."%'");

//And we display the results
while($result = mysql_fetch_array( $data ))
{
echo $result['id'];
echo $result['title'];
}
}
?>
<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='POST'>
<input type='text' size='90' name='keyword'>
<select name='table'>
<option value="">Select table</option>
<option value="title">title</option>
<option value="address">address</option>
<option value="phone">phone</option>
</select>
<input type='submit' name='submit' value='submit' >
</form>
</body>
</html>

最佳答案

看起来您所拥有的应该可以工作,但您还需要连接 where 子句。试试这个。

$data = mysql_query("SELECT * FROM information WHERE `{$table}` LIKE '%".$keyword."%'");

关于php - Mysql 搜索所有列连同 WHERE 和 LIKE 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24727246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com