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.
Related
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
}
I have a MySQL query which is this:
SELECT * FROM tbl_post WHERE tbl_post.post_id NOT IN
(SELECT tbl_readsave.post_id FROM tbl_readsave)
I want to convert it into Codeigniter Active records, so I used the following code segment:
$this->db->select('tbl_readsave.post_id');
$queryReadSave = $this->db->get('readsave');
$this->db->where_not_in('post_id', $queryReadSave->result_array());
$queryNewPost = $this->db->get('readsave');
if($queryNewPost->num_rows()>0)
{
return $queryNewPost->result_array();
}
else
return false;
However, the code throws me an error, which is like the following:-
Error Number: 1054
Unknown column 'Array' in 'where clause'
SELECT * FROM (`tbl_readsave`) WHERE `post_id` NOT IN (Array)
Filename: /var/www/html/teamF/tharjumal/models/webservice_model.php
Line Number: 28
How can I convert the above stated query into Codeigniter Active Records format?
$queryReadSave->result_array() returns an array of arrays. You can't use that in where_not_in.
You need to loop over that and create a flat array of the IDs you (don't) want.
$this->db->select('post_id');
$queryReadSave = $this->db->get('readsave');
$postIDs = $queryReadSave->result_array();
$this->db->where_not_in('post_id', array_column($postIDs, 'post_id'));
$queryNewPost = $this->db->get('post');
array_column() only exists in PHP 5.5+. If you are on a lower version, you'll need to do something like this:
$this->db->select('post_id');
$queryReadSave = $this->db->get('readsave');
$postIDs = array_map(function($a){
return $a['post_id'];
}, $queryReadSave->result_array());
$this->db->where_not_in('post_id', $postIDs);
$queryNewPost = $this->db->get('post');
P.S. Your second table is called post, right? You'll need to update the query to use the right table.
Update: Your original query uses a subquery which is not natively supported by CodeIgniter. If you want to try this all as one query, you can use the Subquery library I created (https://github.com/NTICompass/CodeIgniter-Subqueries).
$this->db->select('*');
$this->db->from('post');
$sub = $this->subquery->start_subquery('where_in');
$sub->select('post_id')->from('readsave');
$this->subquery->end_subquery('post_id', FALSE);
$queryNewPost = $this->db->get();
try below code:
$read_post_id = [];
$queryReadSave = $this->db->select('post_id')->get('tbl_readsave')->result_array();
if(count($queryReadSave) > 0){
foreach($queryReadSave as $row){
$read_post_id[] = $row['post_id']; // add each post id to the array
}
}
$this->db->select('*');
if(!empty($read_post_id)) $this->db->where_not_in('post_id',$read_post_id);
$post = $this->db->get('tbl_post');
print_r($post->result_array());
exit;
I have a problem here in NotORM code.
This code are working well:
$select = $db->pspaym->select("COUNT(*)")->where("F4","$textdate")->fetch();
$count = count($select);
But this code here does not working:
$select = $db->pssale->select("COUNT(*)")->where("F8","$textdate")->fetch();
$count = count($select);
This code have an error message said:
"Trying to get property of non-object"
cannot resolve this problem.
all variables are not null.
thanks.
if you want to count lines in a table, simply use:
$select = $db->pspaym->where("F4","$textdate");
$count = count($select);
The problem comes from your combination of fetch() and count() methods. Because of the ending fetch call, $select doesn't contain a Table object, but a Record object.
So count($select) will count the number of columns in your record. Normally, it should always returns 1 (because one field in the returned record).
For your information, if you want to be uselessly explicit, you may do something like this.
$record = $db->pspaym("F4","$textdate")->select("COUNT(*) AS c")->fetch();
$count = $record['c'];
But, that's the long way.
I'm new to joomla jdatabase. Currently I having a error code on my query in joomla which I using sourcerer plugin to insert the code.
Here is my code:
$campusID = $_POST['campusID'];
$courseID = $_POST['courseID'];
// Get default database object
$db =JFactory::getDBO();
// Get a new JDatabaseQuery object
$query = $db->getQuery(true);
$tmpIds = array();
foreach($courseID as $cId){
$tmpIds = $db->quote($cId); //sanitize the input
}
$courseID1 = explode($tmpIds, ",");
// Build the query
$query->select($db->quoteName('courseid'));
$query->from($db->quoteName('intake'));
$query->where($db->quoteName('campusid').'='. $db->quote($campusID));
$query->where($db->quoteName('courseid').'IN('.$courseID1.')');
// Set the query for the DB oject to execute
$db->setQuery($query);
// Get the DB object to load the results as a list of objects
$results = $db->loadObjectList();
if($results){
echo 'Good';
}
else{ echo 'Error';}
Apparently, the courseID is an array that post by other form and it was using a check box. But somehow maybe my logic thinking is bad, I couldn't found anyway to compare the courseID with the database courseID to determine whether the courseID is existing in the database or not, else it might just prompt the error message.
Here is what error message I get from joomla when I am trying to execute it.
Error 1054. Unknown column 'Array' in 'where clause' SQL=SELECT `courseid` FROM `intake` WHERE `campusid`='Campus2' AND `courseid`IN(Array)
Try this
first of all you have a change of
$tmpIds = $db->quote($cId);
To
$tmpIds[] = $db->quote($cId);
after you want to use a implode()
$courseID1 = explode($tmpIds, ",");
To
$courseID1 = implode(',',$tmpIds);
This code works and enters all of the correct information into the database but the error check returns an error. I could remove the error check but I'm afraid of creating some nightmare that comes back to haunt me later or that I'm missing a fundamental issue:
$sql5a = mysql_query("SELECT id FROM categories WHERE category='$category'");
$categoryresult = mysql_fetch_array($sql5a);
$oldcategoryid = $categoryresult['id'];
$sql6a = "INSERT INTO lookupcategory SET
fbid='$fbid2',
categoryid='$oldcategoryid'";
if ( #mysql_query($sql5b) ) {
echo('sql5b updated successfully<br>');
} else {
echo('Error: sql5b not updated<br>'.mysql_error() );
}
if ( #mysql_query($sql6b) ) {
echo('sql6b updated successfully<br>');
} else {
echo('Error: sql6b not updated<br>'.mysql_error() );
}
The output is: "Error: sql5b not updated
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #7' at line 1"
"sql6b updated successfully"
When I check the database all entries are correct. If sql5a didn't work, sql6b couldn't work, hence my confusion over the error.
A sample Category would be: Travel/Leisure
The category was originally created from a form response:
$category = htmlentities($fbdetail['category'], ENT_QUOTES);
and entered into the database successfully. An id number was assigned using AUTO_INCREMENT.
You assign query to variable $sql5a, but call #mysql_query($sql5b).
$sql5b doesn't exist (at least in this sample). Same with $sql6a...
You can use INSERT syntax without VALUES, but you need to ommit INTO keyword.
$sql5a = mysql_query("SELECT id FROM categories WHERE category='$category'");
if ( $res5a = #mysql_query($sql5a) ) { // first execute query and store resource in variable
echo('sql5a selected successfully<br>');
} else {
echo('Error: sql5a failed<br>'.mysql_error() );
}
$categoryresult = mysql_fetch_array($res5a); // fetch array passing the RESOURCE var, NOT query string
$oldcategoryid = $categoryresult['id'];
$sql6a = "INSERT lookupcategory SET
fbid='$fbid2',
categoryid='$oldcategoryid'";
if ( #mysql_query($sql6a) ) {
echo('sql6a inserted successfully<br>');
} else {
echo('Error: sql6a failed<br>'.mysql_error() );
}
I don't know where you get $fbid2 or $category from, since it's not in this piece of code.
The syntax for the insert is :
INSERT INTO table(col1, col2 ...) VALUES(val1, val2 ...)
In your specific case:
$sql6a = "INSERT INTO table(fbid, categoryid) VALUES('{$fbid2}','{$oldcategoryid}')"