【问题标题】:PHP MYSQLI Prepared Statement returns NULLPHP MYSQLI Prepared Statement 返回 NULL
【发布时间】:2012-03-14 14:18:43
【问题描述】:

我有一个在 phpmyadmin 中有效的查询,但在我的代码中无效!我尝试了各种变量转储,看看我是否在执行查询之前丢失了数据,一切似乎都很好,我在 phpmyadmin 中成功查询时使用的变量内容相同

为了测试我替换了:

$account_id = $account->getAccountId(); //output below
string(2) "59" string(4) "main" NULL NULL array(2) { ["id"]=> NULL["name"]=>NULL}

有

$account_id = 59; //output below
int(59) string(4) "main" NULL NULL array(2) { ["id"]=> NULL ["name"]=> NULL }

以下是代码摘录,我使用的是 mysqli:

        $account = $Add_Profile_Image->getUserAccount();
        $account_id = $account->getAccountId();
        $status = $account->getType();
        var_dump($account_id);
        var_dump($status);
        $conn = $this->create_connection('read');
        $stmt = $conn->prepare('SELECT add_profile_images.image_id, image_name FROM add_profile_images, users_profile_images WHERE users_profile_images.account_id=? AND users_profile_images.status=?');
        $stmt->bind_param('is',$account_id,$status);
        $stmt->bind_result($id,$imageName);
        $stmt->execute();
        var_dump($id);
        var_dump($imageName);
        $result['id'] = $id;
        $result['name'] = $imageName;

我换了

image_name //in the query

到

add_profile_images.image_name //in the query

但结果仍然是NULL?

所以我在这篇文章中尝试了以下示例:PHP Prepared Statement Returns -1 当我转储 mysqli 对象时,它确实返回 -1 但是当我实现以下没有错误时 显示出来了!

if($conn->connect_error) {
            printf('connect error (%d) %s', $conn->connect_errno, htmlspecialchars($conn->connect_error));
                        die;
        }
        $stmt = $conn->prepare('SELECT add_profile_images.image_id, add_profile_images.image_name FROM add_profile_images, users_profile_images WHERE users_profile_images.account_id=? AND users_profile_images.status=?');
        if ( false===$stmt ) {
            printf('prepare failed: %s', htmlspecialchars($conn->error));
            die;
        }

        $rc = $stmt->bind_param('is',$account_id,$status);
        if ( false===$rc ) {
            printf('bind_param failed: %s', htmlspecialchars($stmt->error));
        die;
        }

        $rc= $stmt->execute();
        if ( false===$rc ) {
                printf('execute failed: %s', htmlspecialchars($stmt->error));
                die;
        }
        $rc = $stmt->bind_result($id,$imageName);
        if ( false===$rc ) {
                printf('bind_result failed: %s', htmlspecialchars($stmt->error));
                die;
        }

我哪里错了?

希望有人能帮忙!

谢谢

【问题讨论】:

  • 你说准备好的语句返回NULL是什么意思? $mysqli->prepare() 是否返回 NULL?还是$id 和$imageName 在$mysqli->execute() 之后为NULL?
  • 执行后绑定参数为NULL的时候应该是$id=(int) and $imageName = (String)
  • 您是否验证过您的查询确实返回了结果?我认为无论如何您的查询可能存在一些问题。您对两个表执行INNER JOIN,但从不指定任何连接条件。您是否打算将add_profile_images 中的每条记录与users_profile_images 中的每条记录相匹配?如果这是您想要的,那么这样做并没有错,我只是确保您确实想要这些结果。
  • 是的,图像 id (Pk) 在 users_profile_images 中被称为 (FK),users_profile_images 有一个带有 account_id 和 image_id 的复合键。这是个坏主意吗?
  • 定义外键不会导致您的联接自动匹配这些字段 - 这是一种确保数据完整性的方法。如果您想根据该外键关系匹配表中的行,则需要指定它。像这样的东西:FROM add_profile_images JOIN users_profile_images USING( image_id )

标签: php mysqli


【解决方案1】:

你需要在 $stmt->execute() 之后声明 $stmt->bind_result() (见:http://php.net/manual/en/mysqli-stmt.bind-result.php)

【讨论】:

  • 不错的发现。另外,Alan 需要在调用bind_result() 后获取数据。 $stmt->bind_result() 指定放置数据的位置,但您仍然需要调用 $stmt->fetch() 直到它返回 null。请参阅链接页面上的示例 #1。
  • 感谢 Jam6549 的建议,在测试错误时,我将 bind_result 移到了执行下,结果仍然为 NULL
  • 谢谢我添加了 while ($stmt->fetch()) { $result['id'] = $id; $result['name'] = $imageName; @jam6549 和 Ryan P 感谢您的链接!
【解决方案2】:

我遇到了同样的问题,原因是使用语句准备时关闭连接。确保在您的流程中没有这样的连接关闭

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-24
    相关资源
    最近更新 更多