奇怪的错误:不在任何其他页面上具有相同代码的页面上

时间:2014-07-22 04:51:29

标签: php mysql mysqli

我正在尝试从mysql转换为mysqli,我遇到了一些问题。我已经能够修复它们中的大多数,但有些我没有。这是其中之一。这是我见过的唯一文件。我还要测试一些,但到目前为止这是唯一的一个。我该如何解决这些错误?我找不到任何东西。请注意它与mysql一起正常工作。任何帮助表示赞赏。

有了错误报告,这就是我得到的。

Notice: Trying to get property of non-object in <path to file> on line 16

Fatal error: Call to a member function query() on a non-object in <path to file> on line 36

为便于查看,这是第16行

 if ($link->connect_errno > 0){

这是第36行

$admincheck = $link->query($adminchecksql);

这是文件

<head>
<title>User Processing</title>
    <body bgcolor="#9966FF">
    <link rel="icon" type="image/ico" href="path to favicon"/>
</head>
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
define('DB_NAME', 'DB Name');
define('DB_USER', 'DB USER');
define('DB_PASSWORD', 'DB Password');
define('DB_HOST', 'DB Host');


$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

if ($link->connect_errno > 0){
die('Could not connect: ' .$link->connect_error());
}

if ($_POST['password']!=$_POST['confirmpass']){
    echo "That passwords entered do not match, please use your browser's back button and try again.";

}else{
    $fname = $_POST['First_Name'];
    $lname = $_POST['Last_Name'];
    $usid = $_POST['user_id'];
    $pword = $_POST['password'];
    $admins = $_POST['isadmin'];
    $admin_user_id = $_POST['your_user_id'];
    $admin_pass = $_POST['your_password'];
    $table = "tc_".$usid;
   $isadmincheck = "yes";
    $sql = "INSERT INTO tc_users (FirstName, LastName, userid, password, isadmin) VALUES ('$fname', '$lname', '$usid', '$pword', '$admins')";
    $newtable = "CREATE TABLE $table (id INT(11) NOT NULL AUTO_INCREMENT, time datetime NOT NULL,punchtype VARCHAR(255) NOT NULL,groupname VARCHAR(255) NOT NULL, dept VARCHAR(255) NOT NULL, notes VARCHAR(255), PRIMARY KEY (id))";
    $adminchecksql = "SELECT * FROM tc_users WHERE userid = '$admin_user_id' and password = '$admin_pass' and isadmin = '$isadmincheck'";
    $admincheck = $link->query($adminchecksql);
    if ($admincheck->num_rows == 0) {
        echo "Sorry, you are not an administrator or your username and password were not correct. so this will not work,";
    }else{
        $sqlquery = $link->query($sql);
        $tablequery = $link->query($newtable);
        echo "User " . $usid . " added successfully.";
    } 

}
$link->close();
?>
<form method="GET" action="<path to add new user page>">
<input type="submit" value="Add New User">
</form> 
<form method="GET" action="<path to user administration page>">
<input type="submit" value="Cancel">
</form>

2 个答案:

答案 0 :(得分:1)

mysql_*函数使用旧的MySQL API,它不是面向对象的许多其他东西,你试图在代码中稍后使用OOP语法;因此错误。

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

if ($link->connect_errno > 0){        // Na Na, No such property

}

最有可能的是,您正在寻找mysqli

$link = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

答案 1 :(得分:0)

更改

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); 

到此

$link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

您错过了mysqli_connect()