为什么我的mysql_query没有返回结果?

时间:2014-05-12 08:47:05

标签: php mysql sql foreach

编码与非法字符串偏移相关:

echo $row['rowa']. ' - '. $row['rowb']. ' - '. $row['rowc']. ' - '. $row['rowd']. ' - '. $row['rowe'].'<br />';

<?php

   $connection=mysqli_connect("localhost"/*hostname*/,
                              "username"/*username*/,
                              "password"/*password*/,
                              "dbname"/*database name*/);

try {
    // Setting the query and runnin it...
    $result = mysqli_query($connection,"SELECT * FROM table");
    if(mysqli_num_rows($result)>0)
){ 
            foreach(mysqli_fetch_array($result) as $row) {
                echo $row['rowa']. ' - '. $row['rowb']. ' - '. $row['rowc']. ' - '. $row['rowd']. ' - '. $row['rowe'].'<br />';
            }
        }else{
            echo "QUERY IS NOT OKAY";
        }  // Closing the connection.
        $connection = null;
    }catch(PDOException $e) {
        echo $e->getMessage();
    } 

?>

用户名,密码,数据库和表名称代码中使用的实际名称。

5 个答案:

答案 0 :(得分:3)

您将mysqli与PDO例外try/catch混合,并在try块中添加一个额外的括号。这两个API不会混用。

请参阅代码中的注释:(以及下面的固定代码)

旁注:您可能希望将foreach(mysqli_fetch_array($result) as $row)更改为while ($row = mysqli_fetch_assoc($result))

使用foreach(mysqli_fetch_array($result) as $row)将产生行的重复结果,并且只会产生每行的第一个字母。

try {
    // Setting the query and runnin it...
    $result = mysqli_query($connection,"SELECT * FROM table");
    if(mysqli_num_rows($result)>0)

    ){
//  ^-- one bracket too many

            foreach(mysqli_fetch_array($result) as $row) {
                echo $row['rowa']. ' - '. $row['rowb']. ' - '. $row['rowc']. ' - '. $row['rowd']. ' - '. $row['rowe'].'<br />';
            }
        }else{
            echo "QUERY IS NOT OKAY";
        }  // Closing the connection.
        $connection = null;
    }catch(PDOException $e) { // this should be catch (Exception $e)
        echo $e->getMessage();
    } 

将其更改为:

try {
    // Setting the query and runnin it...
    $result = mysqli_query($connection,"SELECT * FROM `table`");
    if(mysqli_num_rows($result)>0)
{ 
            foreach(mysqli_fetch_array($result) as $row) {
                echo $row['rowa']. ' - '. $row['rowb']. ' - '. $row['rowc']. ' - '. $row['rowd']. ' - '. $row['rowe'].'<br />';
            }
        }else{
            echo "QUERY IS NOT OKAY";
        }

}

catch (Exception $e)
{
    echo $e->getMessage();
}

在文件顶部添加错误报告

error_reporting(E_ALL);
ini_set('display_errors', 1);

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

<强>重写:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

$connection=mysqli_connect("localhost"/*hostname*/,
                          "username"/*username*/,
                          "password"/*password*/,
                          "dbname"/*database name*/);

    try {
        // Setting the query and runnin it...
        $result = mysqli_query($connection,"SELECT * FROM `table`");
        if(mysqli_num_rows($result)>0)
    { 

// use while instead of foreach
// while ($row = mysqli_fetch_assoc($result)){
                foreach(mysqli_fetch_array($result) as $row) {
                    echo $row['rowa']. ' - '. $row['rowb']. ' - '. $row['rowc']. ' - '. $row['rowd']. ' - '. $row['rowe'].'<br />';
                }
            }else{
                echo "QUERY IS NOT OKAY";
            }

    }

    catch (Exception $e)
    {
        echo $e->getMessage();
    }

答案 1 :(得分:1)

而不是            $result = mysql_query($connection,"SELECT * FROM table");

使用            $result = mysqli_query($connection,"SELECT * FROM table");

而不是            mysql_fetch_array($result)

使用            mysqli_fetch_array($result)

答案 2 :(得分:0)

我想您应该使用mysqli_querymysqli_connect而不是mysql_query

答案 3 :(得分:0)

试试这个

<?php
 $mysqli = new mysqli("localhost", "username", "password", "Database name");
  if (mysqli_connect_errno()) {
  printf("Connect failed: %s\n", mysqli_connect_error());
  exit();
}

  // Setting the query and runnin it...
  $result = $mysqli->prepare("SELECT * FROM table");

  /* execute query */
  $result->execute();
  $result = $mysqli->get_result();

     while ($row= $result->fetch_assoc()) 
     { 

            echo $row['rowa']. ' - '. $row['rowb']. ' - '. $row['rowc']. ' - '. $row['rowd']. ' - '. $row['rowe'].'<br />';

     }


    ?>

答案 4 :(得分:0)

最有可能的是,你的连接出了问题。由于mysqli *不会抛出异常,因此无法捕获它们。

这些函数返回有效对象或FALSE,0或出错时为null。所以你需要检查一下。

以下是代码,它应该告诉您与DB的通信有什么问题。

<?php

function ctest() {
    $connection=mysqli_connect("localhost"/*hostname*/,
                               "username"/*username*/,
                               "password"/*password*/,
                               "dbname"/*database name*/);

    if($connection) {
        $result = mysqli_query($connection,"SELECT * FROM `table`");
        if($result) {
            if(mysqli_num_rows($result)>0) {
                foreach(mysqli_fetch_array($result) as $row) {
                    echo $row['rowa']. ' - '. $row['rowb']. ' - '
                                          . $row['rowc']. ' - '
                                          . $row['rowd']. ' - '
                                          . $row['rowe'].'<br />';
                }
                return;
            }
            echo "0 rows";
            return;
        }
        echo "No result";
        return;
    }
    echo "No connection";
    $connection = null;
}
?>