0

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;

1 Answer 1

1
$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.

Sign up to request clarification or add additional context in comments.

2 Comments

If for example i call a function within the same model. do i have to return anything from that function? or is it automatic if i do this $result = $this->function();
Well you called a function you made. How am I suppose to know what that function returns, if it returns anything at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.