从非对象调用方法?

时间:2011-02-23 04:02:01

标签: php mysql

$query = "INSERT INTO users (name, password) VALUES ('$myusername', '$mypassword')";
if (!($result = $mysqli->query($query)))
    die("WHAT???? " . $mysqli->error . " EEEEEFFFFFFF.");

$count = $result->num_rows;

while ($row = $result->fetch_array()) {
    if ($row[name] == $myusername) {
        $mysqli->query("DELETE FROM users WHERE name='$myusername' AND password='$mypassword'");
        $count = 5;
    }
}

当我运行它时,它给了我一个错误: 致命错误:在第26行的/home/appstore/public_html/phpstoof/signedup.php中调用非对象的成员函数fetch_array()

第26行是while语句开始的地方(while(x))。 $ mysqli ALREADY mysqli()的一个实例。如果相同的代码在另一个文件上工作,我不会看到这是一个错误。

2 个答案:

答案 0 :(得分:2)

INSERT语句无法获取。

答案 1 :(得分:2)

正如@mellamokb所说,INSERT无法获取。您还混用了MySQLMySQLi

使用MySQLi,代码应该是:

$mysqli = new mysqli($db_host, $db_username, $db_password, $db_database);

$str_sql = 'INSERT INTO users (name, password) VALUES (?, ?)';

// Create a prepared statement
$stmt = $mysqli->prepare($str_sql);

// Bind parameters for markers; same order and same count in prepared statement
$stmt->bind_param('ss', $myusername, $mypassword);

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

// *************************************************************************
// If you're using a SELECT statement, each output field must be bound to 
// a variable in the same order as in SELECT
// Bind result variables
$stmt->bind_result($_var1, $_var2, $_var3, ...);

// Fetch results and generate output as an associative array
while ($stmt->fetch())
{
    // Handle $_var1, $_var2, $_var3, ...
}
// *************************************************************************

// Free stored result memory
$stmt->free_result();

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

// Close connection
$mysqli->close();