This question already has answers here:
Error Exception Trying to access array offset on value of type null [closed]
(2 answers)
Closed 2 years ago.
I'm building a social media web app, and while I can successfully display posts to a feed, I also get these error messages:
Notice: Trying to access array offset on value of type null in /opt/lampp/htdocs/social/includes/classes/User.php on line 34
Notice: Trying to access array offset on value of type null in /opt/lampp/htdocs/social/includes/classes/User.php on line 37
These error messages repeat a number of times, and are displayed on the feed:
Here's the code that the error messages point to:
public function getFirstAndLastName() {
$username = $this->user['username']; //ERROR line 34
$query = mysqli_query($this->con, "SELECT first_name, last_name FROM users WHERE username='$username'");
$row = mysqli_fetch_array($query);
return $row['first_name'] . " " . $row['last_name']; // ERROR line 37
}
And here's my constructor for a user:
public function __construct($con, $user){
$this->con = $con;
$user_details_query = mysqli_query($con, "SELECT * FROM users WHERE username='$user'");
$this->user = mysqli_fetch_array($user_details_query);
}
The database has values for the first and last names, and they're written in the same format. Why am I getting a null value even though I'm able to post to the app?
It's not in array (and actually notice is pretty clear about that). Your $this->user is null which means you need to rather check where you set it and why it did not work (or maybe you try to access it too early, before it's set?).
Related
This question already has an answer here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 2 years ago.
<?php
$queryuser = "SELECT id, email, firstname, lastname, address, city, country FROM users WHERE role = 'client'";
$resultuser = $connection->query($queryuser);
if ($resultuser->num_rows > 0)
{
// output data of each row
while ($rowuser = $resultuser->fetch_assoc())
{
$id_user = $rowuser['id'];
$firstname = $rowuser['firstname'];
$lasttname = $rowuser['lastname'];
$email = $rowuser['email'];
$phone = $rowuser['phone'];
$city = $rowuser['city'];
$state = $rowuser['state'];
$address = $rowuser['address'];
?>
Perhaps you've copied code from this page https://www.w3schools.com/php/php_mysql_select.asp
Please understand code line by line first. You have not define $connection variable in your code (may be it is a intentional approach for security).
Your notice saying that you are trying to get num_rows property from the object $resultuser but in this query $resultuser is not a object due to any error/faults of your query because query() returns FALSE on failure and object on success.
To confirm, you can check your result set by using var_dump();
Please replace this line if $resultuser->num_rows > 0 with (!empty($resultuser->num_rows) && ($resultuser->num_rows > 0)))
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 5 years ago.
enter image description hereI make a one function to fetch the data from database in php.I used the return value of another function for select the id from the database.But when i store worker id into array i get the error "Trying to get property of non-object" like this.What i do?I wan to store match id according to query into one array.
public function getWorkers()
{
$db = JFactory::getDBO();
$orderid=$this->getTodayOrder();
$workersId=array();
foreach($orderid as $workers1)
{
$query2 = "SELECT * FROM #__orderassignment WHERE orderid='".$workers1."'";
$db->setQuery($query2);
$result1 = $db->loadObjectList();
$workersId[]=$workers1->workers;
}
return $workersId;
}
if array is given then you need to access it like this
$workersId[]=$workers1['workers'];
This question already has answers here:
Object of class mysqli_result could not be converted to string
(5 answers)
Closed 1 year ago.
There is some data is stored in my sql table. I want to fetch the data in the form of string.
$sql= "SELECT hash FROM signupinfo WHERE fname = 'nikhil';";
$hashfix = mysqli_query($connection,$sql);
echo $hashfix;
but the error coming
Catchable fatal error: Object of class mysqli_result could not be converted to string in C:\WampDeveloper\Websites\localhost\webroot\signup.php on line 48
var_dump($hashfix). If you want to read your array all Data fetched from a db come in the form of array
Its not a good idea to echo array...by that I mean you can't. What you can do in to make a string is. $a=implode(',',$hashfix)
There are 2 ways to print values you want
If its a single key array.
echo $hashfix[0]->ColumnName
Or run a foreach
You could do something like this...
$sql= "SELECT `hash` FROM `signupinfo` WHERE `fname` = 'nikhil';";
$hashfix = mysqli_query( $connection, $sql );
if( $hashfix ){
while( $rs=$connection->fetch_object( $hashfix ) ){
echo $rs->hash;
}
}
This question already has answers here:
Fatal error: Array callback has to contain indices 0 and 1
(2 answers)
Closed 12 months ago.
I'm getting an error saying
Fatal error: Array callback has to contain indices 0 and 1 in
C:\xampp\htdocs\series\databases\index.php on line 10
Here's the little code that I have. I know I am not actually pulling anything from the database but can't go any further until I resolve this issue.
<?php
require 'connect.inc.php';
$query = "SELECT `food`, `calories` FROM `food` ORDER BY `id`";
//$query_run = mysql_query($query);
if ($query_run = mysql_query($query)){
while ($query_row = mysql_fetch_assoc($query_run)) {
$food = $query_row('food'); //Fatal error message for this line
$calories = $query_row('calories'); //When I comment out line 10 this gives an error too
}
} else {
echo mysql_error();
}
?>
If you want any more info let me know, I was following this tutorial that may be out of date.
So yeah, any ideas on how to resolve this? Thanks.
$query_row['food'];
You are using the wrong kind brackets, square brackets access array items.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result
I am getting this error while trying to run a function. I am sending a value $id to this function and trying to return the associated array. But I get a warning supplied argument is not a valid MySQL-Link resource.
But if i run the same code by eliminating the function part and give a static value to $id it returns the result correctly. This is the function
<?php
mysql_select_db($database_spyware, $spyware);
function get_product($id)
{
$query = sprintf("select product.descr as product_descr, product_id as Product_id,
category.descr as category from sp_url
inner join product
on product.id = sp_url.product_id
inner join category
on product.category_id = category.id
where sp_url.id = $id");
$query_result = mysql_query($query, $spyware) or die(mysql_error());
$row_rs_query = mysql_fetch_assoc($query_result);
$totalRows_rs = mysql_num_rows($query_result);
return($row_rs_query);
}
?>
That happens because your second parameter spyware in function "mysql_query" is declared outside the function and is unreadable on your function scope.
try
global $spyware;
at the beginning of the function.
or leave the second parameter empty.