user3205047
user3205047

Reputation: 199

Codeigniter function return result value

Lets say i have this code

$sql = "SELECT Lname,Fname FROM users WHERE username = ."$username;
$result = $this->db->query($sql);

Is my syntax correct? Plus, do i have to declare $result like $result[] if i need multiple result values from it like above where i want to get the last name and first name of the username inputted.

Im only using this as my reference.

$sql = "SELECT id FROM users WHERE firstname LIKE '%" . $firstname . "%'";

Im not sure what purpose '%" has. All i know is that when i echo something, if i were to put a value. it would be like this

echo "select from user where username =".$username;

Upvotes: 0

Views: 160

Answers (1)

Connor Tumbleson
Connor Tumbleson

Reputation: 3330

$response = $this->db
                 ->select('Lname,Fname')
                 ->where('username', $username)
                 ->from('users')
                 ->get()
                 ->result_array();

I'd recommend using Active Record, as whats the point of the framework even with such a basic query if you aren't gonna take advantage of its features.

If you choose to ignore that, I'd say again. Read the documentation.

The query() function returns a database result object when "read" type queries are run, which you can use to show your results. When "write" type queries are run it simply returns TRUE or FALSE depending on success or failure. When retrieving data you will typically assign the query to your own variable, like this:

Which would tell you, that you have a result object. So you can do result(), result_array(), etc.

Upvotes: 1

Related Questions