How to convert any value from sql table into string [duplicate] - php

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

Related

Extracting data from MYSQL and storing it into a variable in PHP [duplicate]

This question already has answers here:
Single Value Mysqli [duplicate]
(8 answers)
Why shouldn't I use mysql_* functions in PHP?
(14 answers)
Closed 2 years ago.
I am having trouble extracting information from a table I have created in MYSQL. I am trying to extract the last entry id number of an individual and want to store it into a variable called $person_id.
So far, I have the following:
$res = mysql_query("SELECT max(person_id) FROM Persons;");
$person_id= mysql_query($res,$conn);
echo $person_id;
Nothing shows up when I try to print the variable name person_id.
The connection to the database works fine since I am able to insert data from a form I created. Any advice?
mysql_query was deprecated in PHP 5.5.
It will return a resource on success, or FALSE on error.
So, instead of echo, you could first try a var_dump($person_id).
Source : https://www.php.net/manual/en/function.mysql-query.php
First, avoid using mysql this extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used. Now back to your question, you are printing out the wrong value, your query using mysqli should look like this
$res = "SELECT max(person_id) FROM Persons;";
$person = mysqli_query($conn, $res);
echo $person;
this will return nothing because the result is an array(mysqli object) it cannot be converted to string just like that but, using print_r we can print out the value of person as follows
print_r($persons);
you should get something like this
mysqli_result Object ( [current_field] => 0 [field_count] => count [lengths] => [num_rows] => rows [type] => 0 )
the count and rows represents, depending on the results the number of fields and rows used in the query.
To print out the $person variable correctly, as in your query above it should be
$res = "SELECT max(person_id) FROM Persons;";
$result = mysqli_query($conn, $res);
//fetch the database values as an asscoiative array
while($row = mysqli_fetch_assoc($result))
{
$person_id = $row['person_id'];
}
echo $person_id;
this should print the value you wanted
You must have got some error because you are missing something here.
Thats how it should work for you.
$res = "SELECT max(person_id) FROM Persons";
$result = mysql_query($res,$conn);
list($person_id) = mysql_fetch_row($result);
echo $person_id;

How to loop table in php [duplicate]

This question already has answers here:
Object of class mysqli_result could not be converted to string
(5 answers)
Closed 1 year ago.
How can I loop this table in PHP?
$query= "SELECT * FROM table1";
$select= mysqli_query($connection, $query);
$row = mysqli_fetch_array($select);
while ($row = mysqli_fetch_array("$select")) // line 21
{
echo $row["column1"];
}
I have updated the code and I get this error
Recoverable fatal error
: Object of class mysqli_result could not be converted to string on line 21
There are multiple issues with your current code.
You fetch the first row with $row = mysqli_fetch_array($select); (line 3), but you don't do anything with it. This means that the first result is discarded.
Your while loop attempts to loop over an incorrect variable ($query is a string, not the result-object), and you've quoted it into a string - you need to do it as you were with the first fetch (line 3).
You don't do anything inside your loop, so the results aren't printed. At the very least, you should print them with echo.
$query = "SELECT * FROM table1";
$result = mysqli_query($connection, $query);
while ($row = mysqli_fetch_array($result)) {
echo $row["column1"]."<br />\n";
}

How do you convert a mysqli_result into a string? [duplicate]

This question already has answers here:
Single result from database using mysqli
(6 answers)
Closed 3 years ago.
I'm trying to echo the result of a mysqli_query however I keep getting the error 'Catchable fatal error: Object of class mysqli_result could not be converted to string' on line 'echo $result;'.
Is there any way I can convert it into a string so that it can be echoed? (P.S sorry if this is easy, I'm new to coding.)
My database is successfully connected and the SQL statements definitely work.
$sql= "SELECT ImageURL FROM `unnormalisedtable` WHERE Yeargroup = 9 ORDER BY RAND() LIMIT 1" ;
$result = mysqli_query($db, $sql);
echo $result;
The expected output is that the result of my SQLi query will be printed on screen, however the error is generated instead. Thanks for any help in advance.
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'];
}

Display string result of SQL query in PHP [duplicate]

This question already has answers here:
Object of class mysqli_result could not be converted to string
(5 answers)
Closed 1 year ago.
Can someone explain how I would output the result of the sql below? currently getting 'Object of class mysqli_result could not be converted to string'.
$sql = ("SELECT AVG(ab_satisfactionScore) AS AverageSatisfactionScore
FROM tbl_appointmentsbooked;");
$result = mysqli_query($connection, $sql);
echo ($result);
Error because you are echoing an object, so try like this,
while($res = mysqli_fetch_array( $result )) {
echo $res['AverageSatisfactionScore'];
}
Use any of mysqli_fetch_*() functions (or in OOP style: $result->fetch_*()) to retrieve the results from the mysqli_results object ($results).
See mysqli_result documentation on the various methods and their uses.
because you try to use echo to print object, and it used to print string only, you should use:
print_f($result);
instead of
echo ($result);

Object of class mysqli_result could not be converted to string return mysql average [duplicate]

This question already has answers here:
Object of class mysqli_result could not be converted to string
(5 answers)
Closed 1 year ago.
Can someone explain how I would output the result of the sql below? currently getting 'Object of class mysqli_result could not be converted to string'.
$sql = ("SELECT AVG(ab_satisfactionScore) AS AverageSatisfactionScore
FROM tbl_appointmentsbooked;");
$result = mysqli_query($connection, $sql);
echo ($result);
Error because you are echoing an object, so try like this,
while($res = mysqli_fetch_array( $result )) {
echo $res['AverageSatisfactionScore'];
}
Use any of mysqli_fetch_*() functions (or in OOP style: $result->fetch_*()) to retrieve the results from the mysqli_results object ($results).
See mysqli_result documentation on the various methods and their uses.
because you try to use echo to print object, and it used to print string only, you should use:
print_f($result);
instead of
echo ($result);

Categories