无法从MySQL获取数据

时间:2018-08-27 14:44:18

标签: php mysql

我已连接到 phpmyadmin ,但无法从phpmyadmin获取任何数据。 我的 php 版本是 7.2.9 ,我在数据库中做了我想要的所有内容,但是php无法显示站点中的数据(我将m使用localhost)。 这是代码:

    <?php
    $key = $_GET['key'];

    $terms = explode(" ", $key);
    $query = "SELECT * FORM search WHERE ";

    foreach ($terms as $each){
        $i++;
        if($i == 1){
            $query .= "keywords LIKE '%$each%' ";
        } else{
            $query .= "OR keywords LIKE '%$each%' ";
        }
        echo $query;
    }

    //connection
mysql_connect("localhost", "root", "");
mysql_select_db('search');
    $query = mysqli_query($query);
    $numrows = mysqli_num_rows($query);



    if($numrows > 0){
        while ($row = mysql_fetch_assoc($query)){
        $id = $row['id'];
        $title = $row['title'];
        $description = $row['description'];
        $keywords = $row['keywords'];
        $link = $row['link'];
        echo "<h2><a href='$link'>$title</h2></a>
        $description<br /><br />";
        }
        }
        else{ 
        echo "No results found for \"<b>$key</b>\""; }

    //disconnect
    mysql_close();

?>

4 个答案:

答案 0 :(得分:1)

您的PHP / HTML中有几个错误。我要在这里总结一下,以便您看看它们:

  • <h2><a href='$link'>$title</h2></a>$description<br /><br />这是错误的HTML。关闭a内的h2标签。

  • 您正在通过mysql连接到数据库,但正在通过mysqli查询。使用mysqli连接到数据库。 Mysql_系列功能已在PHP 7中删除

  • 您的查询中有错字。您已经写了FORM而不是FROM

  • 您正在将$_GET变量分解为空格。但是我怀疑$_GET变量是否以任何空格开头...检查是否为真。

答案 1 :(得分:0)

首先,在php 5之后,不再提供mysql_connect()。请不要使用mysqli_connect()来使用mysql。请在这里使用php7建立连接和查询数据库。 https://www.w3schools.com/php/func_mysqli_fetch_row.asp

如果仍然有问题。在评论中寻求帮助。

   <?php

    $query = "SELECT * FORM search WHERE ";

    foreach ($terms as $each){
        $i++;
        if($i == 1){
            $query .= "keywords LIKE '%$each%' ";
        } else{
            $query .= "OR keywords LIKE '%$each%' ";
        }
        echo $query;
    }

    //connection
    $conn = mysqli_connect("localhost", "root", "","search");
    if(!$conn)die("Connection Error");
    $query = mysqli_query($conn,$query);
    if(!query)die("query error");
    $numrows = mysqli_num_rows($query);
    if($numrows > 0){
        while ($row = mysqli_fetch_assoc($query)){
        $id = $row['id'];
        $title = $row['title'];
        $description = $row['description'];
        $keywords = $row['keywords'];
        $link = $row['link'];
        echo "<h2><a href='$link'>$title</h2></a>
        $description<br /><br />";
        }
        }
        else{ 
        echo "No results found for \"<b>$key</b>\""; 
        }

答案 2 :(得分:0)

您正在混合mysqli和mysql。我已经编辑了你的东西。请试试。不要忘记修复标有[missing]

的部分
      <?php
       $key = $_GET['key'];

       $terms = explode(" ", $key);


      foreach ($terms as $each){
        $i++;
        if($i == 1){  
           $query .= "keywords LIKE '%$each%' ";
       } else{
          $query .= " description OR keywords LIKE '%$each%' ";
       }
         echo $query;
        }

      //connection
        $conn=mysqli_connect("localhost", "root", "");
        mysqli_select_db($conn, 'search');
       if($result= mysqli_query($conn, $query)){
          $numrows = mysqli_num_rows($result);
       }




        if($numrows > 0){

            while($row = mysqli_fetch_array($result)){
                $id = $row['id'];
                $title = $row['title'];
                $description = $row['description'];
                $keywords = $row['keywords'];
                $link = $row['link'];    
                //echo "<h2><a href='$link'>$title</h2></a>
                //$description<br /><br />";
                echo '<h2><a href="' . $link . '">' . $title . 
                '</h2></a>' .  $description . '<br /><br />';
              }
            }
             else{ 
              echo "No results found for \"<b>$key</b>\""; }

           //disconnect
            mysqli_close();

           ?>

答案 3 :(得分:0)

这是最后一个完整的工作示例(昨晚我不在工作机器上,无法测试代码。后来我创建了一个小数据库并对其进行了测试。我用虚拟键'ram搜索了)玛丽·阿尔伯特”在我的示例中

    <?php
        $key = $_GET['key'];

        $terms = explode(" ", $key);

        $qu1 = "SELECT * FROM search WHERE ";
        $qu2 = "order  by id ASC";



        $conn=mysqli_connect("localhost", "root", "");

        mysqli_select_db($conn, 'search');

        for($i=0; $i< count($terms);  $i++){

          $query = $qu1 . " keywords LIKE '%$terms[$i]%'  "  .  $qu2;

          echo( $query . "<br>" );

          $resulter= mysqli_query($conn, $query);

          while($row = mysqli_fetch_array($resulter)){;

                $id = $row['id'];
                $title = $row['title'];
                $description = $row['description'];
                $keywords = $row['keywords'];
                $link = $row['link'];    

                $EscLink='\'' . $link . '\'';

                echo ('<a href="javascript:void(0)" onClick="alert(' . 
                $EscLink . ')">' . $title .  '</a><br>' .  $description . 
                '<br /><br />');


           } // Close While
         } // Close for



         //disconnect
         mysqli_close($conn);

         ?>

See the SCREENSHOT of the result