PHP 单引号和双引号 + 数组的问题

PHP trouble with single and double quotes + array

带有 href 的部分将不起作用

<?php
    $key=$_GET['key'];
    $array = array();
    $con = mysqli_connect("localhost", "root", "", "neukenet");
    $query=mysqli_query($con,"select * from blog_content where caption LIKE '%{$key}%'");
    while($row=mysqli_fetch_assoc($query))
    {
      $array[] = <a href="#">$row['caption']</a>;
    }
    echo json_encode($array);
?>

感谢您的努力!

您应该将锚标记存储在引号内并使用连接运算符。

while ($row = mysqli_fetch_assoc($query)) {
  $array[] = '<a href="#">' . $row['caption']. '</a>'; // Modify this line
}
echo json_encode($array);

您必须从 while 循环中回显变量 (concatenate),然后需要保存到 array() 变量中。如果没有 concatenate,您的数据将不会打印到其中。

You have not closed the PHP tag so that you need to use ' operator to concatenate based on the forward quotes that you have used or else your <a> tag will be throwing out error over to the solution. And your json_encode() will also replicate the same.

替换:

$array[] = <a href="#">$row['caption']</a>;

搭配:

$array[] = '<a href="#">'.$row['caption'].'</a>';

而不是所有其他代码看起来都很好,整个代码如下所示。

<?php
    $key=$_GET['key'];
    $array = array();
    $con = mysqli_connect("localhost", "root", "", "neukenet");
    $query=mysqli_query($con,"select * from blog_content where caption LIKE '%{$key}%'");
    while($row=mysqli_fetch_assoc($query))
    {
      $array[] = '<a href="#">'.$row['caption'].'</a>';
    }
    echo json_encode($array);
?>