![](/img/trans.png)
[英]How to check if a value already exists in the database with codeigniter
[英]How to check if content is already in the database and if it not then enter it?
我做了一個簡單的php類,它從astrology.com提取了星座的每日星座運勢,我想在wordpress中使用它(使用$ wpdb)。
工作原理(理論上):
問題是我被卡在第2點和第3點之間的某個地方...(我不知道如何檢查課程是否返回了一些內容,如果不顯示前一天的內容)
<?php require('./wp-blog-header.php');
$horoscope = new Horoscope(); global $wpdb;
$config = array('sign' => 'cancer',
'type' => 'daily',
'date' => date('Y-m-d'),);
$query = $wpdb->get_results("SELECT * FROM `wp_horoscope` WHERE zodiacal_sign = '".$config['sign']."' AND last_updated = '".$config['date']."';", ARRAY_A);
// step 1
if( $query && $query['0']['content'] != '' ){ $content = $query['0']['content']; }
// step 2
elseif( empty($query) || $content === '' ){
$content = $horoscope->get_daily_horoscope($config);
$fetched_content = $content;
$update = $wpdb->prepare("INSERT INTO `wp_horoscope` (`last_updated`, `content`, `zodiacal_sign`) values (%s,%s,%s)", $config['date'], $content, $config['sign']);
$wpdb->query($update);}
// step 3
elseif( $fetched_content === '' ) { // if previous function `get_daily_horoscope` does not return any content return the content for previous day
$yesterday_date = date('Y-m-d', strtotime('- 1 day'));
$query = $wpdb->get_results("SELECT * FROM `wp_horoscope` WHERE zodiacal_sign = '".$config['sign']."' AND last_updated = '".$yesterday_date."';", ARRAY_A);
$content = $query['0']['content']; }
// step 4
else{$content = 'The information is not in the DB';}
echo $content;
?>
有人可以幫我嗎?
由於內容未初始化,因此您總是要// step 2
進入// step 2
,因此elseif ( empty($query) || $content == '' )
將始終返回true,因為未聲明的$content
將“等於” ''。
您可以嘗試: $content === ''
(注意額外的“ =”),除非它實際上是一個空字符串,否則它不會為true。
在我看來,您永遠不會進入第3步,因為第2步滿足了您對第3步的要求。
elseif( a || b) {
}
elseif( b ) {
}
如果b為真,則永遠不會進入elseif(b),因為b在elseif(a || b)中已經為真。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.