doushitang4276 2014-02-19 21:12
浏览 13

PHP函数无法连接到DB [关闭]

I have this piece of code:

<?php
require('includes/db.php');

function connectHouseObjects() {

        /* Register a prepared statement */
        if ($stmt = $mysqli->prepare('SELECT * FROM house_room1 WHERE user_id = ?')) {

            /* Bind parametres */
            $stmt->bind_param('i', $user_id);

                /* Insert the parameter values */
                $user_id = 1;

                /* Execute the query */
                $stmt->execute();

                /* Close statement */
                $stmt->close();

            } else {
                /* Something went wrong */
                echo 'Something went terrible wrong'     . $mysqli->error;
            }

        }
?>

And I use this in a seperate document to call it:

require('main_database.php');
connectHouseObjects();

Soo I get an error on this line: if ($stmt = $mysqli->prepare('SELECT * FROM house_room1 WHERE user_id = ?')) {

saying: Fatal error: Call to a member function prepare() on a non-object in .../x/main_database.php on line 7.

Any ideas? Anything is appreciated. Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dqayok7935 2014-02-19 21:17
    关注

    Basic PHP syntax:

    function connectHouseObjects() {
    
            /* Register a prepared statement */
            if ($stmt = $mysqli->etc....
                        ^^^^^^---undefined in your function
    

    You need to do

       global $mysqli;
    

    within your function before you actually use that connection handle.

    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料