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
https://forums.phpfreaks.com/topic/296320-checks-for-errors/
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
https://forums.phpfreaks.com/topic/296320-checks-for-errors/#findComment-1511877
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
    }

    ...
Link to comment
https://forums.phpfreaks.com/topic/296320-checks-for-errors/#findComment-1511879
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
https://forums.phpfreaks.com/topic/296320-checks-for-errors/#findComment-1511880
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
https://forums.phpfreaks.com/topic/296320-checks-for-errors/#findComment-1511884
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.