Undefined Variable in PHP? [duplicate] - php

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 6 years ago.
This is my function:
public function showDataall($result)
{
$q = $this->conn->prepare($result) or die("failed!");
$q->execute();
while ($r = $q->fetch(PDO::FETCH_ASSOC))
{
$data[] = $r;
}
return $data;
}
This function perfectly work in old xampp but new xampp return a Notice:
Undefined variable: data in /opt/lampp/htdocs/live/demo/model/config.php on line 152

Declare variable before using it :
If your query returns no data, your current code will never actually create the $data array, and therefore when you try and return it, this error will happen.
public function showDataall($result)
{
$q = $this->conn->prepare($result) or die("failed!");
$q->execute();
$data = array();
while ($r = $q->fetch(PDO::FETCH_ASSOC))
{
$data[] = $r;
}
return $data;
}

Related

Laravel/PHP error: Undefined variable: email2 [duplicate]

This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 3 years ago.
i was trying to send an email and when i want to whrite the email address of the destination (the email is a parameter of the function), i have this error. Can someone help me.
this is my function:
public function email($name, $email2) {
$data = array('name'=> $name);
Mail::send('mail',$data,function($message) {
$message->to($email2, $name)->subject('Deposit Confirmation');
$message->attach(public_path('document.pdf'));
$message->from('xxx#xxx.pt', 'XXX');
});
}
this is my error: "Undefined variable email2"
thank you
When you use anonymous function to pass variables you should use use construction
public function email($name, $email2) {
$data = array('name'=> $name);
Mail::send('mail',$data,function($message) use ($name, $email2) {
$message->to($email2, $name)->subject('Deposit Confirmation');
$message->attach(public_path('document.pdf'));
$message->from('xxx#xxx.pt', 'XXX');
});
}
Here you can find more examples about anonymous functions https://www.php.net/manual/en/functions.anonymous.php
Here simple example from official documentation
$message = "hello";
// Inherit $message
$example = function () use ($message) {
var_dump($message);
};
$example();
// Output: string(5) "hello"

