简体   繁体   中英

Getting data from MySQL table via Codeigniter

I'm having issues getting this data from within a codeigniter Controller.

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


    $orderdata = $q->result_array();

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

Do you see anything wrong with this code ?.

Yes, Try :

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

OR,

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

You need first to defined your select and where method then you call the get() method which are used for get data.

Like this:

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

And as I see from your query, you need to fetch only one result and for it better solution is to use row() function because this function returns a single result row.

Like this:

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

Also to learn more about it you can read this acticle .

 <!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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM