dongyi1748 2015-10-27 14:59
浏览 15

面对的PHP PDO常见错误

I gotten this error

Database Connected successfully Fatal error: Call to a member function prepare() on a non-object in D:\home\site\wwwroot\DatabaseMethods.php on line 22 

I can't seem to find what is causing this any one could point out whats wrong please enlighten me.

I have tried to call the connection before running the prepared statement but cant seems to work or what should i do

Thanks in advance

DatabaseMethods.php

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

    require_once("DatabaseConnection.php");

    date_default_timezone_set("Asia/Singapore");

    //Register

    function RegisterUser($email , $password)
    {

    $conn = connect_db();

    $stmt = null;

    $stmt = $conn->prepare("INSERT INTO secure_login (email,password,created_dt) VALUES(?,?,?)"); //ERROR LINE

    $stmt->execute(array($email, $password , date("Y-m-d h:i:s")));


    if( $stmt ) 
        {
            return "success";
        }
    else
        {
            return "Failed";
        }
    }

    //date("Y-m-d h:i:s")

    //End Register
    ?>

DatabaseConnection.php

<?php

    function connect_db()
    {
    $servername = "xxxx";
    $username = "xxx";
    $password = "xxx";

    try {
        $conn = new PDO("mysql:host=$servername;dbname=xxx", $username, $password);
        // set the PDO error mode to exception
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        echo "Database Connected successfully"; 
        }
    catch(PDOException $e)
        {
        echo "Connection failed: " . $e->getMessage();
        }
    }

    ?>

Answer

<?php

function connect_db()
{
$servername = "xxx";
$username = "xxx";
$password = "xxx";

try {
    $conn = new PDO("mysql:host=$servername;dbname=xxx", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Database Connected successfully"; 

    return $conn;

    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }
}

?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
    • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
    • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
    • ¥15 lingo18勾选global solver求解使用的算法
    • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
    • ¥20 测距传感器数据手册i2c
    • ¥15 RPA正常跑,cmd输入cookies跑不出来
    • ¥15 求帮我调试一下freefem代码
    • ¥15 matlab代码解决,怎么运行
    • ¥15 R语言Rstudio突然无法启动