Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP MYSQLI Prepared语句返回NULL_Php_Mysqli - Fatal编程技术网

PHP MYSQLI Prepared语句返回NULL

PHP MYSQLI Prepared语句返回NULL,php,mysqli,Php,Mysqli,我有一个在phpmyadmin中工作的查询,但是在我的代码中不工作!我尝试了各种变量转储,以查看在执行查询之前是否丢失了数据,所有这些看起来都正常,与我在phpmyadmin中成功查询中使用的变量内容相同 要测试我,请替换: $account_id = $account->getAccountId(); //output below string(2) "59" string(4) "main" NULL NULL array(2) { ["id"]=> NULL["name"]=&

我有一个在phpmyadmin中工作的查询,但是在我的代码中不工作!我尝试了各种变量转储,以查看在执行查询之前是否丢失了数据,所有这些看起来都正常,与我在phpmyadmin中成功查询中使用的变量内容相同

要测试我,请替换:

$account_id = $account->getAccountId(); //output below
string(2) "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

但结果仍然是空的

因此,我在本文中尝试了以下示例: 当我转储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;
        }
我哪里做错了

希望有人能帮忙


谢谢

您需要在$stmt->execute之后声明$stmt->bind\u结果
请参阅:

您需要在$stmt->execute之后声明$stmt->bind\u结果

请参阅:

我遇到了相同的问题,原因是在使用prepare语句时关闭了连接。确保流中没有关闭连接的情况

我也遇到了同样的问题,原因是在使用语句prepare时关闭了连接。确保流中没有关闭连接的情况

当您说prepared语句返回NULL时是什么意思?$mysqli->prepare是否返回NULL?或者在$mysqli->execute之后$id和$imageName是否为NULL?执行之后,绑定参数为NULL,它们应该是$id=int和$imageName=string。您是否验证了查询是否返回结果?我想你可能对你的查询有一些问题。对两个表进行内部联接,但从不指定任何联接条件。您是否打算将“添加\配置文件\图像”中的每条记录与“用户\配置文件\图像”中的每条记录进行匹配?如果那是你想要的,那么这样做没什么错,我只是想确定你真的想要这些结果。是的,图像id Pk在用户配置文件图像中被引用为FK。用户配置文件图像有一个带有帐户id和图像id的复合键。这是一个坏主意吗?定义外键不会导致你的联接自动匹配这些字段-这是为了确保数据安全诚实正直如果要匹配表中该外键关系上的行,则需要指定它。类似这样的内容:从add\u profile\u images使用image\u id加入用户\u profile\u images当您说prepared语句返回NULL时是什么意思?$mysqli->prepare是否返回NULL?或者在$mysqli->execute之后$id和$imageName是否为NULL?执行之后,绑定参数为NULL,它们应该是$id=int和$imageName=string。您是否验证了查询是否返回结果?我想你可能对你的查询有一些问题。对两个表进行内部联接,但从不指定任何联接条件。您是否打算将“添加\配置文件\图像”中的每条记录与“用户\配置文件\图像”中的每条记录进行匹配?如果那是你想要的,那么这样做没什么错,我只是想确定你真的想要这些结果。是的,图像id Pk在用户配置文件图像中被引用为FK。用户配置文件图像有一个带有帐户id和图像id的复合键。这是一个坏主意吗?定义外键不会导致你的联接自动匹配这些字段-这是为了确保数据安全诚实正直如果要匹配表中该外键关系上的行,则需要指定它。类似这样的内容:从添加配置文件图像使用图像id加入用户配置文件图像。此外,Alan需要在调用bind_result后获取数据$stmt->bind_result指定将数据放在何处,但您仍然需要调用$stmt->fetch,直到它返回null。请参阅链接页面上的示例1。感谢您的建议Jam6549,在测试错误时,我将bind_result移到execute下,result仍然为空。感谢您在$stmt->fetch{$result['id']=$id;$result['name]=$imageName;}@Jam6549时添加了此链接!很好的发现。此外,Alan需要在调用bind_result后获取数据$stmt->bind_result指定将数据放在何处,但您仍然需要调用$stmt->fetch,直到它返回null。请参阅链接页面上的示例1。感谢您的建议Jam6549,在测试错误时,我将bind_result移到execute下,result仍然为空。感谢您在$stmt->fetch{$result['id']=$id;$result['name]=$imageName;}@Jam6549时添加了此链接!
add_profile_images.image_name //in the query
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;
        }