Check if row exist or not / prepared statement

Discussion in 'MySQL' started by aHMAD_SQaLLi, Aug 8, 2015.

  1. #1
    Hello
    I m having trouble with this script it gives this error message
    Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, object given in C:\xampp\htdocs\4.php on line 43

    line 43 is if(mysqli_num_rows($stmt) !== 0){

    Here is the script

    $tid = 1;
    /* create a prepared statement */
    if ($stmt = mysqli_prepare($link, "SELECT users.username, topics.topic_body FROM `users` LEFT JOIN topics ON topics.topic_author = users.id WHERE topics.id=? order by edit_time ASC")) {
        /* bind parameters for markers */
        mysqli_stmt_bind_param($stmt, "s", $tid);
        /* execute query */
        mysqli_stmt_execute($stmt);
        /* bind result variables */
        mysqli_stmt_bind_result($stmt, $name, $body);
        /* fetch value */
        mysqli_stmt_fetch($stmt);
        if(mysqli_num_rows($stmt) !== 0){
            while (mysqli_stmt_fetch($stmt)) {
                echo "username : <b>[" . $name . "]</b> replay : <b>[" . $body . "]</b><br> " ;
            }
        } else { echo "Not Found"; }
        /* close statement */
        mysqli_stmt_close($stmt);
    }
    /* close connection */
    mysqli_close($link);
    PHP:


    thanks
     
    Last edited by a moderator: Aug 16, 2015
    aHMAD_SQaLLi, Aug 8, 2015 IP
  2. Marko Platanic

    Marko Platanic Active Member

    Messages:
    30
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    53
    #2
    You're combining procedural and object oriented approaches, when you only want object oriented.

    Change if(mysqli_num_rows($stmt) !== 0){

    to if(($stmt->num_rows()) !== 0){
     
    Marko Platanic, Aug 15, 2015 IP