Object of class mysqli_result could not be converted to string - php

I am getting the error:
Object of class mysqli_result could not be converted to string
This is my code:
$result = mysqli_query($con, "SELECT classtype FROM learn_users WHERE username='abcde'");
echo "my result <a href='data/$result.php'>My account</a>";

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

Before using the $result variable, you should use $row = mysqli_fetch_array($result) or mysqli_fetch_assoc() functions.
Like this:
$row = mysqli_fetch_array($result);
and use the $row array as you need.

mysqli:query() returns a mysqli_result object, which cannot be serialized into a string.
You need to fetch the results from the object. Here's how to do it.
If you need a single value.
Fetch a single row from the result and then access column index 0 or using an associative key. Use the null-coalescing operator in case no rows are present in the result.
$result = $con->query($tourquery); // or mysqli_query($con, $tourquery);
$tourresult = $result->fetch_array()[0] ?? '';
// OR
$tourresult = $result->fetch_array()['roomprice'] ?? '';
echo '<strong>Per room amount: </strong>'.$tourresult;
If you need multiple values.
Use foreach loop to iterate over the result and fetch each row one by one. You can access each column using the column name as an array index.
$result = $con->query($tourquery); // or mysqli_query($con, $tourquery);
foreach($result as $row) {
echo '<strong>Per room amount: </strong>'.$row['roomprice'];
}

The query() function returns an object, you'll want fetch a record from what's returned from that function. Look at the examples on this page to learn how to print data from mysql

Try with:
$row = mysqli_fetch_assoc($result);
echo "my result <a href='data/" . htmlentities($row['classtype'], ENT_QUOTES, 'UTF-8') . ".php'>My account</a>";

Related

how to retrieve value only of JSON by php from database?

may I know how to retrieve the value only array in json instead of the whole json object in php from the database?
<?php
require_once('dbConnect.php');
$sql = "SELECT RestaurantName FROM Restaurant";
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));
$restaurantArray = array();
while($rows = mysqli_fetch_assoc($result)) {
$restaurantArray[] = $rows;
}
echo json_encode($restaurantArray);
mysqli_close($connection);
?>
For example,
["Afonso Cláudio", "Água Doce do Norte"]
instead of
[{"city":"Afonso Cláudio"},{"city":"Água Doce do Norte"}]
When fetching data from the database as assoc, you will get an associative array (which when converted to json will become a json object) as result.
What you are currently doing is adding the row (which is an associative array with one key-value) to your result array. If you only want the city, you can easily fetch only that data from the assoc-array.
Changing
$restaurantArray[] = $rows;
to
$resturantArray[] = $rows['city'];
should do the trick.
Additionally, if you wanted to reformat the data in the result, and not just push the city name to it, a array_map call could be used:
$result = array_map(function($assocArr) {
return $assocArr['city']; // Or whatever you want the value to be.
},
$resturantArray);
echo json_encode($result);
How about you change this line:
$restaurantArray[] = $rows;
With:
$restaurantArray[] = $rows['city']; // (or 'RestaurantName')

Object of class mysqli_result could not be converted to string on mybb [duplicate]

I am getting the error:
Object of class mysqli_result could not be converted to string
This is my code:
$result = mysqli_query($con, "SELECT classtype FROM learn_users WHERE username='abcde'");
echo "my result <a href='data/$result.php'>My account</a>";
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>";
}
Before using the $result variable, you should use $row = mysqli_fetch_array($result) or mysqli_fetch_assoc() functions.
Like this:
$row = mysqli_fetch_array($result);
and use the $row array as you need.
mysqli:query() returns a mysqli_result object, which cannot be serialized into a string.
You need to fetch the results from the object. Here's how to do it.
If you need a single value.
Fetch a single row from the result and then access column index 0 or using an associative key. Use the null-coalescing operator in case no rows are present in the result.
$result = $con->query($tourquery); // or mysqli_query($con, $tourquery);
$tourresult = $result->fetch_array()[0] ?? '';
// OR
$tourresult = $result->fetch_array()['roomprice'] ?? '';
echo '<strong>Per room amount: </strong>'.$tourresult;
If you need multiple values.
Use foreach loop to iterate over the result and fetch each row one by one. You can access each column using the column name as an array index.
$result = $con->query($tourquery); // or mysqli_query($con, $tourquery);
foreach($result as $row) {
echo '<strong>Per room amount: </strong>'.$row['roomprice'];
}
The query() function returns an object, you'll want fetch a record from what's returned from that function. Look at the examples on this page to learn how to print data from mysql
Try with:
$row = mysqli_fetch_assoc($result);
echo "my result <a href='data/" . htmlentities($row['classtype'], ENT_QUOTES, 'UTF-8') . ".php'>My account</a>";

