Jump to content

Checks for errors


Lassie

Recommended Posts

I am trying to introduce some checks to

1. see if a query result is successful

2. check if the query returns a result

3.put in appropriate error messages.

However the following gives me a syntax error and I cant find it.

global $posts;
                        
                        $attachments_id=array();
                        
                        global $wpdb;
                        $result = mysql_query("SELECT post_id FROM sw_images_upload WHERE property_title = '$property_title' ");// Get the post IDs
                        //check if there is a result
   if ($result)
      {
       //check if the query returned a row
       ($row=mysql_fetch_assoc($result))
                
              
           if($row){
                        while ( $row = mysql_fetch_assoc( $result ) ) {
			
				$attachments_id[]=$row['post_id'];//put all post ids for specific address into array
                                }
                                
                                //extract post ids and cycle through image code to display images for selected property
                                foreach ($attachments_id as $iPostID) {
                                  
                                 // Get images for all the posts for the dselected address
                                $arrImages =& get_children('post_type=attachment&post_mime_type=image&post_parent=' . $iPostID );
                                    
                                }//end of foreach
                                
                                //select all the attachment ids from the array
                                $imagesID= wp_list_pluck($arrImages, 'ID');
                                
                                //set format for shortcode to use in wordpress [gallery include ="N1.N2,N3 etc"]
                                $images=implode(" ,  ",$imagesID);
                                //echo $images;
                                echo "<p><br>Your images for $property_title </br></p>";
                                        echo do_shortcode("[gallery include='{$images}']");
                                        
                       }
                    }
                       else {
                           echo "There are no images associated with the property  $property_title  ";
                       }
                    }
                    
                    else{
                           //query failed
                           echo "An error occurred. Please try again";
                        
                    }
Any help appreciated.
Link to comment
Share on other sites

Right I now dont have a syntax error but dont get my expected result.

When I execute a valid query which I know has image attachments it fails the if($row) and gives the first error message.

I will check my brackets again.

My code now is

if ($result)
      {
       //check if the query returned a row
       $row=mysql_fetch_assoc($result);
                
              
           if($row){
                        while ( $row = mysql_fetch_assoc( $result ) ) {
			
				$attachments_id[]=$row['post_id'];//put all post ids for specific address into array
                                }
                                
                                //extract post ids and cycle through image code to display images for selected property
                                foreach ($attachments_id as $iPostID) {
                                  
                                 // Get images for all the posts for the dselected address
                                $arrImages =& get_children('post_type=attachment&post_mime_type=image&post_parent=' . $iPostID );
                                    
                                }//end of foreach
                                
                                //select all the attachment ids from the array
                                $imagesID= wp_list_pluck($arrImages, 'ID');
                                
                                //set format for shortcode to use in wordpress [gallery include ="N1.N2,N3 etc"]
                                $images=implode(" ,  ",$imagesID);
                                //echo $images;
                                echo "<p><br>Your images for $property_title </br></p>";
                                        echo do_shortcode("[gallery include='{$images}']");
                                        
                       }
      
                       else {
                           echo "There are no images associated with the property  $property_title  ";
                       }
                 }
                    
                    else{
                           //query failed
                           echo "An error occurred. Please try again";
                        }
             
Link to comment
Share on other sites

You should not use mysql_fetch_assoc() for checking if the query returned a result, as this will cause your function to skip the first row in the result. What you should use is mysql_num_rows() instead.

if(mysql_num_rows())
{
    while ( $row = mysql_fetch_assoc( $result ) )
    {
        $attachments_id[]=$row['post_id'];//put all post ids for specific address into array
    }

    ...
Edited by Ch0cu3r
Link to comment
Share on other sites

Ok I have changed that and I get the first error message.

Is my logic right that I cant just check for a result with $result or !$result because that just says whether the query executed not that the result holds any return?

My code is now

if ($result)
      {
       //check if the query returned a row
       $row=mysql_fetch_assoc($result);
                
              
           if(mysql_num_rows()){
                        while ( $row = mysql_fetch_assoc( $result ) ) {
			
				$attachments_id[]=$row['post_id'];//put all post ids for specific address into array
                                }
                              
                                //extract post ids and cycle through image code to display images for selected property
                                foreach ($attachments_id as $iPostID) {
                                  
                                 // Get images for all the posts for the selected address
                                $arrImages =& get_children('post_type=attachment&post_mime_type=image&post_parent=' . $iPostID );
                                    
                                }//end of foreach
                                
                                //select all the attachment ids from the array
                                $imagesID= wp_list_pluck($arrImages, 'ID');
                                
                                //set format for shortcode to use in wordpress [gallery include ="N1.N2,N3 etc"]
                                $images=implode(" ,  ",$imagesID);
                                //echo $images;
                                echo "<p><br>Your images for $property_title </br></p>";
                                        echo do_shortcode("[gallery include='{$images}']");
                                        
                       }
      
                       else {
                           echo "There are no images associated with the property  $property_title  ";
                       }
      }
                    
                    else{
                           //query failed
                           echo "An error occurred. Please try again";
                        }
Link to comment
Share on other sites

Thanks everybody. Having looked at mysql_num_rows I have amended the code as follows and this gives the expected result.

if ($result)
      {
       //check if the query returned a row
       
                
                if(mysql_num_rows($result)){
                        while ( $row = mysql_fetch_assoc( $result ) ) {
			
				$attachments_id[]=$row['post_id'];//put all post ids for specific address into array
                                }
                              
                                //extract post ids and cycle through image code to display images for selected property
                                foreach ($attachments_id as $iPostID) {
                                  
                                 // Get images for all the posts for the selected address
                                $arrImages =& get_children('post_type=attachment&post_mime_type=image&post_parent=' . $iPostID );
                                    
                                }//end of foreach
                                
                                //select all the attachment ids from the array
                                $imagesID= wp_list_pluck($arrImages, 'ID');
                                
                                //set format for shortcode to use in wordpress [gallery include ="N1.N2,N3 etc"]
                                $images=implode(" ,  ",$imagesID);
                                //echo $images;
                                echo "<p><br>Your images for $property_title </br></p>";
                                        echo do_shortcode("[gallery include='{$images}']");
                                        
                       }
      
                       else {
                           echo "There are no images associated with the property  $property_title  ";
                          
                       }
      }
                    
                    else{
                           
                            //query failed
                          echo "An error occurred. Please try again";
                            
                        }
                    
                        
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.