2017-09-20 35 views
0

我想那些從MySQL在PHP MySQL的獲取和

這裏選擇的整數之和是我的代碼

<?php 
Include 'init.php'; 
$page = '2459'; 

$ttl = array(); 

$search = $db->link->query("SELECT id,packid,rating FROM pakrate WHERE packid LIKE '%$q%'"); 

if($search->num_rows>0){ 
    while($result=$search->fetch_assoc()){ 
     $ttl[] = $row['rating']; 

     echo $ttl; 
     echo $result[SUM('rating')]; 
    } 
} 
else { 
    echo "no such query"; 
} 

所以我在做什麼錯在這裏,任何其他建議?

+0

把它放在查詢總和(等級)的總和FROM pakrate ... – clearshot66

+0

u能解釋一下嗎?因爲我不明白 –

+0

評論可以刪除,所以不是最好的答案。 @ clearshot66你能把這個評論變成候選答案嗎? – jdv

回答

0

只要運行在您的查詢的總和

<?php 

Include 'init.php'; 
$page = '2459'; 

$ttl = array(); 

// Not sure why you're doing this but there should only be one leading connection ($conn->query) unless you're doing some object item elsewhere in init. 

// I would also use prepared statements since they're more secure: 

$q = "%$q%"; // create your like variable, wherever $q is coming from?? 

$query = "SELECT sum(rating) from pakrate WHERE packid LIKE ?); 
$stmt = $db->prepare($query); // assuming $db is your connection variable 
if($stmt){ 
    $stmt->bind_param("s",$q); // Bind the variable in 
    $stmt->execute();   // Execute 
    $stmt->bind_result($rating); // Bind the result variable 
    $stmt->store_result();  // Store it so you can get num_rows 
    $rows = $stmt->num_rows; // assign rows found 

    if($rows > 0){ 
     while($stmt->fetch()){ // Loop the result 

     $ttl[] = $rating; // add to your array 

     // You can't echo an array like this.... echo $ttl; 
     // if you really want to, print_r($ttl); 
     } 
    } 
$stmt->close(); // Close it 
} 

?>