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
}
Related
I'm attempting to set a cookie containing a post ID, but only when the current post ID isn't present in a table. The code works, but I'm getting a PHP warning "Undefined offset: 0" from wp-includes/wp-db.php and I'm wondering what I've done wrong.
function getFromDatabase() {
global $wpdb;
$sql = $wpdb->prepare( "SELECT page_id FROM foo WHERE page_id IS NOT NULL" );
$result = $wpdb->get_results( $sql );
$resultsArray = array();
foreach ( $result as $row ) {
$resultsArray[] = $row->page_id;
}
return $resultsArray;
}
...
$currentPID = get_the_id();
$ignoredPIDs = getFromDatabase();
if ( !in_array($currentPID, $ignoredPIDs) ) {
// Set a cookie to post ID
}
I should mention that the DB query will always return at least one result. Any thoughts on this would be greatly appreciated.
Update - Found the problem, it was the call to $wpdb->prepare. It's just a guess, but nothing needed preparing, everything is hard-coded, and perhaps it was complaining about that.
Without being able to see what's in getFromDatabase:
I know you said that the DB query will always return at least one result but just for the heck of it, try to check for empty. If this works then your problem is obviously in the getFromDatabase function.
$currentPID = get_the_id();
$ignoredPIDs = getFromDatabase();
if(!empty($ignoredPIDs)) {
if ( !in_array($currentPID, $ignoredPIDs) ) {
// Set a cookie to post ID
}
}
So, I am trying to get the IMDb id for the from the table called videos. I tried it with these these are giving me error.
Code 1:
$imdb_id = $this->db->get_where('videos', array('imdbid'))->result_array();
Code 2:
$imdb_id = $this->db->get('videos','imdbid');
code 3:
$query = $this->db->query("SELECT * FROM videos;");
$row = $query->row(0, 'videos');
$imdb_id = $row['imdbid'];
Here is a screenshot of database table. Thank you for your contribution in advance.
Please explain the error and output expectations.
you want only one return data or multiple data?
This Codeigniter 3 right?
the current code you are using is incorrect :
Code 1 :
// will get only one data with where condition
// videos_id = 1
$video_id = 1;
$data = $this->db->get_where('videos', array('videos_id'=>$video_id))->result_array();
$imdb_id = $data[0]['imdbid'];
Code 2 :
// will get all data without where condition
// must use loop foreach
$data = $this->db->get('videos')->result_array();
I am terrible at PHP and I need to retrieve data from a database and give it to an index.php view. The view is pre-made and has this code:
//This is simplified - it has error handling that is not shown
$results = getAll($tableName);
//This is the line where it is failing
//Undefined Offset
$columns = empty($results) ? array() : array_keys($results[0]);
$idColumn = $columns[0];
There is all the rest of it but I just need to know what on earth it is that this bit of code is expecting. I have not even got the first clue what is supposed to be sent to this thing. I just need to get it to work.
This is what I have tried so far:
function getAll($tablename)
{
$mysqlConnection = getDbConnection();//Just the normal PDO db connection
$sql = "SELECT * FROM ".$tablename;
$sth = $mysqlConnection->prepare($sql);
$sth->execute();
$resultSet = $sth->fetch(PDO::FETCH_ASSOC);
return $resultSet;
}
I have tried various different PDO::FETCH_... types but nothing is working. There is no information about what it is that I am supposed to send that part of the view.
If you want all the rows from fetch(), you will need to loop through the result set because it will return a single row. In the loop you can place them in an array.
You can use fetchAll() instead. It will return all the results as an array.
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.
Here is my code:
$child = $this->getDetails($row['ParentOf_Id']);
which is in another function that calls a function called getdetails:
function getDetails($child_id=null,$limit=20){
// select all the fields in the children table
$this->db->select(castedColumns($this->children_table, $this->db));
//where the id is the child id
$q = $this->db->get_where($this->children_table, 'id='.$child_id);
//store results array in details
$details = $q->row_array();
I then get the following SQL error:
A Database Error Occurred
Error Number:
Incorrect syntax near '='.
SELECT CAST(AdditionalNotes AS TEXT) as AdditionalNotes, CAST(DisabilitiesNotes AS TEXT) as DisabilitiesNotes, DOB, msrepl_tran_version FROM Children WHERE id=
Id does not get appended? why?
Been driving me nuts because, when I do echo $child_id; I get the child Id before passing it into the SQL.
try:
$this->db->select(castedColumns($this->children_table, $this->db));
$q = $this->db->get_where($this->children_table, array('id' => $child_id));
$details = $q->row_array();
I would guess that $child_id is null, otherwise it would show in the db error query. Try to print $child_id and see if it's really a null which would mean that
$child = $this->getDetails($row['ParentOf_Id']);
$row['ParentOf_Id'] is null.