How to deal with NULL value in Codeigniter? - php

Tried almost dozen of thing with Mysql function (IFNULL,COALESCE,if condition etc) and PHP but nothing work for me. it simple query I want 0 (zero) output if there is no fines against employee. Query works fine when I add emp_id in table(emp_fines).
$query_str="SELECT SUM(amount) AS fines FROM `emp_fines`
WHERE MONTH(P_date) = MONTH(CURDATE()) AND emp_id ='".$em_cod."'
GROUP BY emp_id";
$query=$this->db->query($query_str);
$record=$query->row();
if (is_int($record->fines))
echo "0";
else
echo $record->fines;
using codeigniter, it gives below error!!
Severity: Notice Message: Trying to get property of non-object Filename: backend/dashboard.php eLine Number: 350

Thanks for #Akina for helping me. Finally below code works
$query_str="SELECT SUM(amount) AS fines FROM `emp_fines` WHERE MONTH(P_date) = MONTH(CURDATE()) AND emp_id ='".$em_cod."'"; //echo $query_str;
$query=$this->db->query($query_str);
$record=$query->row();
if ($record->fines===NULL)
echo "0";
else
echo $record->fines;

it should use
if(is_null($record->fines)){
// codes Here
}

Related

PHP Notice: Trying to get property 'message' of non-object

and thank you for dropping in to help me.
currently i am trying to bring an old mafia game up to date for my own personal use as no one uses them now, and am stuck using objects which i have never used before.
The error i get is :
Notice: Trying to get property 'message' of non-object in C:\wamp64\www\mafia\Send.php on line 67
Here is the code
$query=mysqli_query($connection,"SELECT * FROM messages WHERE f='$ownusername' ORDER by id DESC LIMIT 3");
$info = mysqli_fetch_object($query);
$fetch=mysqli_fetch_object(mysqli_query($connection,"SELECT * FROM autospam LIMIT 1"));
if ($info->message == $message && $fetch->c == 1){
mysqli_query($connection,"UPDATE users SET mute='1', mutedby='Spamming' WHERE username='$ownusername'");
echo "You have been muted!";
}
This has been fixed thanks to Dmitry, for pointing me in the right direction, i found that the id in the sql insert was the error, where it was trying to insert an empty value into an auto increment table.
I removed the ID from the sql insert and all is working perfectly

Undefined offset error, but the offset exists

I want to make a list of all my users, so I used the following SQL query:
"SELECT Username FROM inloggen"
which works perfectly fine when I run it in phpMyAdmin and when I use
$list = mysql_fetch_array(mysql_query("SELECT Username FROM inloggen")); in php,
echo "<p> $list[0] </p>"; correctly returns the first user, but echo "<p> $list[1] </p>"; and higher all return an Undefined offset error, even though I have 6 users in my database.
Does anyone know why this happens?

PHP count row returns error

i'm using the following to count the number of rows in my table:
$book_count = query("SELECT status FROM scifi_book WHERE read = $uid");
(count($book_count));
echo $book_count;
and i get the following error:
Notice: Array to string conversion on line 167
(which is the line echo $book_count;)
FIY, the query function is defined and works fine. Never had any issues to insert, select, update and delete.
Am I missing something here?
Try this:
$book_count = query("SELECT status FROM scifi_book WHERE read = $uid");
echo count($book_count);
Also, you need to use print_r($book_count) since your $book_count is not a string.
Suggestion: if you only use that query for getting the count, this may be a bit better:
$book_count = query("SELECT count(*) FROM scifi_book WHERE read = $uid");
your query function seems to return an array, not a string. Instead of echo $follower_count use print_r($follower_count) to see what's inside the query response.
The reason you are seeing that error is because you are echoing Array which is the returned by your query function. echo construct only works on strings, please see the documentation here: http://www.php.net/manual/en/function.echo.php.
Had you used print_r or var_dump then you wouldn't have seen that error. So as #A.S. Roma and Nathaniel Granor suggest use print_r
$book_count = query("SELECT status FROM scifi_book WHERE read =".$uid);
(count($book_count));
echo $book_count;

table doesnt exist

