i create my own function with count of data.
Here is my function.
function Test($data){
global $acc;
$count = $acc->get_var("SELECT Count(*) FROM _users WHERE ID='$data'");
if($count == 0)
{
return true;
}else{
return false;
}
}
But i got This is error : "PHP Catchable fatal error: Object of class stdClass could not be converted to string
In This is Line $count = $acc->get_var("SELECT Count(*) FROM _users WHERE ID='$data'");
I try var_dump in function and Other ezSQL methods (query,num_rows) but i got every same error.
--
Edit:
Problem solved. I wrong posted $data
Please make sure that
$data
is not an array
its better practice to pass $acc by reference
function Test($data, &$acc){
$count = $acc->query("SELECT Count(*) FROM _users WHERE ID='$data'");
if($count == 0)
{
return true;
}else{
return false;
}
}
Related
$query = $this->db->select('password')->get_where('members', array('email' => $this->input->post('email')));
if ($query->num_rows() == 1) {
if (password_verify($this->input->post('password'), $query->row(1))) {
return TRUE;
} else {
return FALSE;
}
} else {
return FALSE;
}
I'm using the above code to verify user input password with hash from the database, and I'm using CodeIgniter which has the built in password_compat to make the functions work for versions less than 5.5.
The above should work, but I keep getting this error:
Severity: Warning Message: strlen() expects parameter 1 to be string,
object given Filename: compat/password.php Line Number: 220
What could be the issue here?
Thanks!
You need to get query result in variable and this is wrong use of
$query->row(1);
It would be
$rows=$query->row();
$rows->password;
check below code
$query = $this->db->select('password')->get_where('members', array('email' => $this->input->post('email')));
if ($query->num_rows() == 1) {
$rows=$query->row();// here you assign result to $rows variable
Check here how you pass your password variable as parameter
if (password_verify($this->input->post('password'), $rows->password)) {
return TRUE;
} else {
return FALSE;
}
} else {
return FALSE;
}
Read Generating Query Results
Simply change $query->row(1) to $query->row(0)->password
I am trying this code:
My Code snippet
public function beforeFind($query) {
$query = parent::beforeFind($query);
if(!isset($query['conditions'])){
$query['conditions'] = array();
}
if(!$this->Authake->isAdmin()){
if(empty($query['conditions']) || is_array($query['conditions'])){
$query['conditions'] = array('organ_id' => $this->Organ->Group->getUserBranches());
}
}
return $query;
}
But I am getting this error
Error
Warning (2): Cannot use a scalar value as an array [APP\Model\Ticket.php, line 56]
Warning (2): Cannot use a scalar value as an array [APP\Model\Ticket.php, line 56]
As said by commentor that $query is not an array, and beforeFind is returning boolean. So the following line is having problem.
$query = parent::beforeFind($query);
The correction is
There is no use of calling parent method as it is overridden it to implement own logic.
so the correct code is
public function beforeFind($query) {
if(!isset($query['conditions'])){
$query['conditions'] = array();
}
if(!$this->Authake->isAdmin()){
if(empty($query['conditions']) || is_array($query['conditions'])){
$query['conditions'] = array('organ_id' => $this->Organ->Group->getUserBranches());
}
}
return $query;
}
I'm trying to find out why I'm getting a Trying to get property of non-object. I'm not completely skilled with objects and arrays but i'm trying. Here's the code and the error message. Any ideas on how to fix this issue?
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: models/sitemodel.php
Line Number: 208 and 215
function getSubMenuPages()
{
$this->db->select('site_menu_structures.id');
$this->db->from('site_menu_structures');
$this->db->where('site_menu_structures.short_name', 'mainnav');
$query = $this->db->get();
$menu_id = $query->row()->id;
$this->db->select('site_menu_structures_links.id, site_menu_structures_links.short_name, is_category');
$this->db->from('site_menu_structures_links');
$this->db->where('site_menu_structures_links.menu_structure_id', $menu_id);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
$linksArray = $query->result();
foreach ($linksArray as $key => $link)
{
if ($link->is_category == 'Yes')
{
$linksArray->{$key}->child_links;
$this->db->select('site_menu_structures_links_children.link_name');
$this->db->from('site_menu_structures_links_children');
$this->db->where('site_menu_structures_links_children.site_menu_structures_links_id', $link->id);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
$linksArray->{$key}->child_links = $query->result();
}
}
}
}
return $linksArray;
}
My guess would be $linksArray is an array, not an object so the line
$linksArray->{$key}->child_links;
will not work. In any case, this line does nothing so why have it at all?
Where you assign a value to this "property", try this instead
$linksArray[$key]->child_links = $query->result();
"Trying to get property of non-object"
this type of errors only exist if you try to treat a variable as an object instance and you actually failed to create that instance successfully try to check this part of the code if this is really an object or not:
$linksArray->{$key}->child_links
this is on the bottom of your code.
I'm trying to get a database query which is an object converted to an associative array, so that I can use it in the calendar class in codeigniter.
This is my model:
<?php
class Get_diary_model extends Model {
function getAllDiaries($year,$month) {
$data = $this->db->query("SELECT day AND entry FROM diary WHERE month=$month AND year=$year"); // the entries for the relevant month and year
foreach($data->result_array() as $row) { // return result as assoc array to use in calendar
echo $row['day'];
echo $row['entry'];
}
return $data;
}
}
and this is the error I get:
atal error: Cannot use object of type CI_DB_mysql_result as array in C:\wamp\www\mm\system\libraries\Calendar.php on line 219
Any ideas?
Check ou this video tutorial, it will help you -> http://net.tutsplus.com/tutorials/php/codeigniter-from-scratch-the-calendar-library/
Your model should look like this:
function getAllDiaries($year,$month)
{
$q = $this->db->query("SELECT day AND entry FROM diary WHERE month=$month AND year=$year");
if($q->num_rows() > 0):
foreach($q->result() as $row):
$data[] = $row;
endforeach;
return $data;
else:
return false;
endif;
}
and your controller:
function index($year = null, $month = null)
{
$this->load->model('Get_diary_model');
if (!$year) {
$year = date('Y');
}
if (!$month) {
$month = date('m');
}
$data['calendar'] = $this->Get_diary_model->getAllDiaries($year, $month);
}
The problem was not in your use of result_array(), more that you later return $data directly. $query = $this->db->query() then use $query->result_array() in the foreach. Then you can return $data after building it up in the foreach.
The other answer is a long-winded way of writing the following:
function getAllDiaries($year,$month)
{
$sql = "SELECT day AND entry FROM diary WHERE month=$month AND year=$year";
return $this->db->query($sql)->result();
}
But of course that will return an array of objects, not an multidimensional array.
Use below simple method,
$query = $this->db->get_where('table', array('table_id' => $id));
$queryArr = $query->result();
foreach ($queryArr[0] as $key=>$val)
{
$row[$key]=$val;
}
print_r($row); //this will give associative array
Here is the solution for CodeIgniter-3
function getAllDiaries(){
$query = $this->db->query("YOUR QUERY HERE");
return $query->result('array');
}
OR
function getAllDiaries(){
return $this->db->query("YOUR QUERY HERE")->result('array');
}
Note: result() function accept "array" or "object" as parameter. Default is "object"
I was trying to return a set of objects.
But this code gives me the following error:
Catchable fatal error: Object of class User could not be converted to string in ...
public function fetchObject($psClassname ="",$paParams =array()){
$lrResource = $this->mrQueryResource;
$liResult = null;
while($row = mysql_fetch_object($lrResource,$psClassname,$paParams)){
$liResult .= $row; <-this line produces the error
}
return $liResult;
}
In your code $row is a an object (you've used mysql_fetch_object), and the .= operator tries to build a string, concatenating $liResult and $row. I believe this behaviour only works if your object implements a toString method
You could return an array of rows using this code:
public function fetchObject($psClassname ="",$paParams =array()){
$lrResource = $this->mrQueryResource;
$liResult = array();
while($row = mysql_fetch_object($lrResource,$psClassname,$paParams)){
$liResult[] = $row;
}
return $liResult;
}
That's because you are trying to convert $row to a string (the .= assumes a string is given on the right hand side)