开发者

Calling method from a non-object?

$query = "INSERT INTO users (name, password) VALUES ('$myusername', '$mypassword')";
if (!($result = $mysqli-&g开发者_高级运维t;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;
    }
}

When I run this, it gives me an error: Fatal error: Call to a member function fetch_array() on a non-object in /home/appstore/public_html/phpstoof/signedup.php on line 26

Where line 26 is where the while statement starts (while(x)). $mysqli ALREADY an instance of mysqli(). I don't see the how this is an error if the same code works on another file.


An INSERT statement has nothing to fetch.


As @mellamokb says, INSERT has nothing to fetch. Also you have used a mix of MySQL and MySQLi.

With MySQLi, the code should be like:

$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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