I have created a query that loops through a group of table ID's to get a sum of their combined values. with error handling i get a "Table 'asterisk.custom_' doesn't exist" error and the query is killed obviously. but if i remove the error handling then i get an "mysql_fetch_array() expects parameter 1 to be resource" and the query completes as it should.
Thank in advance for your help.
include("currentday.php");
//---used for testing passing variables
//echo $customID[0];
//echo "<br>".$numrows;
$i = 0;
while($i<=$numrows)
{
mysql_select_db("asterisk") or die(mysql_error()); //This line determines the database to use
$query = "SELECT
vicidial_users.user,
vicidial_users.full_name,
sum(vicidial_agent_log.pause_sec) as sumPause,
sum(custom_$customID[$i].d_amt) as sumDamnt,
sum(custom_$customID[$i].up_amt) as sumUpamnt,
sum(custom_$customID[$i].md_amt) as sumMdamnt,
sum(custom_$customID[$i].s_amount) as sumSamnt,
sum(vicidial_agent_log.dispo_sec)
FROM
vicidial_agent_log
INNER JOIN
vicidial_users
ON
(vicidial_agent_log.user = vicidial_users.user)
INNER JOIN
custom_$customID[$i]
ON
(vicidial_agent_log.lead_id = custom_$customID[$i].lead_id)
WHERE
vicidial_users.user = 'tcx'
GROUP BY
vicidial_users.full_name
ORDER BY
vicidial_agent_log.event_time DESC
";
$queryResult = mysql_query($query);// or die(mysql_error());
while ($rowResult = mysql_fetch_array($queryResult))
{
$pauseResult[] = $rowResult["sumPause"];
$sumdamntResult[] = $rowResult["sumDamnt"];
$sumupamntResult[] = $rowResult["sumUpamnt"];
$summdamntResult[] = $rowResult["sumMdamnt"];
$sumsamntResult[] = $rowResult["sumSamnt"];
}
//print_r($pauseResult);
//echo $pauseResult[0];
$i++;
}
Update:
The table exist in the database:
custom_2346579543413
custom_5466546513564
they are created by the dialer software and im calling them from another query that provides me the numeric part of the table name so this query loops through the values in customID array to make the query, Thanks again
Update:
Sammitch, thank you for the suggestion, however they did not work.
Solution:
Thanks Marc, you confirmed a suspicion i had in that it was looping correctly but for some reason it was looping more times that there we keys. so i echoed $i to confirm and in fact it was it was outputting 0,1,2,3 and since i know there is only 3 keys the last one didn't return anything and so error handling caught it and killed the entire loop and why it appeared correct when error handling was turned off. The solution was actually rather simple and it was in the while loop evaluation string i had used
while($i<=$numrows)
while($i<$numrows)//this worked, the equals part of that gave it an extra loop
and the query completes as it should.
No, it doesn't. "mysql_fetch_array() expects parameter 1 to be resource" means "the query failed, and you didn't bother to check before calling mysql_fetch_array()".
Your problem is that variable expansion inside of a string doesn't like array indexes. You need to change:
"sum(custom_$customID[$i].d_amt) as sumDamnt,"
To:
"sum(custom_{$customID[$i]}.d_amt) as sumDamnt,"
Or:
"sum(custom_" . $customID[$i] . ".d_amt) as sumDamnt,"
For it to work properly.

How do I count the rows in a table in the CodeIgniter framework?

I'm trying to count the number of row in a table. The table has a fk 'city_id'. I want to count the number of the row that meets certain criteria and want to echo the number.
I've tried with the following code:
function count(){
$citys = $this->db->get('city');
foreach ($citys->result() as $city) {
$this->db->where('info', array('city_city_id'=>$city->city_id) AND status_status_id==1);
$sql = $this->db->count_all('info');
}
return $sql->result;
}
Controller:
$data['city'] = $this->state_model->count();
$this->load->view('sview', $data);
View:
<?php foreach($citys as $cities):?>
<h4><?php echo $city ?>
<?php endforeach;?></br></br>
In my model i'm trying to count the num of rows where lets say, city_city_id=1 and status_status_id=1 in my 'info' table.
But i'm getting the following error :
Severity: Notice
Message: Use of undefined constant status_status_id - assumed 'status_status_id'
Filename: models/State_model.php
Line Number: 98
In line 98 i have
$this->db->where('info', array('city_city_id'=>$city->city_id) AND status_status_id==1);
i'm newly working with codeigniter so a little help would be appreciated.
Thanks in advance.
This should get you going:
$this->db->where('city_id', $city_id);
$this->db->from('city');
return $this->db->count_all_results();
Returns an integer. CodeIgniter Active Record
You can’t use SQL queries in the middle of PHP, that’s why AND breaks it. You don’t need to join the queries with AND, CodeIgniter does that for you.
your where should go like
$this->db->where('info', array('city_city_id'=>$city->city_id,
'status_status_id'=>'1'));

Categories