2017-02-14 89 views
1
$query33=mysql_query("SELECT * FROM order_payment_tb where order_status='Success'",$conn) or die (mysql_error()); 
while($row33=mysql_fetch_array($query33)) { 
    $query3=mysql_query("SELECT * FROM user_order_tb where status='Pending' and order_id='".$row33['order_id']."'",$conn) or die (mysql_error()); 
    $count5=mysql_num_rows($query3); 
    { 
    echo $count5; ?> 
    <?php 
    } 
} 
?> 

我的mysql結果數據是0111,我想顯示數據爲3即(0 + 1 + 1 + 1 = 3) 。 請幫助如何計算0111爲0 + 1 + 1 + 1 = 3在php中

+0

瞭解有關JOIN的內容。並停止使用PHP的棄用mysql_ API。 – Strawberry

+0

像這樣:'echo 0 + 1 + 1 + 1;' –

+0

**警告**您的代碼容易受到sql注入攻擊 –

回答

3

str_split分割成數的陣列和陣列中的每個數字概括起來使用array_sum

與結果數據

array_sum(str_split($data)); 
0

str_split字符串轉換成數組替換$data。然後你可以遍歷這些值並添加它們,使用類型轉換將它們設置爲int,然後添加。

$str = mysql_num_rows($query3); 
    if ($str) { 
    $arr = str_split($str); 
    $sum = 0; 
    foreach ($arr as $num) { 
      $sum += (int) $num; 
    } 
    echo $sum; 
    } 
} 
?> 
+0

中刪除我認爲我們不應該鼓勵使用已棄用的API – Strawberry

1

答案是等於該查詢返回的行數...

SELECT x.name 
    , x.the 
    , y.columns 
    , y.you 
    , y.actually 
    , y.want 
    FROM order_payment_tb x 
    JOIN user_order_tb y 
    ON y.order_id = x.order_id 
WHERE x.order_status = 'Success' 
    AND y.status = 'Pending'; 
+0

非常感謝草莓 –

0
$string = '02113'; 
$stringArray = preg_split('//', $string, -1, PREG_SPLIT_NO_EMPTY); 
$count = 0; 

for ($i=0; $i < count($stringArray); $i++) { 

    $count = $count + (int)$stringArray[$i]; 

} 

echo $count; 
+0

這不能提供問題的答案。要批評或要求作者澄清,請在其帖子下方留言。 - [來自評論](/ review/low-quality-posts/15205070) – Farkie

+0

@Farkie它確實試圖回答這個問題(儘管很糟糕)。見[這篇文章](http://meta.stackoverflow.com/questions/287563/youre-doing-it-wrong-a-plea-for-sanity-in-the-low-quality-posts-queue) – Machavity

0

請試試這個,我已經更新了你的代碼,這裏是下面的代碼:

$query33=mysql_query("SELECT * FROM order_payment_tb where order_status='Success'",$conn) or die (mysql_error()); 
$sum = 0; 
while($row33=mysql_fetch_array($query33)) { 
    $query3=mysql_query("SELECT * FROM user_order_tb where status='Pending' and order_id='".$row33['order_id']."'",$conn) or die (mysql_error()); 
    $count5=mysql_num_rows($query3); 
    $sum = $sum + $count5; 

} 
echo $sum; 
?> 

希望這可能對你有用。

相關問題