【问题标题】:Why is my mysql_query returning no result? [closed]为什么我的 mysql_query 没有返回结果? [关闭]
【发布时间】:2014-06-29 13:17:33
【问题描述】:

编码出现非法字符串偏移

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();
    } 

?>

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

【问题讨论】:

  • 您正在混合使用不同的数据库连接器:mysql_...mysqli_... 是两个完全不同的东西。你不能混合它们,你必须决定使用哪一个。由于旧的mysql_... 连接器不久前已被弃用,您应该选择更新、更安全、更灵活的mysqli_... 连接器。您还想了解“准备好的语句”在防止 sql 注入漏洞方面的优势。
  • 你的密码真的是password,用户名真的是username,dbname真的是dbname吗?如果你的答案是肯定的,那么你是我这辈子见过的最粗心的程序员。
  • table 是保留字。如果您真的想将其用作表名,则需要将其包装在反引号中。您最好使用不同的表名。
  • @MikeW 我使用实际的变量。
  • 你有一个括号 ) 太多 try {// Setting the query and runnin it... $result = mysqli_query($connection,"SELECT * FROM table"); if(mysqli_num_rows($result)&gt;0) ){ 更改为 try {// Setting the query and runnin it... $result = mysqli_query($connection,"SELECT * FROM table"); if(mysqli_num_rows($result)&gt;0) { @TheOkayMan

标签: php mysql sql foreach


【解决方案1】:

您将 mysqli 与 PDO 异常 try/catch 混合在一起,而且您的 try 块中还有一个额外的括号。这两个 API 不能混合使用。

查看代码中的 cmets:(以及下面的固定代码)

旁注:您可能需要将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
  • @EliasVanOotegem 谢谢 Elias,非常感谢,干杯。
【解决方案2】:

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

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

而不是 mysql_fetch_array($result)

使用 mysqli_fetch_array($result)

【讨论】:

  • 我得到与 DhruvPathak 的答案相同的错误。
  • 把 foreach() 放在这个 if 条件 if(mysqli_num_rows($result)>0){ foreach(mysql_fetch_array($result) as $row){......}}
  • 非法字符串偏移我已经更新了编码
【解决方案3】:

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

【讨论】:

  • 使用'i'我得到的错误是Illegal string offset for line echo $row['Name']. ' - '. $row['Club']. ' - '. $row['Level']. ' - '. $row['App']. ' - '. $row['Score'].'&lt;br /&gt;';
  • 检查列的名称。 mysqli_fetch_array() 构建具有数字和文本索引的混合数组。仅使用 mysqli_fetch_assoc() 获取关联数组,这样您的数组就不会翻倍。您的列可能不一样,因此您的索引不存在。
【解决方案4】:

试试这个

<?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 />';

     }


    ?>

【讨论】:

  • Call to a member function execute() on a non-object in``$result-&gt;execute();
  • 你的表名是什么
  • 表名是score
  • 您是在查询中放置任何 where 条件还是按原样使用?
  • 把握当下;除非它能使编码工作。
【解决方案5】:

很可能,您的连接有问题。由于 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;
}
?>

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2012-11-04
  • 2019-07-29
  • 2011-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-08
  • 2019-03-08
相关资源
最近更新 更多