为什么这会将数组显示为字符串转换?

时间:2013-08-20 20:45:58

标签: php html mysql

我在以下代码中遇到第30行错误的“数组到字符串转换”错误,但我找不到原因。

<?php 

$find = $_GET['input'];
if($find == " ")
{
    echo "<img src='dontmess,bro.png'><br>DON'T MESS<br>";
}

$findo = explode(" ",$_GET['input']);
$find = array_map('strtoupper', $findo);

$con = mysqli_connect("localhost", "%%%%", "****", "sites");

$field = "keywords";
foreach($find as $value)
{
    $value = mysqli_query($con, "SELECT * FROM webinfo WHERE upper($field) LIKE '%$find%'"); 
    $data = $value;
    while($results = mysqli_fetch_assoc( $data )) 
    { 

        echo "<a href='" . $results['link'] . "'>" . $results['title'] . "</a>"; 
        echo "<br> "; 
        echo $results['description']; 
        echo "<br>"; 
        echo "<br>"; 
        echo "<br>"; 
    }


    $anymatches=mysqli_num_rows($data); 
    if ($anymatches == 0) 
    { 
        echo "Sorry, no results. WHY!!!!<br><br>"; 
    } else 
    {
        echo $anymatches; 
    }

?>

1 个答案:

答案 0 :(得分:5)

你的foreach循环可能有点怀疑:

foreach($find as $value)
{
    $value = mysqli_query($con, 
        "SELECT * FROM webinfo WHERE upper($field) LIKE '%$find%'"); 
    $data = $value;

您在查询中使用$find作为参数。好像你打算使用$value,但是$value会被覆盖,丢弃你从循环中获得的原始值。

以下内容可能有所帮助:

foreach($find as $value)
{
     $db_query = mysqli_query($con, 
         "SELECT * FROM webinfo WHERE upper($field) LIKE '%$value%'"); 

     while($results = mysqli_fetch_assoc( $db_query )) 
     { 
        ... etc