【问题标题】:PHP array_push() fails with text from SQLPHP array_push() 因来自 SQL 的文本而失败
【发布时间】:2018-01-25 21:40:07
【问题描述】:

我将以下 PHP 脚本连接到失败的 MySQL 数据库:

error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

// array for JSON response
$response = array();

// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();


// get a author from authors table
$result = mysql_query("SELECT authorid, name, biography FROM author WHERE authorid = 3");

if (!empty($result)) {
    // check for empty result
    if (mysql_num_rows($result) > 0) {
        // success
        $response["success"] = 1;

        $result = mysql_fetch_array($result);

        $author = array();
        $author["authorid"] = $result["authorid"];
        $author["name"] = $result["name"];
        $author["biography"] = $result["biography"];

        // user node
        $response["author"] = array();

        array_push($response["author"], $author);

        // echoing JSON response
        echo json_encode($response);
    } else {
        // no author found
        $response["success"] = 0;
        $response["message"] = "No author found";

        // echo no users JSON
        echo json_encode($response);
    }
} else {
    // no author found
    $response["success"] = 0;
    $response["message"] = "No author found";

    // echo no users JSON
    echo json_encode($response);
}

程序得到结果,但停在行 array_push($response["author"], $author); (我设法使用 echo 得到它,但程序实际上似乎没有报告任何错误)。如果我从查询中删除传记字段(这是一个 TEXT 字段)或删除行 $author["biography"] = $result["biography"];程序恢复工作。因此我认为这可能是由错误/空传记返回引起的,但我不明白为什么它应该返回,以便查询在数据库中运行时显示正确的结果并且传记不为空。

这是从查询中删除传记字段时返回的响应:

{"success":1,"author":[{"authorid":"3","name":"Jane Austen","biography":null}]}

您认为这个问题是如何产生的,我该如何解决?

谢谢

编辑:根据 Ben Harris 的建议进行了更改:

$result = mysql_fetch_array($result);

$response["author"] = [
            "authorid"  => $result["authorid"],
            "name"      => $result["name"],
            "biography" => $result["biography"]
];

var_dump($response);

// echoing JSON response
echo json_encode($response);

Var_dump 打印正确

array(2) { 
    ["success"]=> int(1) 
    ["author"]=> array(3) { 
                ["authorid"]=> string(1) "3" 
                ["name"]=> string(11) "Jane Austen" 
                ["biography"]=> string(41) "Jane Austen (1775 � 1817) was an English " 
                } 
    }

但是它仍然没有回应任何东西。知道为什么吗?

【问题讨论】:

  • 如果你var_dump($response),你会得到什么?
  • @LioraHaydont 'array(2) { ["success"]=> int(1) ["author"]=> array(3) { ["authorid"]=> string(1) "3" ["name"]=> string(11) "Jane Austen" ["biography"]=> string(41) "Jane Austen (1775 � 1817) 是英语 " } }' 正确但仍然不是呼应任何东西
  • UTF-8 字符可能会导致您出现问题
  • More info 在 UTF-8 上。如果您不使用它,请确保您在所有地方都使用相同的编码。
  • 是的,问题是数据库没有使用 utf8 排序规则。非常感谢@RiggsFolly 和 BenHarris!

标签: php mysql sql arrays


【解决方案1】:

您不需要创建中间数组,甚至不需要使用array_push。试试这个,如果没有看到预期结果,请使用print_rvar_dump 进行调试。

$response["author"] = [
    "authorid"  => $result["authorid"],
    "name"      => $result["name"],
    "biography" => $result["biography"]
];

【讨论】:

  • 您可以将其进一步简化为$response["author"] = $result 尽管这也不能回答问题
【解决方案2】:

问题是数据库没有使用 utf8 排序规则。感谢@RiggsFolly 和@BenHarris 在 cmets 中的建议! 非常感谢。

【讨论】:

    猜你喜欢
    • 2013-04-22
    • 1970-01-01
    • 1970-01-01
    • 2015-11-12
    • 2010-10-20
    • 1970-01-01
    • 2023-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多