Object of class mysqli_result could not be converted to string
The mysqli_query()
method returns an object resource to your $result
variable, not a string.
You need to loop it up and then access the records. You just can't directly use it as your $result
variable.
while ($row = $result->fetch_assoc()) {
echo $row['classtype']."<br>";
}
PHP/MYSQL Error : Object of class mysqli_result could not be converted to string
$var
in your code is actually an object of type mysqli_result
.
This is actually a handle to a resultset, and you have to unload the results from that handle using some sort of fetch
.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
// dont normally get ID's of zero so I chnaged this to 1
$result = $conn->query("SELECT Name FROM users WHERE ID=1");
// now get the result row
$row = $result->fetch_assoc(); // row will be an array
?>
Now in your HTML you can do this
<html>
<head>
<title>Title</title>
</head>
<body>
<?php include 'pehape.php' ?>
<span>Username:</span><?php echo $row['Name'];?>
</body>
</html>
Object of class mysqli_result could not be converted to number (PHP | MySQL)
I updated your SQL query to alias the aggregation for easy access.
$qty = $db->query("SELECT SUM(`qty`) as 'val_sum' FROM `transaction` WHERE `date` = '2021-04-01'");
You can cast a string to an int with "(int) $string_value"
if ($qty->num_rows > 0) {
$row = $qty->fetch_assoc()
$profit = (int) $row['val_sum'] * 10;
}
mySQL Object of class mysqli_result could not be converted to string problems
you should fetch the result.
http://php.net/manual/en/mysqli-result.fetch-array.php
That is,
$row = $result->fetch_array(MYSQLI_NUM);
var_dump($row);
You could see all the rows as array.
Your code will be like this.
<?php
// Create connection
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// sql to create table
$url = 'http://google.com';
$ip = $_SERVER['REMOTE_ADDR'];
$sql = "
SELECT ID FROM urls
WHERE url LIKE ('http://google.com')
";
$result = $conn->query($sql);
$row = $result->fetch_array(MYSQLI_NUM);
echo $row[0] . " " . $row[1];
$conn->close();
How do you convert a mysqli_result into a string?
Since you limit the selection to one entry use
$row = mysqli_fetch_array($result);
echo $row['ImageURL'];
If you select more than one entry loop over the result.
while($row = mysqli_fetch_array($result)) {
echo $row['ImageURL'];
}
Error converting SQL result to string
You use variable $result
both to concatenate string$result .= "<div class='post modelVariant'>".$value."</div>";
and for storing result from MySQL query $result = $query->get_result()
.
Related Topics
Fatal Error: Call to Undefined Function Mysqli_Connect()
Laravel 6: Call to Undefined Method App\\User::Createtoken()
Nginx Error Recv() Failed (104: Connection Reset by Peer)
How to Deal With Json That Is Encoded Twice
How to Use Find in Set for User Rankings Based on Score
Using Maatwebsite Excel 3.1 Export How to Set Background Color for Cells and Font Size for Heading
How to Stop My PHP Form Resubmitting Input After Page Refresh
Showing Image Binary Data Using JavaScript
How to Resolve MySQL Port 3306 Error on Wamp
Combining 'Where' and 'Like' Statements by Using the Ci Activerecords
How to Get Toggle Button Working With PHP
Display Text(Html) Saved in Database Using Ckeditor
How to Unlink (Delete) an Image in Codeigniter
How to Change Default Url from Localhost:8000 to Other Ip in Laravel When We Run "Php Artisan Serve"
Checking If Email Is in MySQL Database in PHP
Codeigniter: Fatal Error Call to Undefined Function Mysqli_Init()