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

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

Related

How to deal with NULL value in Codeigniter?

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
}

issue of undefined offset in generating referance no

i am stuck in a code, which is working fine but not all the time, it works for 4 transactions after that it shows an undefined offset error, which i can not figure out why it is occurring after running for four times, here is the code
<?php
$Selectlatest=mysqli_query($connection,"Select * from pos_order order by transaction_id desc limit 0,1");
$row_Selectlatest=mysqli_fetch_array($Selectlatest);
if(mysqli_num_rows($Selectlatest)==0){
$num = 001;
$invoice_no = date('dmy').$num;
}
$new = str_split($row_Selectlatest['invoice_no'],6);
$invoice_no= date('dmy').$new[1]+1 ;
?>
it works fine but after 4 consecutive transaction it shows an error which is
Notice: Undefined offset: 1 in C:\xampp\htdocs\Point_of_sale\admin\cart.php on line 60
How to solve it or any other way of generating the invoice or kind of referance no, which increases after checking the previous number from database?

Get the last id before insert ZF2

I am trying to implement an action that can get me the last inserted id before I insert a record.
The action is basically supposed to get the last inserted id, then i add 1 to it then the value will be used in the current data been inserted.
This how far I have gotten and the error am getting
//the action to get the last inserted id
public function getLastID(){
$lastcourseid = $this->tableGateway->select(function (Select $select){
$select->columns(array('id'));
$select->order('id ASC')->limit(1);
});
var_dump($lastcourseid);
return $lastcourseid;
}
I call the function here before saving
if($id == 0){
$data['course_code'] = $this->getLastID();
$this->tableGateway->insert($data);
}else{
if($this->getAlbum($id)){
$this->tableGateway->update($data, array('id' => $id));
}else{
throw new \Exception("Form id does not exist");//an error is thrown in case the id is not found
}
}
This is the error am getting
Catchable fatal error: Object of class Zend\Db\ResultSet\ResultSet
could not be converted to string
I do not know where am going wrong.
Any help will be appreciated. Thanks
There is no such thing like "last id before insert".
And you don't need it.
First insert a record and then get your id. This is how it works.
You do not mention the underlying database (postgressql, mysql, etc.). I am more familiar with MySQL. The Perl and php AIP's have "last row id" functions. For example, php has "mysqli_insert_id()". This assumes that your rowID is an AUTO_INCREMENT column. Other DBs may have different requirements.

MySQL , PHP : Hide specific Value from table

I am working on a ' Show Online user' script where the script show everybody who is online.
Now i want to remve the entry which matches the session user name i.e "if (Online user = Session User name ) then do not display it , just like on facebook.com chat where your friends id is shown and not your own Id
my code is as follows :
<?php
mysql_connect("localhost","root","12345");
mysql_select_db("accounts");
$user = $_SESSION['user_name'];
$result = mysql_query("SELECT * FROM online * WHERE ($chat<>$user)");
while($row=mysql_fetch_array($result)) {
$chat=$row["emp_name"];
$chlk = ("<a href=javascript:void(0) onclick=javascript:chatWith('$chat')>$chat</a>");
$chs = ("<a>$chat</a>");
if ($chat <> $user) {
echo $chlk;
}
else {
echo $chs;
}
echo $chlk;
}
?>
I am getting the following error :
Notice: Undefined variable: chat in localhost/accounts/removeuser.php on line 7
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given localhost/accounts/removeuser.php on line 9
Any help is highly appreciated.
Correction in query.
"SELECT * FROM online WHERE ($chat<>$user)"
OR
Replace $chat in query with your table field name.
there is extra * before WHERE that is invalid.
and $chat is not defined in query.
<> is not PHP as far as I know. You need to use !==.
(and probably read up on some PHP basics...)

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