Object of Class Mysqli_Result Could Not Be Converted to String

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>";
}

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'];
}

PHP/SQL - Object of class mysqli_result could not be converted to string

Try fetching your mysqli_result, at the place of using directly in the query

$my_id_query=mysqli_query($con,"SELECT `id` FROM `users` WHERE (`username`='$myusername')");
//Fetch result
$my_id_array=mysqli_fetch_assoc($my_id_query);
$my_id=$my_id_array['id'];
//Cast this into int to protect yourself against sql injection
$user=(int)$_GET['user'];
$check_conv=mysqli_query($con,"SELECT `hash` FROM `message_g` WHERE (`user_one`='$my_id' AND
`user_two`='$user') OR (`user_one`='$user' AND `user_two`='$my_id')");
if(mysqli_num_rows($check_conv) == 1){

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();

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;
}

PHP and MySQL error: Object of class mysqli_result could not be converted to string

You cannot directly output the result of a query. Use:

$sql = ("select sum(`memory`) AS memTotal from `server`");
// Show used memory
$result = mysqli_query($con, $sql);
echo $result->fetch_object()->memTotal;

The $result variable holds an object (of type mysqli_result), from which you can fetch the scalars you need to output.



Related Topics



Leave a reply



Submit