This question already has an answer here:
How to fix Trying to access array offset on value of type null error
(1 answer)
Closed 1 year ago.
I am trying to use table data in a single row data as the value of an variable in my code below and i keep getting "Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\done\test1.php on line 29"
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT lev1 FROM ref where id=1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "lev1: " . $row["lev1"]. "";
}
}
$mamo=$row["lev1"];
$conn->close();
what I'm I dong wrong ?
Trying to access array offset on value of type null
It means that $row is null (in the $mamo=$row["lev1"]; line), therefore it cannot behave as an array.
If you're using while ($row = $result->fetch_assoc()), it will keep assigning values to $row until it can no more (because that's supposed to break the loop). After you reach the last result, $row will always inevitably be null.
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 4 years ago.
I've spent the past 2 hours trying to solve this one error. I am a complete rookie so I dont know what's going on. Here's the code, please help:
<?php
header('Access-Control-Allow-Origin: *');
$host="localhost"; // Host name
$username="id11111_ab"; // Mysql username
$password="*****"; // Mysql password
$db_name="id11111_cd"; // Database name
$tbl_name="ef"; // Table name
// Connect to server and select database.
$link = mysqli_connect($host, $username, $password, $db_name);
// Retrieve data from database
$sql = "SELECT * FROM scores ORDER BY score DESC LIMIT 10";
$result = mysqli_query($link,$sql);
// Start looping rows in mysql database.
while($rows=mysqli_fetch_array($result)){
echo $rows['name'] . "|" . $rows['score'] . "|";
// close while loop
}
?>
mysqli_query() returns false if it fails. Subsequently, the mysqli_fetch_array() function is being passed this false boolean value, on which it can't operate. You'd be wise to check the value that mysqli_query() returns isn't false prior to attempting to retrieve the resource. E.g.:
$result = mysqli_query($link,$sql);
if (!$result) {
die('Query failed');
}
It seems like the mysql connection is not established properly.
Check for errors using this:
if(mysqli_connect_errno()){
echo mysqli_connect_error();
}
I'm trying to echo a phone number from my database using php but it keeps returning blank. I've been fiddling around with it all night and looking at various examples but I've not been able to get it to work. Can someone help me please?
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = 'SELECT phone FROM users WHERE ID = 29' ;
$results = mysqli_query($conn, $sql);
$row = mysqli_fetch_row($results);
echo $row['phone'];
Since your using mysqli_fetch_row it returns result enumerated array which means it returns the result row array staring from 0 offset.
Reference : http://php.net/manual/en/mysqli-result.fetch-row.php
ie.
$row = mysqli_fetch_row($results);
//Now $row columns can be accessed by $row[0] $row[1] and so on.
On the other hand if you use mysqli_fetch_assoc then the result will be returned as associative array ie key value pairs
Reference : http://php.net/manual/en/mysqli-result.fetch-assoc.php
ie.
$row = mysqli_fetch_assoc($results);
//Now you can use $row as $row['phone'], $row['id'] and so on
At the moment, I'm trying to store a value from a MySQL table as a variable in PHP, so running some basic tests to make sure that I can access the variable.
I've managed to store the varaible, which will either be a 1 or a 0 (1 = server is up and running, 0 = server down).
<?php
$servername = "localhost";
$username = "*****";
$password = "*****";
$dbname = "scicomservers";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT nccpm FROM web_servers WHERE time_checked='2016-02-16 11:44:17.212126'";
$nccpm = $conn->query($sql);
if($nccpm==1){
echo("NCCPM Server is running");
}
$conn->close();
?>
When I run this code, it reads in that $nccpm is 1, and it echos the statement, however, I get the error:
Notice: Object of class mysqli_result could not be converted to int in
/Applications/XAMPP/xamppfiles/htdocs/SciCom_admin_servers/files/connect2.php
on line 17
Line 17 being the if statement: "if($nccpm==1){".
I've had a look around on here, and I think this may be because it is trying to print an array of the answers, however it will only ever be one value that I will retrieve. The column of the DB is an int.
I was wondering, what would be a better way of coding this? It clearly isn't the best practice!
Thank you very much.
$sql = "SELECT nccpm FROM web_servers WHERE time_checked='2016-02-16 11:44:17.212126'";
$ncc = $conn->query($sql);
$nccpm = $conn->fetch_array($ncc);
if ($nccpm['nccpm'] == 1)
{
// Rest of script
}
You need to fetch your query or find how many rows are returned
This question already has answers here:
Object of class mysqli_result could not be converted to string
(5 answers)
Closed 1 year ago.
When I am echo the $result variable. It display an error msg like this:
Catchable fatal error: Object of class mysqli_result could not be converted to string in D:\Inspiration\server\Table2_yahooData_db.php
on line 13
<?php
// connect to the dataBase
$conn = mysqli_connect('localhost', 'root', '', 'yahooData');
//checking Connection.
if (!$conn) {
die('Error connection failed: ' . mysqli_connect_error($conn));
}
// Creating select query Using sql commands.
$sql = "SELECT * FROM User;" ;
$result = mysqli_query($conn, $sql);
echo $result;
mysqli_close($conn);
?>
$result is an object.
You cannot print it with string's echo function.
You can rather print it with print_r($result);
echo is for scalar variables (which have single value or are single dimensions) like number, string. For multi-dimensional variables e.g. array, objects we need to use print_r() which prints the whole tree.
When using a SELECT query the mysqli_query() function returns a resource, not a string. You need to use mysqli_fetch_assoc() to put the results into an array. Here is an object-oriented example:
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$query = "SELECT name FROM User";
if ($result = $mysqli->query($query)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
echo $row['name'];
}
/* free result set */
$result->free();
}
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 8 years ago.
I keep getting this message. I am simply trying to retrieve my table data from a sql db
the line that is giving me the problem is:
if ($result->num_rows > 0) {
In the code:
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$prodlist = "SELECT item number, item description FROM product";
$result = $conn->query($prodlist);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Item Number: " . $row["item number"]. " - Item Description: " . $row["Item Description"]."<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
</body>
</html>
IF you did actually have a table field called item number you would have to quote it so : item number with backtick "`" characters hence you'd have to do the same in your query.