Tags
Asked 2 years ago
17 Jun 2021
Views 188
Scarlett

Scarlett posted

How to echo a value from select query in codeigniter

How to echo a value from select query in codeigniter
david

david
answered Apr 27 '23 00:00

To echo a value from a select query in CodeIgniter, you can retrieve the result of the query using the $this->db->query() method and then access the value of the field you want to echo.

Here's an example:



$query = $this->db->query("SELECT name FROM users WHERE id = 1");

if ($query->num_rows() > 0) {
    $row = $query->row();
    echo $row->name;
}

In this example, we are selecting the name field from the users table where the id field equals 1. We use the $this->db->query() method to execute the query and store the result in the $query variable.

We then check if the query returned any rows using $query->num_rows() > 0 . If it did, we retrieve the first row using $query->row() and store it in the $row variable. Finally, we echo the value of the name field using $row->name.

You can modify this example to suit your specific query needs, changing the table name, column names, and where conditions as necessary. Note that you should properly sanitize any user input to prevent SQL injection vulnerabilities.
Post Answer