Lassie Posted May 14, 2015 Share Posted May 14, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/296320-checks-for-errors/ Share on other sites More sharing options...
fastsol Posted May 14, 2015 Share Posted May 14, 2015 Well there are number of things wrong in the code, but lets start with the error you seem to get. What is the error? You didn't tell us that. Quote Link to comment https://forums.phpfreaks.com/topic/296320-checks-for-errors/#findComment-1511866 Share on other sites More sharing options...
Lassie Posted May 14, 2015 Author Share Posted May 14, 2015 Hi Sorry, its a syntax error after { unexpected if on if($row). Quote Link to comment https://forums.phpfreaks.com/topic/296320-checks-for-errors/#findComment-1511872 Share on other sites More sharing options...
CroNiX Posted May 14, 2015 Share Posted May 14, 2015 (edited) Look at the previous line, which is missing the ending semicolon. Also the parenthesis are unnecessary: ($row=mysql_fetch_assoc($result)) Edited May 14, 2015 by CroNiX Quote Link to comment https://forums.phpfreaks.com/topic/296320-checks-for-errors/#findComment-1511873 Share on other sites More sharing options...
Lassie Posted May 14, 2015 Author Share Posted May 14, 2015 Ok, thanks that fixed that but syntax erro on my final else so I guess my {} are not right. Quote Link to comment https://forums.phpfreaks.com/topic/296320-checks-for-errors/#findComment-1511874 Share on other sites More sharing options...
CroNiX Posted May 14, 2015 Share Posted May 14, 2015 Right, so go through and match all opening braces to closing braces. You have extras. Quote Link to comment https://forums.phpfreaks.com/topic/296320-checks-for-errors/#findComment-1511875 Share on other sites More sharing options...
Lassie Posted May 14, 2015 Author Share Posted May 14, 2015 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/296320-checks-for-errors/#findComment-1511877 Share on other sites More sharing options...
Ch0cu3r Posted May 14, 2015 Share Posted May 14, 2015 (edited) 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 May 14, 2015 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/296320-checks-for-errors/#findComment-1511879 Share on other sites More sharing options...
Lassie Posted May 14, 2015 Author Share Posted May 14, 2015 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/296320-checks-for-errors/#findComment-1511880 Share on other sites More sharing options...
Lassie Posted May 14, 2015 Author Share Posted May 14, 2015 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/296320-checks-for-errors/#findComment-1511884 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.