0

我在从 codeigniter 控制器中获取这些数据时遇到问题。

    $q = $this->db->get('offers_orders');
    $this->db->select('total');
    $this->db->where('order_number', $orderid);


    $orderdata = $q->result_array();

    $orderamount = $orderdata[0]['total'];

你看到这段代码有什么问题吗?

4

3 回答 3

2

是的,试试:

$this->db->select('count(*) as total', false);
$this->db->where('order_number', $orderid);
$q = $this->db->get('offers_orders');

或者,

$q = $this->db->select('count(*) as total', false)->where('order_number', $orderid)->get('offers_orders');
于 2013-07-10T11:14:26.247 回答
0

您需要首先定义您的 select 和 where 方法,然后调用用于获取数据的 get() 方法。

像这样:

$this->db->select('total');
$this->db->where('order_number', $orderid);
$q = $this->db->get('offers_orders');

正如我从您的查询中看到的那样,您只需要获取一个结果,更好的解决方案是使用 row() 函数,因为该函数返回单个结果行。

像这样:

$orderdata = $q->row();
$orderamount = $orderdata->total;

要了解更多信息,您可以阅读此文章

于 2013-07-10T12:09:46.543 回答
-1
 <!DOCTYPE html>
<html>
<body>

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

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

$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
     // output data of each row
     while($row = $result->fetch_assoc()) {
         echo "<br> id: ". $row["id"]. " - Name: ". $row["firstname"]. " " . $row["lastname"] . "<br>";
     }
} else {
     echo "0 results";
}

$conn->close();
?> 

</body>
</html>
于 2015-04-18T04:36:08.417 回答