-1

I'm trying to query the DB with mysqli and then fetch the result, but it's throwing an Object of class could not be converted to a string error.

Here's what I am trying to accomplish:

<?php
include ('conn.php');

$query= "select value from the_table where item = 'url'";
$result = mysqli_query($conn, $query);
?>

And then I am trying to populate a link with the $result:

<a href="<?php echo $result; ?>">This is the Link to the URL</a>

I saw this post: PHP and MySQL error: Object of class mysqli_result could not be converted to string

So, I tried to format the echo with echo $result->fetch_object()->url;, but that didn't work.

I'm not sure if I have to fetch the result and then throw it into a look with a mysqli_fetch_array() and if so, how do I get it to populate that the value outside of the loop?

Community
  • 1
  • 1
ultraloveninja
  • 1,969
  • 5
  • 27
  • 56
  • 1
    You need to `$row = mysqli_fetch($result)` to get a row, first. – Marc B Mar 10 '14 at 21:44
  • you always first have to fetch the actual result using the fetch_array or fetch_object method. That is correct. However your `->url` part indicates that you have a selected column called `url` in your query. While in the query posted, you do not. Instead, what you wanted is echo `$result->fetch_object->value` – Tularis Mar 10 '14 at 21:44

2 Answers2

0

Assuming that $db is your database connection link you can perform the query:

$result = mysqli_query($db, 'select name from fruits');

Then you can get name using procedural style:

echo mysqli_fetch_object($result)->name;

Or object style:

echo $result->fetch_object()->name;

In your code you're trying to output url property which is not defined - as we can see in your query.

barell
  • 1,470
  • 13
  • 19
0

Try some thing like this:-

    $query= "select value from the_table where item = 'url'";
    $result = mysqli_query($conn, $query) or die ("Couldn't execute query.");

    // use returned data
    while($row = mysqli_fetch_assoc($result))
    {
        echo $row['value'];
    }

OR

while ( $row = mysqli_fetch_assoc( $result ) )           
   {
      foreach ($row as $key => $value)
      {
     print ($row . " = " . $value . "\n");
      }
      print("================");
   }
Mubo
  • 1,078
  • 8
  • 16