my problem is i whenever i input fname and mname i got an error from the database. i just want to search both of fname and mname in search field.
my search controller function
public function search()
{
$li = $this->session->userdata('logged_in');
$id = $this->session->userdata('idnumber');
if($li == TRUE)
{
$this->load->model('users_model');
$this->load->helper('smiley');
$this->load->library('table');
$image_array = get_clickable_smileys('http://localhost/efg/images/smileys', 'status');
$col_array = $this->table->make_columns($image_array, 8);
$image_array2 = get_clickable_smileys('http://localhost/efg/images/smileys', 'status');
$col_array2 = $this->table->make_columns($image_array2, 8);
$this->data['smiley_table1'] = $this->table->generate($col_array);
$this->data['smiley_tables'] = $this->table->generate($col_array2);
if($this->input->post())
{
$search = $this->input->post('search');
$memb = $this->users_model->search($search);
$usersearch = $this->users_model->search($search);
$grpsearch = $this->users_model->searchgrp($search);
redirect ('home/profile/'.$memb);
}
}
}
My users_model model
public function search($search)
{
$this->db->select('*');
$this->db->from('users');
$this->db->like('username',$search);
$this->db->or_like('fname',$search);
$this->db->or_like('lname',$search);
$this->db->or_like('mname',$search);
$query = $this->db->get();
foreach ($query->result_array() as $row)
{
$memb = $row['idnumber'];
}
$error = 'There is no record for the one you searched. Please go Back.';
$query1 = $this->db->query("SELECT * FROM users WHERE idnumber ='$memb'");
$hehe = $query1->result_array();
if($hehe==NULL)
{
echo $error; exit;
}
else
{
return $memb;
}
}
Always check the varibles using in condition are declared before the condition.
If it doesnt pass the condition what would be the output
public function search($search)
{
$this->db->select('*');
$this->db->from('users');
$this->db->like('username',$search);
$this->db->or_like('fname',$search);
$this->db->or_like('lname',$search);
$this->db->or_like('mname',$search);
$query = $this->db->get();
$memb = null;
foreach ($query->result_array() as $row)
{
$memb = $row['idnumber'];
}
$error = 'There is no record for the one you searched. Please go Back.';
$query1 = $this->db->query("SELECT * FROM users WHERE idnumber =$memb");
$hehe = $query1->result_array();
if($hehe==NULL)
{
return $error;
}
else
{
return $memb;
}
}
Make sure the variables that youll be using in queries are set. Trap before executing queries.
Also, use Query Builder (Active Record) when you can.
public function search($search)
{
$this->db->select('*');
$this->db->from('users');
$this->db->like('username',$search);
$this->db->or_like('fname',$search);
$this->db->or_like('lname',$search);
$this->db->or_like('mname',$search);
$query = $this->db->get();
$memb = '';
if ($query->result_array() > 0) {
foreach ($query->result_array() as $row) {
$memb = $row['idnumber'];
}
if ($memb) {
$this->db->select("*");
$this->db->where("idnumber", $memb);
$query1 = $this->db->get("users");
//$query1 = $this->db->query("SELECT * FROM users WHERE idnumber ='$memb'");
$hehe = $query1->result_array();
return ($hehe) ? $memb : false;
}
return false;
}
$error = 'There is no record for the one you searched. Please go Back.';
return $error;
}
OK, I am new to PHP so I plead you to have understanding.
I have made my new application, however I can't figure out why is it complaining about. I get very odd and strange error
db->query($sql);
if ($query->num_rows() > 0) {
$row = $query->row();
foreach ($row as $key => $val) {
$this->$key = $val;
}
return $row;
}
return null;
}
function getUser($info=NULL, $active=NULL) {
$sql = "SELECT * FROM tv_user WHERE 1 = 1 ";
if (!is_null($info)) {
if (empty($info->username) || empty($info->password)) {
return null;
}
if (!empty($info->username)) {
$sql .= " AND (username = '{$info->username}' OR email = '{$info->username}' OR id = '{$info->username}') ";
}
if (!empty($info->password)) {
$sql .= " AND password = '".md5($info->password)."' ";
}
}
if (!empty($active)) {
$sql .= " AND active = '{$active}' ";
}
$query = $this->db->query($sql);
if ($query->num_rows() > 0) {
$row = $query->row();
foreach ($row as $key => $val) {
$this->$key = $val;
}
return $row;
}
return null;
}
function getUsers($active=NULL) {
$sql = "SELECT * FROM tv_user WHERE 1 = 1";
if (!empty($active)) {
$sql .= " AND active = '{$active}'";
}
$query = $this->db->query($sql);
return $query->result();
}
function isUserExist($username=NULL, $active=NULL) {
$sql = "SELECT * FROM tv_user WHERE 1 = 1 ";
if (!empty($username)) {
$sql .= " AND username = '{$username}' ";
}
if (!empty($active)) {
$sql .= " AND active = '{$active}' ";
}
$query = $this->db->query($sql);
return ($query->num_rows() > 0);
}
function getUserByEmail($email, $active=NULL) {
if (empty($email)) { return null; }
$sql = "SELECT * FROM tv_user WHERE email = '{$email}' ";
if (!empty($active)) {
$sql .= " AND active = '{$active}' ";
}
$query = $this->db->query($sql);
if ($query->num_rows() > 0) {
$row = $query->row();
foreach ($row as $key => $val) {
$this->$key = $val;
}
return $row;
}
return null;
}
function getSocialUser($uid=NULL, $provider,$email=NULL, $active=NULL) {
if (empty($provider)) { return null; }
$sql = "SELECT * FROM tv_user WHERE 1 = 1 ";
if (!empty($uid)) { $sql .= " AND uid = '{$uid}' "; }
if (!empty($email)) { $sql .= " AND email = '{$email}' "; }
$sql .= " AND provider='{$provider}'";
if (!empty($active)) { $sql .= " AND active = '{$active}' "; }
$query = $this->db->query($sql);
if ($query->num_rows() > 0) {
$row = $query->row();
foreach ($row as $key => $val) {
$this->$key = $val;
}
return $row;
}
return null;
}
function uuid() {
return strtoupper(substr(dechex(time()).dechex(mt_rand(1,65535)), 0, 6));
}
function add($data) {
return $this->db->insert("tv_user", $data);
}
function update($data, $where) {
return $this->db->update("tv_user", $data, $where);
}
function delete($where) {
return $this->db->delete("tv_user", $where);
}
function login($username, $password) {
if (empty($username) || empty($password)) {
return FALSE;
}
$password = md5($password);
$sql = "SELECT * FROM tv_user WHERE username='{$username}' AND password='{$password}' AND active=1";
$query = $this->db->query($sql);
if ($query->num_rows() > 0) {
$user = $query->row();
$user_data = array();
foreach($user as $key => $val){
$user_data[$key] = $val;
}
$user_data["login"] = TRUE;
$this->session->set_userdata('user', $user_data);
if ($user->user_type == USR_ROOT) {
$this->session->set_userdata('root_addmin_logged_in', TRUE);
$this->session->set_userdata('addmin_logged_in', TRUE);
} else
if (in_array($user->user_type,array(USR_ADMIN,
USR_MODERATOR,
USR_TOUR,
USR_HOTEL,
USR_FLIGHT,
USR_VISA)
))
{
$this->session->set_userdata('root_addmin_logged_in', FALSE);
$this->session->set_userdata('addmin_logged_in', TRUE);
} else {
$this->session->set_userdata('root_addmin_logged_in', FALSE);
$this->session->set_userdata('addmin_logged_in', FALSE);
}
$this->session->set_userdata('logged_in', TRUE);
$this->session->set_userdata('logged_user', $user);
return TRUE;
}
return FALSE;
}
function logout() { $this->session->sess_destroy(); }
function verify_reset_password_code($email, $code) {
if (empty($email) || empty($code)) {
return FALSE;
}
$sql = "SELECT * FROM tv_user WHERE email = '{$email}' ";
$query = $this->db->query($sql);
if ($query->num_rows() > 0) {
$row = $query->row();
return ($code == md5(SITE_NAME.$row->fullname))? TRUE : FALSE;
} else {
return FALSE;
}
}
}
?>
which is interpreted as
PHP Fatal error: Class 'M_user' not found in C:\wamp\www\system\core\Loader.php on line 303
I am not very good in this since I am new. I have to say that I am running my application under WAMP.It may be something trivial, but I don't get it.
I have even included all modules in my autoload.php file as following:
$autoload['model'] = array(
'm_user',
'm_user_online',
'm_content_category',
'm_content',
'm_nation',
'm_mail',
'm_message',
'm_feedback',
'm_photo',
'm_tour',
'm_tour_rate',
'm_tour_departure',
'm_tour_departure_rate',
'm_tour_itinerary',
'm_tour_category',
'm_tour_activity',
'm_tour_destination',
'm_tour_tripnote',
'm_tour_request',
'm_visa',
'm_visa_tips',
'm_question',
'm_hotel',
'm_room',
'm_room_rate',
'm_album_category',
'm_album',
'm_tour_option_category'
);
Part of the code where it gets mentioned:
// --------------------------------------------------------------------
/**
* Model Loader
*
* This function lets users load and instantiate models.
*
* #param string the name of the class
* #param string name for the model
* #param bool database connection
* #return void
*/
public function model($model, $name = '', $db_conn = FALSE)
{
if (is_array($model))
{
foreach ($model as $babe)
{
$this->model($babe);
}
return;
}
if ($model == '')
{
return;
}
$path = '';
// Is the model in a sub-folder? If so, parse out the filename and path.
if (($last_slash = strrpos($model, '/')) !== FALSE)
{
// The path is in front of the last slash
$path = substr($model, 0, $last_slash + 1);
// And the model name behind it
$model = substr($model, $last_slash + 1);
}
if ($name == '')
{
$name = $model;
}
if (in_array($name, $this->_ci_models, TRUE))
{
return;
}
$CI =& get_instance();
if (isset($CI->$name))
{
show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
}
$model = strtolower($model);
foreach ($this->_ci_model_paths as $mod_path)
{
if ( ! file_exists($mod_path.'models/'.$path.$model.'.php'))
{
continue;
}
if ($db_conn !== FALSE AND ! class_exists('CI_DB'))
{
if ($db_conn === TRUE)
{
$db_conn = '';
}
$CI->load->database($db_conn, FALSE, TRUE);
}
if ( ! class_exists('CI_Model'))
{
load_class('Model', 'core');
}
require_once($mod_path.'models/'.$path.$model.'.php');
$model = ucfirst($model);
$CI->$name = new $model();
$this->_ci_models[] = $name;
return;
}
// couldn't find the model
show_error('Unable to locate the model you have specified: '.$model);
}
This is my m_user.php class:
<?
class M_User extends CI_Model
{
function load($id)
{
$sql = "SELECT * FROM tv_user WHERE id = '{$id}' ";
$query = $this->db->query($sql);
if ($query->num_rows() > 0) {
$row = $query->row();
foreach ($row as $key => $val) {
$this->$key = $val;
}
return $row;
}
return null;
}
function getUser($info=NULL, $active=NULL)
{
$sql = "SELECT * FROM tv_user WHERE 1 = 1 ";
if (!is_null($info)) {
if (empty($info->username) || empty($info->password)) {
return null;
}
if (!empty($info->username)) {
$sql .= " AND (username = '{$info->username}' OR email = '{$info->username}' OR id = '{$info->username}') ";
}
if (!empty($info->password)) {
$sql .= " AND password = '".md5($info->password)."' ";
}
}
if (!empty($active)) {
$sql .= " AND active = '{$active}' ";
}
$query = $this->db->query($sql);
if ($query->num_rows() > 0) {
$row = $query->row();
foreach ($row as $key => $val) {
$this->$key = $val;
}
return $row;
}
return null;
}
function getUsers($active=NULL)
{
$sql = "SELECT * FROM tv_user WHERE 1 = 1";
if (!empty($active)) {
$sql .= " AND active = '{$active}'";
}
$query = $this->db->query($sql);
return $query->result();
}
function isUserExist($username=NULL, $active=NULL)
{
$sql = "SELECT * FROM tv_user WHERE 1 = 1 ";
if (!empty($username)) {
$sql .= " AND username = '{$username}' ";
}
if (!empty($active)) {
$sql .= " AND active = '{$active}' ";
}
$query = $this->db->query($sql);
return ($query->num_rows() > 0);
}
function getUserByEmail($email, $active=NULL)
{
if (empty($email)) {
return null;
}
$sql = "SELECT * FROM tv_user WHERE email = '{$email}' ";
if (!empty($active)) {
$sql .= " AND active = '{$active}' ";
}
$query = $this->db->query($sql);
if ($query->num_rows() > 0) {
$row = $query->row();
foreach ($row as $key => $val) {
$this->$key = $val;
}
return $row;
}
return null;
}
function getSocialUser($uid=NULL, $provider,$email=NULL, $active=NULL)
{
if (empty($provider)) {
return null;
}
$sql = "SELECT * FROM tv_user WHERE 1 = 1 ";
if (!empty($uid)) {
$sql .= " AND uid = '{$uid}' ";
}
if (!empty($email)) {
$sql .= " AND email = '{$email}' ";
}
$sql .= " AND provider='{$provider}'";
if (!empty($active)) {
$sql .= " AND active = '{$active}' ";
}
$query = $this->db->query($sql);
if ($query->num_rows() > 0) {
$row = $query->row();
foreach ($row as $key => $val) {
$this->$key = $val;
}
return $row;
}
return null;
}
function uuid()
{
return strtoupper(substr(dechex(time()).dechex(mt_rand(1,65535)), 0, 6));
}
function add($data)
{
return $this->db->insert("tv_user", $data);
}
function update($data, $where)
{
return $this->db->update("tv_user", $data, $where);
}
function delete($where)
{
return $this->db->delete("tv_user", $where);
}
function login($username, $password)
{
if (empty($username) || empty($password)) {
return FALSE;
}
$password = md5($password);
$sql = "SELECT * FROM tv_user WHERE username='{$username}' AND password='{$password}' AND active=1";
$query = $this->db->query($sql);
if ($query->num_rows() > 0) {
$user = $query->row();
$user_data = array();
foreach($user as $key => $val){
$user_data[$key] = $val;
}
$user_data["login"] = TRUE;
$this->session->set_userdata('user', $user_data);
if ($user->user_type == USR_ROOT) {
$this->session->set_userdata('root_addmin_logged_in', TRUE);
$this->session->set_userdata('addmin_logged_in', TRUE);
}
else if (in_array($user->user_type, array(USR_ADMIN,USR_MODERATOR,USR_TOUR,USR_HOTEL,USR_FLIGHT,USR_VISA))) {
$this->session->set_userdata('root_addmin_logged_in', FALSE);
$this->session->set_userdata('addmin_logged_in', TRUE);
}
else {
$this->session->set_userdata('root_addmin_logged_in', FALSE);
$this->session->set_userdata('addmin_logged_in', FALSE);
}
$this->session->set_userdata('logged_in', TRUE);
$this->session->set_userdata('logged_user', $user);
return TRUE;
}
return FALSE;
}
function logout()
{
$this->session->sess_destroy();
}
function verify_reset_password_code($email, $code)
{
if (empty($email) || empty($code)) {
return FALSE;
}
$sql = "SELECT * FROM tv_user WHERE email = '{$email}' ";
$query = $this->db->query($sql);
if ($query->num_rows() > 0) {
$row = $query->row();
return ($code == md5(SITE_NAME.$row->fullname))? TRUE : FALSE;
} else {
return FALSE;
}
}
}
?>
Set
short_open_tag=On
in php.ini
And restart your Apache server.
As per the comments:
Your first chunk of PHP is appearing in your HTML view - good idea to include a screenshot! That would indicate that it is not being parsed as PHP, and instead is being parse as HTML, so I would check to see if a <?php appears at the start.
Can someone tell me how to return the number of results if I use this function to grab data from the database? I have tried including this:
$this->number = $result->num_rows;
But that didn't do the trick. Also, if anyone can give me some advice to do the below code in a better way, that would be helpful too.
<?php
public function grabResults($table, $values = '*', $where = NULL, $field1 = NULL, $and = NULL, $field2 = NULL, $order = NULL)
{
$result = 'SELECT '.$values.' FROM '.$table;
if($where != NULL)
{
$result = 'SELECT '.$values.' FROM '.$table.' WHERE '.$field1.' = '.$where;
}
if($and != NULL)
{
$result = 'SELECT '.$values.' FROM '.$table.' WHERE '.$field1.' = '.$where.' AND '.$field2.' = '.$and;
}
if($order != NULL)
{
$result = 'SELECT '.$values.' FROM '.$table.' WHERE '.$field1.' = '.$where.' ORDER BY '.$order.' ASC';
}
$query = $this->data->mysqli->query($result);
if($query)
{
while($row = $query->fetch_assoc())
{
$rows[] = $row;
}
return $rows;
}
else {
return false;
}
}
?>
$result->num_rows;
sounds like a Codeigniter function that you tried to use in raw php.
Have you tried to count() the query?
$count=count($query);
I am using the following method to query from my SQL database:
function query() {
global $link;
$debug = false;
//get the sql query
$args = func_get_args();
$sql = array_shift($args);
//secure the input
for ($i=0;$i<count($args);$i++) {
$args[$i] = urldecode($args[$i]);
$args[$i] = mysqli_real_escape_string($link, $args[$i]);
}
//build the final query
$sql = vsprintf($sql, $args);
if ($debug) print $sql;
//execute and fetch the results
$result = mysqli_query($link, $sql);
if (mysqli_errno($link)==0 && $result) {
$rows = array();
if ($result!==true)
while ($d = mysqli_fetch_assoc($result)) {
array_push($rows,$d);
}
//return json
return array('result'=>$rows);
} else {
//error
return array('error'=>'Database error');
}
}
$result = $result = query("SELECT * FROM users WHERE email='$email' limit 1");
$name = (what goes here?)
I am trying to get the string name from users, how can I do this?
If your query is right then
try this:
function query() {
global $link;
$debug = false;
//get the sql query
$args = func_get_args();
$sql = array_shift($args);
//secure the input
for ($i=0;$i<count($args);$i++) {
$args[$i] = urldecode($args[$i]);
$args[$i] = mysqli_real_escape_string($link, $args[$i]);
}
//build the final query
$sql = vsprintf($sql, $args);
if ($debug) print $sql;
//execute and fetch the results
$result = mysqli_query($link, $sql);
if (mysqli_errno($link)==0 && $result) {
$rows = array();
if ($result!==true)
while ($d = mysqli_fetch_assoc($result)) {
array_push($rows,$d);
}
//return json
return array('result'=>$rows);
} else {
//error
return array('error'=>'Database error');
}
}
$result = query("SELECT * FROM users WHERE email='$email' limit 1");
$name = $result['result'][0]['name'];
You forgot to pass the value to the function
function query($sql) {
global $link;
$debug = false;
//get the sql query
$args = func_get_args();
$sql = array_shift($args);
//secure the input
for ($i=0;$i<count($args);$i++) {
$args[$i] = urldecode($args[$i]);
$args[$i] = mysqli_real_escape_string($link, $args[$i]);
}
//build the final query
$sql = vsprintf($sql, $args);
if ($debug) print $sql;
//execute and fetch the results
$result = mysqli_query($link, $sql);
if (mysqli_errno($link)==0 && $result) {
$rows = array();
if ($result!==true)
while ($d = mysqli_fetch_assoc($result)) {
array_push($rows,$d);
}
//return json
return array('result'=>$rows);
} else {
//error
return array('error'=>'Database error');
}
}
$result = $result = query("SELECT * FROM users WHERE email='$email' limit 1")
$name = "";
if(! isset($result["error"]) and isset($result["name"])) // check it returns error or not. then check name field exist or not.
{
$name = $result["name"];
}
echo $name;
i am beginner in php and codeigniter.can anyone make this php programme as a codeigniter format?
if ($user_id > 0){
$follow = array();
$fsql = "select user_id from following
where follower_id='$user_id'";
$fresult = mysql_query($fsql);
while($f = mysql_fetch_object($fresult)){
array_push($follow, $f->user_id);
}
if (count($follow)){
$id_string = implode(',', $follow);
$extra = " and id in ($id_string)";
}else{
return array();
}
if ($user_id > 0)
{
$follow = array();
$qry = "select user_id from following where follower_id='$user_id'";
$res = $this->db->query(qry);
foreach($res->result() as $row)
{
array_push($follow, $row->user_id);
}
if (count($follow))
{
$id_string = implode(',', $follow);
$extra = " and id in ($id_string)";
}
else
{
return array();
}
}
Try this:
if ($user_id > 0){
$follow = array();
$rs = $this->db->select('user_id')->where('follower_id', $user_id)->get('following')->result_array();
if( is_array( $rs ) && count( $rs ) > 0 ){
$follow = array();
foreach( $rs as $key => $each ){
$follow[] = $each['user_id'];
}
}
if (count($follow)){
$id_string = implode(',', $follow);
$extra = " and id in ($id_string)";
}else{
return array();
}
}
This should be done in the model.
Using avtice record you can do it like this
function getFriends($user_id){
return $this->db
->select('user_id')
->where('follower_id',$user_id)
->get('following')
->result_array() // for single object use row_array()
}