1
<?php
$q = intval($_GET['q']);

$con = mysqli_connect('censored','censored','censored','db');
if (!$con) {
    die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con,"db");
$sql="SELECT message FROM messages WHERE code = '".$q."'";
$result = mysqli_query($con,$sql);
$resultstring = (string)$result;

echo $resultstring;

mysqli_close($con);
?>

I am attempting to echo the result of the query to the user but when this PHP runs through ajax I get this error:

Recoverable fatal error: Object of class mysqli_result could not be converted to string in D:\xampp\htdocs\getmessage.php on line 12

Now I don't understand this because I am already converting $result into a string.. Thanks!

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 5
    Which bit of [fetch](http://php.net/manual/en/class.mysqli-result.php)ing records from a [resultset](http://php.net/manual/en/mysqli.query.php#refsect1-mysqli.query-returnvalues) did you miss in the documentation? – Mark Baker Nov 09 '17 at 09:24
  • *"Now I don't understand this because I am already converting $result into a string"* ... well you're not, because you can't, hence the error. – CD001 Nov 09 '17 at 09:33

5 Answers5

2

mysqli_query() can not convert to string.

You must use mysqli_fetch for parse to array.

Example:

if ($result=mysqli_query($con,$sql))
  {
  // Fetch one and one row
  while ($row=mysqli_fetch_row($result))
    {
    printf ("%s (%s)\n",$row[0],$row[1]);
    }
}
Thuan Nguyen
  • 244
  • 1
  • 14
2

First of all mysqli_query Performs a query on the database it does not directly return a value

$query = mysqli_query($con,$sql);

so to do that use mysqli_fetch_assoc it will Fetch a result row as an associative array

$result = mysqli_fetch_assoc($query);

Then you can now get your value

$resultstring = $result['message'];

so your code should be like this

<?php
    $q = intval($_GET['q']);

    $con = mysqli_connect('censored','censored','censored','db');
    if (!$con) {
        die('Could not connect: ' . mysqli_error($con));
    }

    mysqli_select_db($con,"db");
    $sql="SELECT message FROM messages WHERE code = '".$q."'";
    $query = mysqli_query($con,$sql);
    $result = mysqli_fetch_assoc($query);
    $resultstring = $result['message'];

    echo $resultstring;

    mysqli_close($con);
?>
Beginner
  • 4,118
  • 3
  • 17
  • 26
  • Thank you, this somewhat solved my issue. The result is now given to me if I inspect it through Chrome's dev tools but the echo is not showing up on the page where the request is made.. I thought it would echo out so that the user sees it? –  Nov 09 '17 at 09:51
  • perfect solution – Hass Jan 21 '20 at 15:17
1

you need to convert the data(RS) into array !

$result = mysqli_fetch_assoc($result);
print_r($result);
Y.op
  • 79
  • 1
  • 1
  • 9
0

As an result of your query on success You will get mysqli_result object. If You want to get received rows use fetch data function.

$result = mysqli_query($con,$sql);
$resultstring = $result->fetch_row()[0];

See docs: mysqli_query & mysqli_result

Anwarus
  • 138
  • 2
  • 11
0

I have resolved similar issue with following approach.

// Assign your sql statement
$sqlMasterSiteIds = "SELECT DISTINCT table_name FROM site WHERE gwid = 39 AND master_site_id NOT LIKE '0'";

// Run the query
$masterSiteIdsQuery = $this->db->query($sqlMasterSiteIds);

// Prepare container               
$masterSiteIds = [];

// Fetch first row of query results
$first_row = $masterSiteIdsQuery->fetch_assoc();

// Assign first row to your prepared container
$masterSiteIds= $first_row['master_site_id'];

// Change the results into string comma separated so it can be used in next mysqli query or echoed
while($sub_row = $masterSiteIdsQuery->fetch_assoc()) {
     $masterSiteIds = implode(',', array($masterSiteIds, $sub_row['master_site_id']));
}

// Now you can use $masterSiteIds which is a STRING of values comma separated
echo $masterSiteIds;   // Output: '1024, 1142, 8492'

I hope it will help you or anybody else in need. I am not sure if it is the best approach but it worked well in my case as I had to change my results into STRING so I could use it in my next mysqli query with FIND_IN_SET command.

Happy coding!

RafalM
  • 13
  • 1
  • 4