admin管理员组文章数量:1130349
I've inherited an old WordPress site that my customer doesn't want to be rebuilt. It has an old MySQL query in the functions.php of the theme that is supposed to put a category image with the category. Over time of course, WordPress has been updated and the query doesn't work any more. I'm hoping someone can help advise how I can update this. The code is:
function catImg($catId){
$data = mysql_fetch_object(mysql_query('SELECT * FROM `wp_ig_caticons` WHERE `cat_id` = "'. $catId .'"'));
if($data->icon){
echo '<img height="158" width="121" title="" alt="skyway" class="attachment-post-thumbnail wp-post-image" src="'. site_url() .'/'. $data->icon .'"/>';
}
else{
echo '<img height="158" width="121" alt="no-image" class="attachment-post-thumbnail wp-post-image" src="'. site_url() .'/wp-content/themes/sky-way/images/no-image.jpg"/>';
}
}
Thank you in advance
I've inherited an old WordPress site that my customer doesn't want to be rebuilt. It has an old MySQL query in the functions.php of the theme that is supposed to put a category image with the category. Over time of course, WordPress has been updated and the query doesn't work any more. I'm hoping someone can help advise how I can update this. The code is:
function catImg($catId){
$data = mysql_fetch_object(mysql_query('SELECT * FROM `wp_ig_caticons` WHERE `cat_id` = "'. $catId .'"'));
if($data->icon){
echo '<img height="158" width="121" title="" alt="skyway" class="attachment-post-thumbnail wp-post-image" src="'. site_url() .'/'. $data->icon .'"/>';
}
else{
echo '<img height="158" width="121" alt="no-image" class="attachment-post-thumbnail wp-post-image" src="'. site_url() .'/wp-content/themes/sky-way/images/no-image.jpg"/>';
}
}
Thank you in advance
Share Improve this question edited Dec 29, 2018 at 23:04 butlerblog 5,1413 gold badges28 silver badges44 bronze badges asked Dec 29, 2018 at 20:16 fuzball1stfuzball1st 32 bronze badges1 Answer
Reset to default 2mysql_fetch_object() was deprecated in PHP 5.5 and fully removed in PHP 7.0, which is probably why it no longer works for you. It's always better to use WP functions - often they are wrappers for something in PHP (or elsewhere), but by using the WP wrapper version, it usually protects you from deprecation (since the WP method can be updated to accommodate the new scripting language changes) - that way your core code wouldn't need to change.
Try using the $wpdb->get_row(). It's pretty much the same as what you have, but more "WordPressy":
function catImg( $catId ){
global $wpdb;
$data = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM `' . $wpdb->prefix . 'ig_caticons` WHERE `cat_id` = %d', absint( $catId ) ) );
if ( $data->icon ) {
echo '<img height="158" width="121" title="" alt="skyway" class="attachment-post-thumbnail wp-post-image" src="'. esc_url( site_url( esc_attr( $data->icon ) ) ) . '"/>';
} else {
echo '<img height="158" width="121" alt="no-image" class="attachment-post-thumbnail wp-post-image" src="'. site_url( '/wp-content/themes/sky-way/images/no-image.jpg' ) . '"/>';
}
}
I've inherited an old WordPress site that my customer doesn't want to be rebuilt. It has an old MySQL query in the functions.php of the theme that is supposed to put a category image with the category. Over time of course, WordPress has been updated and the query doesn't work any more. I'm hoping someone can help advise how I can update this. The code is:
function catImg($catId){
$data = mysql_fetch_object(mysql_query('SELECT * FROM `wp_ig_caticons` WHERE `cat_id` = "'. $catId .'"'));
if($data->icon){
echo '<img height="158" width="121" title="" alt="skyway" class="attachment-post-thumbnail wp-post-image" src="'. site_url() .'/'. $data->icon .'"/>';
}
else{
echo '<img height="158" width="121" alt="no-image" class="attachment-post-thumbnail wp-post-image" src="'. site_url() .'/wp-content/themes/sky-way/images/no-image.jpg"/>';
}
}
Thank you in advance
I've inherited an old WordPress site that my customer doesn't want to be rebuilt. It has an old MySQL query in the functions.php of the theme that is supposed to put a category image with the category. Over time of course, WordPress has been updated and the query doesn't work any more. I'm hoping someone can help advise how I can update this. The code is:
function catImg($catId){
$data = mysql_fetch_object(mysql_query('SELECT * FROM `wp_ig_caticons` WHERE `cat_id` = "'. $catId .'"'));
if($data->icon){
echo '<img height="158" width="121" title="" alt="skyway" class="attachment-post-thumbnail wp-post-image" src="'. site_url() .'/'. $data->icon .'"/>';
}
else{
echo '<img height="158" width="121" alt="no-image" class="attachment-post-thumbnail wp-post-image" src="'. site_url() .'/wp-content/themes/sky-way/images/no-image.jpg"/>';
}
}
Thank you in advance
Share Improve this question edited Dec 29, 2018 at 23:04 butlerblog 5,1413 gold badges28 silver badges44 bronze badges asked Dec 29, 2018 at 20:16 fuzball1stfuzball1st 32 bronze badges1 Answer
Reset to default 2mysql_fetch_object() was deprecated in PHP 5.5 and fully removed in PHP 7.0, which is probably why it no longer works for you. It's always better to use WP functions - often they are wrappers for something in PHP (or elsewhere), but by using the WP wrapper version, it usually protects you from deprecation (since the WP method can be updated to accommodate the new scripting language changes) - that way your core code wouldn't need to change.
Try using the $wpdb->get_row(). It's pretty much the same as what you have, but more "WordPressy":
function catImg( $catId ){
global $wpdb;
$data = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM `' . $wpdb->prefix . 'ig_caticons` WHERE `cat_id` = %d', absint( $catId ) ) );
if ( $data->icon ) {
echo '<img height="158" width="121" title="" alt="skyway" class="attachment-post-thumbnail wp-post-image" src="'. esc_url( site_url( esc_attr( $data->icon ) ) ) . '"/>';
} else {
echo '<img height="158" width="121" alt="no-image" class="attachment-post-thumbnail wp-post-image" src="'. site_url( '/wp-content/themes/sky-way/images/no-image.jpg' ) . '"/>';
}
}
本文标签: Update MySQL query so that it functions again
版权声明:本文标题:Update MySQL query so that it functions again 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749059249a2309844.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论