0
$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;
    }
}

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.

2 Answers 2

2

An INSERT statement has nothing to fetch.

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.