undefined variable when Adding Dynamic Data to the View from database query in codeigniter [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 4 years ago.
I have made an admin page and user page, and i want to show the list of users who have registered in database when the admin logs in.
For that i've created a model as follows,
public function get_last_ten_entries()
{
$query = $this->db->query("SELECT username FROM public");
return $query->result();
}
and this i am accessing through a view i have created, to which, when admin logs in, he is redirected, as follows,
<h1><?php echo $data;?></h1>
through controller,
$this->load->model('loginmodel');
$login_id = $this->loginmodel->login_valid($username, $password);
if($login_id){
$this->load->library('session');
$this->session->set_userdata('user_id','$login_id');
$data['query'] = $this->loginmodel->get_last_ten_entries();
$this->load->view('admin/account', $data);
}
but when i execute this i get,
A PHP Error was encountered Severity: Notice
Message: Undefined variable: data
Filename: admin/account.php
Line Number: 11
Just use $query instead of $data in the view
MODEL
public function get_last_ten_entries()
{
$query = $this->db->query("SELECT username FROM public");
return $query->result();
}
CONTROLLER
$this->load->model('loginmodel');
$data['query'] = $this->loginmodel->get_last_ten_entries();
$login_id = $this->loginmodel->login_valid($username, $password);
if($login_id){
$this->load->library('session');
$this->session->set_userdata('user_id','$login_id');
$data['query'] = $this->loginmodel->get_last_ten_entries();
$this->load->view('admin/account', $data);
}
VIEW
<?php
foreach($query as $row){
echo $row['name'];
}
?>

PHP CLASS Undefined variable (But it is defined) [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 5 years ago.
I just can't figure out why this error is even showing up:
Notice: Undefined variable: authTime in /.../classname.class.php on line 33
class ClassName {
private $authTime = null;
const API_URL = '...';
const CLIENT_ID = '...';
const CLIENT_SECRET = '...';
private static $TOKEN = NULL;
public function __construct() {
$this->_authTime = $authTime; // <----- Line 33
if(!self::$TOKEN OR $this->AuthTime('GET') > 3600 OR !$this->_authTime) {
self::authorise();
}
}
public function getAuthTime() {
return $this->_authTime; // Returns NULL
}
I see $authTime is not defined within the constructor. I think you want to do:
$this->_authTime = $this->authTime;

Cannot spot the issue with PHP Index error-possible multiple threads issue [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
I'm struggling to find the problem with the following piece of PHP code:
The code is as follows
class WorkerThreads extends Thread
{
private $from_list;
public function __construct($x,$host,$users_email,$pass,$inbox)
{
$this->from_list = array(); # holds the unique froms extracted from headers
}
public function run()
{
# Get Froms
if (preg_match('/From\:\ (.+)/i', $headers, $matches, PREG_OFFSET_CAPTURE)) {
$from = trim(str_ireplace("From: ", "", $matches[0][0]));
if (!array_key_exists($from, $this->from_list)) {
$this->from_list[$from] = 1;
echo "<br/>FROM: ".$from."-".$this->from_list[$from]."<br/><br/>";
}
} else {
echo "NO FROM <br/><rb/>";
}
The following error occurs:
Notice: Undefined index: Viva in /var/www/BAMCode/yahoofroms.php on line 198
FROM: Viva-
The offending line 200 is
echo "<br/>FROM: ".$from."-".$this->from_list[$from]."<br/><br/>";
There seems to be a problem with the array
This will work perfectly fine if you don't use multiple threads.
I think many threads are trying to modify and read the same list. Try synchronizing the modification/read of the from_list. We may use a mutex to synchronize read/write to the from_list.
Check the code below:
<?php
class WorkerThreads extends Thread
{
private $from_list;
private $mylock;
public function __construct($x,$host,$users_email,$pass,$inbox)
{
$this->from_list = array(); # holds the unique froms extracted from headers
$this->mylock = Mutex::create();
}
public function __destruct()
{
Mutex::destroy($this->mylock);
$this->mylock = null;
}
public function run()
{
# Get Froms
if (preg_match('/From\:\ (.+)/i', $headers, $matches, PREG_OFFSET_CAPTURE)) {
$from = trim(str_ireplace("From: ", "", $matches[0][0]));
Mutex::lock($this->mylock);
if (!array_key_exists($from, $this->from_list)) {
$this->from_list[$from] = 1;
echo "<br/>FROM: ".$from."-".$this->from_list[$from]."<br/><br/>";
}
Mutex::unlock($this->mylock);
} else {
echo "NO FROM <br/><rb/>";
}
}
}
?>

view file application/views//view..php not found [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
public static function view($name, array $vars = null){
if(preg_match('/\\\\/', $name)){
$view_data = explode('\\', $name);
if(count($view_data) == 3)
$file = APP_PATH.DS.'views'.DS.$view_data[0].DS.$view_data[1].DS.'view.'.$view_data[2].'.php';
else
$file = APP_PATH.DS.'views'.DS.$view_data[0].DS.'view.'.$view_data[1].'.php';
}
else{
$file = APP_PATH.DS.'views'.DS.'view.'.$name.'.php';
}
if(!is_readable($file)){
throw new Exception('view file application'.DS.'views'.DS.$view_data[0].DS.'view.'.$view_data[1].'.php not found.');
}
else{
if(isset($vars)){
extract($vars);
}
require($file);
}
}
[21-Apr-2015 13:10:30 UTC] PHP Notice: Undefined variable: view_data in /home/realitycards/public_html/test/system/load.class.php on line 28
your variable $view_data only gets defined in the first if statement. It looks like in the if statement below that you are using $view_data even though it hasn't been set.
if(!is_readable($file)){
throw new Exception('view file application'.DS.'views'.DS.$view_data[0].DS.'view.'.$view_data[1].'.php not found.');
}
You either need to set $view_data in your else statement, or in the exception above, use the $file variable that you've already set:
if(!is_readable($file)){
throw new Exception('view file '. $file .' not found.');
}

Categories