mysql_query()期望参数2是资源

时间:2015-11-29 12:00:26

标签: php mysql

这是我的mysql连接,我将返回连接---

public function mysqlConnection() {

    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "contactinfo";

    // Create connection
    $conn = mysql_connect($servername, $username, $password);

    if (!$conn) {
        die('Could not connect: ' . mysql_error());
    }
        // returning the mysql connection
        return $conn;
    }

现在当我试图调用我的mysql连接时,它总是返回null !!

public function create($name = null, $email = null, $msg = null) {

    if ($_SERVER["REQUEST_METHOD"] == 'POST') {

        $name = $_POST['name'] ;
        $email = $_POST['email'] ;
        $msg = $_POST['message'] ;

        $sql = "INSERT INTO contactinfo (name, email, message)
        VALUES ('$name', '$email', '$msg')";

        $conn = $this->mysqlConnection(); // null Why !!!

        $retval = mysql_query($sql, $conn);

        if (!$retval) {
            die('Could not insert data ' . mysql_error());
        }
    }
    return $this->view();
}
  

mysql_query()期望参数2是资源,在 $ retval = mysql_query($ sql,$ conn)中给出null;

我做错了什么!! 任何人都有一个想法,请...

0 个答案:

没有答案