PHP, mySQLi: store the value of an array element into a variable

I have an SQL query which returns a single row as a result. This row contains only one column, an integer. I want to put this integer into a variable.
I execute my query and return the results of the query in an array as such:
$row = mysqli_fetch_all($result, MYSQLI_ASSOC);
I would like to save the value of the single column of this single row into a variable, for example $age. How can this be done?
(This query will always return one row with one column, always an integer)
mysqli_fetch_all doesn't return a row. But set of rows.
$rows = mysqli_fetch_all($result, MYSQLI_ASSOC);
$value = reset($rows[0]);
but it would be more logical to use a more suitable function:
$row = mysqli_fetch_row($result);
$value = $row[0]; // here you go.
Since you have only one column and your query returns only one row, that means you'll get only one value, and since you're fetching an associative array, you can do the following and store it in a variable.
$row = mysqli_fetch_all($result, MYSQLI_ASSOC);
foreach ($row as $r) {
$test = $r['age'];
}
However, since you're getting only one value out of that, you can just leave the result type out, and get $test = $r[0].

How do I return data when doing specific MySQL query using PHP?

Here is my PHP MySQL query:
$query = "SELECT falsegoto FROM timeconditions WHERE [timeconditions_id] = 0";
$result = mysql_query($query);
There should be only a single result from this query, and I'm not sure how display it in PHP?
mysql_result() seems to only work with larger data sets?
Any help or explanation would be valued.
As Peeha mentiod, your using mysql, but it's better to use mysqli
So the code will then look like this:
$query = "SELECT falsegoto FROM timeconditions WHERE [timeconditions_id] = 0";
$result = mysqli_query($query);
while($row = myslqi_fetch_assoc($result){
// DO STUFF
}
I use this for everything. It just loops through every row in the result. and if there's just one row, it while's only one time....
use it like this :
while ($row = mysql_fetch_assoc($result)) {
echo $row['firstname'];
echo $row['lastname'];
echo $row['address'];
echo $row['age'];
}
You have to fetch the row, there are a few methods of doing it: mysql_fetch_object, mysql_fetch_row, mysql_fetch_array and mysql_fetch_assoc.
These methods will read a single line from the result and remove it from the handler, so if you loop the call it will read all the rows, one by one until it reaches the end and returns false.
example:
while($obj = mysql_fetch_object($result)){
echo $obj->name;
}
PHP.net documentation:
mysql_fetch_object,
mysql_fetch_row,
mysql_fetch_array,
mysql_fetch_assoc

mysql_fetch_array does not retrieve all rows

$query = "SELECT * FROM table";
$result = mysql_query($query, $db);
$all = mysql_fetch_assoc($result);
echo mysql_num_rows($result) . ":" . count($all);
This returns
2063:7
I have not used count before, so I'm not 100% sure it's not counting the table columns. It's late and I might be going nuts.
Here's another example of what's happening:
$result = mysql_query($query, $db);
echo "Rows: " . mysql_num_rows($result) . " <BR />";
$player_array = mysql_fetch_assoc($result);
echo "<pre>";
print_r($player_array);
echo "</pre>";
Which outputs:
Rows: 9
Array
(
[playerID] => 10000030
)
TL;DR: I submit queries which return multiple rows, but fetch_array only gives me a small portion of those rows in the resulting array.
mysql_fetch_assoc returns only one row in once you have to use loop to retrieve all rows
while($row = mysql_fetch_assoc($result))
{
print_r($row);
}
Every call to mysql_fetch_assoc($result); gives you one row of the result set:
(from the documentation)
mysql_fetch_assoc — Fetch a result row as an associative array
Returns an associative array that corresponds to the fetched row and moves the internal data pointer ahead. mysql_fetch_assoc() is equivalent to calling mysql_fetch_array() with MYSQL_ASSOC for the optional second parameter. It only returns an associative array.
You have to use the function in a loop:
$all = array();
while(($row = mysql_fetch_assoc($result))) {
$all[] = $row;
}
The example in the document shows how it is done.
mysql_fetch_assoc doesn't work that way, you need to call it multiple times to get all rows. Like this:
while ($row = mysql_fetch_assoc($db_result))
{
print_r($row);
}

Categories