Not able to get data from url in codeigniter - php

I have below query http://localhost/barbadosparliament/result/index?qry=testing
Now i want ot get data of qry parameter from url. But not able to get data. I used $this->uri->segment and also $_GET['qry']. But still i doesn't get that record. How can i get that record.

check you have on in config/config.php
$config['allow_get_array'] = TRUE;
then try
$qry = $this->input->get('qry', TRUE);
$qry = $_GET['qry'];
$qry = $_REQUEST['qry'];
Also try url like
http://localhost.com/barbadosparliament/result?qry=testing

try this:
$qry = $this->input->get('qry');
But this should works too
$qry = $_GET['qry'];

Related

Trying to get property of non-object (PHP CodeIgniter)

My code just for save the data session.
This is my code:
$idfb = 12121983918 // just sample id.
$sql = $this->db->query('SELECT * from user WHERE facebookid = "'.$idfb.'" LIMIT 1');
$datalogin = $sql->row();
$loginsession= array(
'jenis_user' =>$datalogin->jenis_user, // the problem is here, i got eror in here, the errors is Trying to get property of non-object .
'photo'=>$datalogin->photo,
'facebookid'=>$datalogin->facebookid,
'id'=>$datalogin->id,
'email'=>$datalogin->email,
'username'=>$datalogin->username,
'nama'=>$datalogin->nama,
// 'jenis_user'=>$datalogin->jenis_user,
'alamat'=>$datalogin->alamat,
'no_telpon'=>$datalogin->no_telpon,
);
$this->session->set_userdata('loginsession',$loginsession);
redirect($this->agent->referrer());
I hope someone can solved this problem. Thanks! :)
It seems you are going right
Please double check your sql, make sure that you are using right table and column names.
and try replacing
$sql = $this->db->query('SELECT * from user WHERE facebookid = "'.$idfb.'" LIMIT 1');
with
<?php
$this->db->where('facebookid', $idfb);
$this->db->limit(1);
$sql = $this->db->get_where('user');
?>
if same error, try echo $sql->num_rows() to see if you are getting row or not.
Since database structure is not given, I can assume the database query is returning 0 rows. Always try to handle exception.
$idfb = 12121983918; // just sample id.
$sql = $this->db->query('SELECT * from user WHERE facebookid = "'.$idfb.'" LIMIT 1');
if($sql->num_rows()>0){
$datalogin = $sql->row();
$loginsession= array(
'jenis_user' =>$datalogin->jenis_user, // the problem is here, i got eror in here, the errors is Trying to get property of non-object .
'photo'=>$datalogin->photo,
'facebookid'=>$datalogin->facebookid,
'id'=>$datalogin->id,
'email'=>$datalogin->email,
'username'=>$datalogin->username,
'nama'=>$datalogin->nama,
// 'jenis_user'=>$datalogin->jenis_user,
'alamat'=>$datalogin->alamat,
'no_telpon'=>$datalogin->no_telpon,
);
$this->session->set_userdata('loginsession',$loginsession);
redirect($this->agent->referrer());
}
else
{
Your Code
}

php mysqli will not get results

I cant reslly fidn the answer to this anywhere as its quite unique to my situation.
I have the following code
$accountid = "%%GLOBAL_accountcustomer%%";
echo $accountid;
$results = $mysqli->query("SELECT * FROM exhibitor_list WHERE companyid='$accountid' ");
When I echo $accountid I get the right the id from the database. (in this case number 1)
But when trying to use $accountid in the WHERE query it displays nothing.
If i manualy change the the WHERE query to
WHERE companyid='1'
It displays the row I want to display.
I have also tried stripping $accountid of any whitespace to see if it helps but it doesnt.
Any help appreciated
In your question
but when trying to use $accountid in the WHERE query it displays nothing.
Simply it means No Data passing to this $accountid
so if $accountid is empty below query will not work
$results = $mysqli->query("SELECT * FROM exhibitor_list WHERE companyid='$accountid' ");
When you use query function, it cant be easier to debug your sql ;)
For example:
$sql = "SELECT * FROM exhibitor_list WHERE companyid='$accountid' ";
// $results = $mysqli->query($sql);
echo $sql; die();
And you have your error. You dont parse %%GLOBAL_accountcustomer%% so dont use it.

