gpt4 book ai didi

php - 将带有另一个搜索查询的变量添加到 MySQL SELECT 语句中

转载 作者:行者123 更新时间:2023-11-29 07:49:31 24 4
gpt4 key购买 nike

@OhGodWhy:再次感谢您的帮助。我现在向您展示我的代码的具体样子,也许您知道可能出了什么问题:

在这里,在第一部分中,我替换了我的 <a href>回答的第一部分(两行)。请参阅下面的代码:

//-query the database table
$sql="SELECT * FROM Hashtags";

//-run the query against the mysql query function
$result=mysql_query($sql);

//-create while loop and loop through result set
while($row=mysql_fetch_array($result)){

//-display the result of the array
$query_string = 'hashtag=true&tag='.urlencode($row['Hashtag']);
echo '<a href="index.php?'.htmlentities($query_string).'" title="Suche nach '.$row['Hashtag'].'">#'.$row['Hashtag'].'</a>';

然后,我在主题标签功能开始后添加了您答案的第二个 block 。我将 if 语句包裹在整个函数中,直到 while 部分结束。见下文:

function hashtags() {

$tag = isset($_GET['tag'])? urldecode($_GET['tag']) : false ;
if($tag) {

$mysqli = new mysqli('host', 'user', 'pass', 'db');
$stmt = $mysqli->prepare("select * from table where name like CONCAT('%', ?, '%')");
$stmt->bind_param('s', $tag);
$stmt->execute();

//-run the query against the mysql query function
$result=mysql_query($sql);

//-create while loop and loop through result set
while($row=mysql_fetch_array($result)){

//-display the result of the array
echo '...'
//end of while & if

while循环是否也必须调整为mysqli?也许这些信息可以提供帮助:在我的浏览器中,URL 看起来正确:“index.php?hashtag=true&tag=...”

当我点击<a href>时我得到一个空屏幕。

再次感谢您的帮助,抱歉打扰您!

最佳答案

您应该只提供哈希标签作为 urlencoded 字符串,其中包含 $row['hashtag']

$query_string = 'hashtag=true&tag='.urlencocde($row['hashtag']);
echo '<a href="index.php?'.htmlentities($query_string).'" title="Suche nach '.$row['Hashtag'].'">#'.$row['Hashtag'].'</a>';

然后在您的函数主题标签中,您可以像这样获取标签值:

$tag = isset($_GET['tag'])? urldecode($_GET['tag']) : false ;
if($tag):

此外,您需要 move away from mysql并保护自己免遭 SQL 注入(inject)。我们可以通过迁移到 mysqli 库并使用 prepared 语句来完成这一切。

$mysqli = new mysqli('host', 'user', 'pass', 'db');
$stmt = $mysqli->prepare("select * from table where name like CONCAT('%', ?, '%')");
$stmt->bind_param('s', $tag);
$stmt->execute();

while($row = $stmt->fetch_assoc()){
//echo stuff
}

您需要concatLIKE,否则您会收到错误。

资源

  1. Ternary Operators
  2. MySQLI prepared statements
  3. MySQLI bind param
  4. urlencode
  5. urldecode
  6. htmlentities

关于php - 将带有另一个搜索查询的变量添加到 MySQL SELECT 语句中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26850973/

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