Question

if($stmt = $this->Sys->db->prepare("INSERT INTO dj_videos (title, url, caption) VALUES (?, ?, ?)")) {

        $stmt->bind_param('sss', $title, $url, $comment);
        $stmt->execute();
        $stmt->store_result();
        $stmt->fetch();

        if($stmt->num_rows == 1) {

            return TRUE;

        }
        else {

            return FALSE;

        }

This is returning false but it inserts the information into the database everytime. It blows my mind.

Was it helpful?

Solution

mysqli_stmt::execute Returns TRUE on success or FALSE on failure. So you only need to return the result of it:

if($stmt = $this->Sys->db->prepare("INSERT INTO dj_videos (title, url, caption) VALUES (?, ?, ?)")) {
    $stmt->bind_param('sss', $title, $url, $comment);
    return $stmt->execute();
}

By the way, if the statement is UPDATE, DELETE, or INSERT, the total number of affected rows can be determined by using the mysqli_stmt_affected_rows() function.

OTHER TIPS

num_rows returns the number of rows in a statement result set.

You want affected_rows, which returns the number of rows changed, deleted, or inserted by the last executed statement

    $stmt->bind_param('sss', $title, $url, $comment);
    $stmt->execute();
    $stmt->store_result();
    $stmt->fetch();

    if($stmt->affected_rows == 1) {

        return TRUE;

    }
    else {

        return FALSE;

    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow