SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,777
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Do I need more error-handling on Results-Set?

    Below is a snippet that involves retrieving an Article...

    PHP Code:
        // Execute query.
        
    mysqli_stmt_execute($stmt);

        
    // Store results.
        
    mysqli_stmt_store_result($stmt);

        
    // Check # of Records Returned.
        
    if (mysqli_stmt_num_rows($stmt)==1){
            
    // Article was Found.
            
    $articleExists TRUE;

            
    // Bind result-set to variables.
            
    mysqli_stmt_bind_result($stmt$articleID$title$description$keywords
                                
    $heading$subHeading$publishedOn$author,
                                
    $body$referenceListing$endnoteListing);

            
    // Fetch record.
            
    mysqli_stmt_fetch($stmt);

            
    // Close prepared statement.
            
    mysqli_stmt_close($stmt);                                // ???? 
    If the Article was found, can I safely assume that there will be data in all required fields in the Results-Set above?

    In other words, if something fails, can I assume that the *entire process* of "Bind result-set to variables" would fail?

    The context of my question is really whether I need to INITIALIZE variables like "$articleID" or "$body" for fear that if the query didn't return these required fields that I would get an undefined variable error?!

    I did initialize these variables since they are optional...
    Code:
    	$subHeading = $referenceListing = $endnoteListing = '';
    Hope this thread makes sense?!


    Debbie

  2. #2
    Non-Member Max Height's Avatar
    Join Date
    Dec 2011
    Posts
    303
    Mentioned
    6 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by DoubleDee View Post
    In other words, if something fails, can I assume that the *entire process* of "Bind result-set to variables" would fail?
    Have a read thru the online php manual. In it you will see that mysqli_stmt_bind_result returns true or false. It's probably a good idea to have some "plan b code" in your code in case it returns false and execute subsequent code only if it returns true.








Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •