Reputation: 999
This is my first time trying web programming and I'm currently trying to use Code Igniter. I got this from tutorial but my problem seems a little bit different, I tried several thing but none work
So I want to get data from my db, here's my code
public function can_log_in($username, $password){
$query = $this->db->query("SELECT col1, col2 FROM table1 where id_login = '$username' and id_password = '$password'");
$data = mysql_fetch_row($query); //got error this line, and several line also on libraries and helper
if ($query->num_rows() == 1){
return (string) $data[0];
}else {
return "0";
}
}
Error said:
Supplied argument is not a valid MySQL result resource
On controller I used:
if($this->model_get->can_log_in($username, $password) != "0")
Honestly I don't know if this work or should I use <>
? haven't got opportunity to tried it, but that's another problem.
So how can I get col1 and col2 here?
Upvotes: 1
Views: 730
Reputation: 41885
Since you're using codeigniter, just be faithful to it, use its built-in functions:
public function can_log_in($username, $password)
{
$username = $this->db->escape($username); // this automatically adds single quotes
$password = $this->db->escape($password);
$query = $this->db->query("SELECT col1, col2 FROM table1 where id_login = $username and id_password = $password");
if($quer->num_rows() > 0) {
$data = $query->row(); // fetches single row
return $data->col1;
}
}
I also suggest use codeigniters active record, so that you don't have to worry about escaping. They already to it for you:
public function can_log_in($username, $password)
{
$this->db->select('col1', 'col2');
$this->db->where('username', $username);
$this->db->where('password', $password);
$query = $this->db->get('table1');
return ($query->num_rows() > 0);
}
In controller:
$username = $this->input->post('username');
$password = $this->input->post('password');
$can_log = $this->model_name->can_log_in($username, $password);
if($can_log) {
// yahoo! can log!
} else {
// sorry but you cannot!
}
Upvotes: 1
Reputation: 89
CodeIgniter provides several methods to perform on query results.
See here: https://ellislab.com/codeigniter/user-guide/database/results.html
result()
returns an array of PHP objects.
row()
returns a single PHP object for that row.
result_array()
returns an array of arrays.
row_array()
returns a single array for that row.
row()
and row_array()
optionally take a parameter which is the number of the row you'd like to return.
$rows = $query->result(); //array of objects
$rows_array = $query->result_array(); //array of arrays
$row = $query->row(); //single object
$row_array = $query->row_array(); //single array
Upvotes: 0
Reputation: 923
In PHP, you need to connect to a database using
$link = mysqli_connect("myhost","myusername","mypassword","mybd") or die("Error " . mysqli_error($link));
Once you've connected to a database you can query that database using
$data = mysqli_query($con,"SELECT * FROM tablename");
Looking at what you've written, it seems like your code is hitting an error because you haven't connected to a database to query. Once you've established a connection you can submit queries and parse them using a loop to enumerate each row of results of
for($i = 0;$i<mysqli_num_rows($data);$i++) {
$row = mysqli_fetch_row($data));
$username = row[0]; //usernames are column 1
$password = row[1]; //passwords are column 2
}
And so on. You can read up more on using PHP at PHP.net
Upvotes: 0
Reputation: 7240
Try this:
$query = $this->db->query("SELECT col1, col2 FROM table1 where id_login = '$username' and id_password = '$password'");
$data = $query->row_array();
if ($query->num_rows() == 1){
return (string) $data['col1']; // Need to give the field name
}else {
return "0";
}
Upvotes: 0