Codeigniter second database connection query without object

I am having trouble using my second database connection is codeigniter.
I have added the connection to database.php
$old = $this->load->database('old_portal', TRUE);
$sql = "SELECT * FROM `frm_root`";
$query = $this->old->query($sql);
But I am getting an error. I am not sure how to use the old object when wanting to use query()
Using multiple databases in CodeIgniter is pretty easy.
https://ellislab.com/codeIgniter/user-guide/database/connecting.html
You would do it like this;
$old = $this->load->database('old', true);
Then, you would access this database object, like this;
$query = $old->query();
Hope this helps.
$query = $old->query($sql);

PHP/mysql fetch multiple variables in array

I am new to PHP. I wanted to create a new record in another table but just one new variable gets returned. I've tried following:
$user_id = mysql_real_escape_string($_POST['user_id']);
$user_name = mysql_query("SELECT user_name FROM accept WHERE user_id=".$user_id." ");
$row1 = mysql_fetch_array($user_name);
$server = mysql_query("SELECT server FROM accept WHERE user_id=".$user_id." ");
$row2 = mysql_fetch_array($server);
$url = mysql_query("SELECT link FROM accept WHERE user_id=".$user_id."");
$row3 = mysql_fetch_array($url);
$lpoints = mysql_real_escape_string($_POST['lpoints']);
And my result is this.
First of all, combine your queries into one:
$user_id = mysql_real_escape_string($_POST['user_id']);
$user_info = mysql_query("SELECT user_name, server, link FROM accept WHERE user_id=".$user_id." ");
$row = mysql_fetch_array($user_info);
$lpoints = mysql_real_escape_string($_POST['lpoints']);
In order to create a new record, you will need INSERT INTO, to change existing records use UPDATE.
When you're fetching info from the database, it will be an array so you will need to use it accordingly. So essentially, to use the variables it will be like this:
$row['user_name'] or $row['server'] etc..
Also, look into using mysqli instead. You will need to change your connection script and some other syntax but it needs to be done. mysql is deprecated, insecure, and future support is not there so you will need to change it later anyway.
You should use pdo or mysqli and here is your code;
$user_id = &$_POST["user_id"];
if($user_id){
$result = mysql_query("select user_name,server,link,lpoints from accept where user_id='".mysql_real_escape_string($user_id)."'");
/*You should use single quotes for escaping sql injection*/
if($result){
$vars = mysql_fetch_array($result);
if($vars){
list($username,$server,$link,$lpoints) = $vars;
}
else{
//do something with errors
}
mysql_free_result($result);
}
else{
//do something with errors
}
}
else{
//do something with errors
}
Try This-
$user_id = mysql_real_escape_string($_POST['user_id']);
$result = mysql_query("SELECT user_name, server, link FROM accept WHERE user_id=".$user_id." ");
$row=mysql_fetch_array($result)
$row1=$row['user_name'];
$row2=$row['server'];
$row3=$row['link'];
$lpoints = mysql_real_escape_string($_POST['lpoints']);
Now you got what you wanted based on your requirement use the data to insert or update.

PHP get result string from PostgreSQL Query

I'm new to PHP and SQL, but I need a way to store the result of an SQL Query into a variable.
The query is like this:
$q = "SELECT type FROM users WHERE username='foo user'";
$result = pg_query($q);
The query will only return one string; the user's account type, and I just need to store that in a variable so I can check to see if the user has permission to view a page.
I know I could probably just do this query:
"SELECT * FROM users WHERE username='foo user' and type='admin'";
if(pg_num_rows($result) == 1) {
//...
}
But it seems like a bad practice to me.
Either way, it would be good to know how to store it as a variable for future reference.
You can pass the result to pg_fetch_assoc() and then store the value, or did you want to get the value without the extra step?
$result = pg_query($q);
$row = pg_fetch_assoc($result);
$account_type = $row['type'];
Is that what you are looking for?
Use pg_fetch_result:
$result = pg_query($q);
$account_type = pg_fetch_result($result, 0, 0);
But on the other hand it's always good idea to check if you got any results so I'll keep the pg_num_rows check.

Categories