簡體   English   中英

我應該如何在通過sql數據庫使用php計算時編寫此代碼

[英]how should i write this code on calculating with php from an sql database

我正在嘗試使用一些數據來計算商品的銷售量,以使用其ID標識每個銷售量從SQL數據庫中獲取總計,然后將其顯示在屏幕上。 供進一步使用。

<?php
$dbhost = 'localhost:3306';
                $dbuser = 'root';
                $dbpass = 'mysql';
                $conn = mysql_connect($dbhost, $dbuser, $dbpass);
                if (! $conn ) {
                die('Could not connect: ' . mysql_error());
                }

    $sql = "select item_num * price + tp AS total from buyers where 
 Ref_code = '" . $result16['ref_code'] . "' ";

                mysql_select_db('sales');

  $retval1 = mysql_query($sql, $conn);

  if (!$retval1) { 
die('Could not get data: ' . mysql_error());
}


 $result15 = mysql_fetch_assoc($retval1); 
?>

 <?php echo $result15['total']; ?> '

我希望當我將ID傳遞給查詢時,它會顯示每筆交易的總額,但它不會顯示任何內容。

1)您可以將計算值存儲在定義的變量“ @total”中,並引用選擇查詢。 有關更多信息,請檢查-https://dev.mysql.com/doc/refman/8.0/en/user-variables.html

查詢會像。

$sql = "SELECT 
@total := b.item_num * b.price + b.tp as total
FROM buyers b
where 
Ref_code = '" . $result16['ref_code'] . "' ";

2)我認為您應該使用由mysqli安裝的mysqli。 有關更多信息- 為什么我不應該在PHP中使用mysql_ *函數?

3)並且還需要使用准備好的語句來防止SQL注入問題,有關更多信息,請檢查-https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php

您可以檢查以下示例。

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

//store a calculated value in a defined variable total and use in select statement
$query = "SELECT 
@total := b.item_num * b.price + b.tp as total
FROM buyers b
where 
Ref_code = ?";

$ref_code = $result16['ref_code'];
if($stmt = $conn->prepare($query)) {

   $stmt->bind_param("s", $ref_code); 
   $stmt->execute(); 

   /*Binds variables to a prepared statement for result storage
     for more detailscheck -https://www.php.net/manual/en/mysqli-stmt.bind-result.php
     */
   $stmt->bind_result($total);

   while ($stmt->fetch()) {
     echo "total: $total\n";
   }
}

$stmt->close();

經過無數次嘗試后,我發現一個錯誤是SQL變量,我使用varchar來存儲數據,因此查詢沒有看到int或float數據,因此無法計算值並給出預期的結果。 在將我的SQL變量更改為兩倍,然后使用我之前嘗試過的公式后,它起作用了。

           <?php       
        $sql1 = "select * from buyers where Ref_code = '" . mysql_insert_id() . "' ";
                  mysql_select_db('sales');
                                        $retval1 = mysql_query($sql1, $conn);
                                        if (!$retval1) {
                                            die('Could not get data: ' . 
   mysql_error());
                                        }


                                        $result16 = mysql_fetch_assoc($retval1);


            }





                ?>
                <?php 


  $num1 = $result16['price'];
  $num2 = $result16['item_num'];
  $num3 = $result16['tp'];
  global $num1;
  global $num2;
  global $num3;
  function total()
 {
  global $num1;
  global $num2;
  global $num3;
 $sum1 = $num1 * $num2;
 $sum = $sum1 + $num3;
 echo "$sum";
 }
                 ?>
                <div id ="recipt">
               <p>
   Price: N<?php echo $result16['price'];?> <br><br>

  <b>Total Amount of Item: <u>N<?php total() ?> </u></b> 
    </p>   
   </div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM