0

I have the feeling this is an easy to fix problem, but I can't find the answer. I'm trying to get a specific value from my database and then print it to the screen but I keep getting the following error:

Object of class mysqli_result could not be converted to string

I'm pretty sure it's because i'm requesting an entire row instead of a value. I'm just not sure how to fix it, i've tried a few options but nothing works. My code:

<?php
  $host = 'localhost';
  $user = 'root';
  $pass = '';
  $db = 'mydatabase';

  $connection = new mysqli ($host, $user, $pass, $db) or die ("Databse connection failed");
  $sql = "SELECT waitlist_text FROM texts";

  $waitlist_text = $connection->query($sql);

  echo $waitlist_text
?>

The database table is called 'texts' and has 2 columns: 'id' 'waitlist_text'. I would like the text thats under column waitlist_text on id 1

Mischa
  • 2,069
  • 4
  • 23
  • 32
  • After performing your [query](http://php.net/manual/en/mysqli.query.php), you'll need to [fetch](http://php.net/manual/en/mysqli-stmt.fetch.php) data from the resulting object. – showdev Apr 11 '16 at 17:40
  • use `fetch_object ` or `fetch_assoc` method on your result – ceadreak Apr 11 '16 at 17:40

5 Answers5

3

Yes, the Query results in a MYSQLI object. You have to actually use another MYSQLI function to get to said data.

For example:

  <?php 
    $result = $connection->query($sql);
    while($row = $result->fetch_rows()){
     print_r($row);
     //In this case, you can acces results like this: echo $row[0]."<br/>";
    }
  ?>

http://php.net/manual/en/class.mysqli-result.php

See the above source for more information on the subject. There are many ways to get values, like for example, using fetch_assoc() instead of fetch_row(). fetch_assoc() makes it possible for you to access your fields (in the same while loop as before) like this: $row['columnname']

Please see example #1 of this page and compare your own function to it. It might help you learn: http://php.net/manual/en/mysqli-result.fetch-row.php

NoobishPro
  • 2,539
  • 1
  • 12
  • 23
2

use where condition, bind parameters and execute for example:

$sql = 'SELECT waitlist_text FROM texts WHERE id = :idparm';
$id = 1;
$dbh = new PDO("mysql:host=$host;dbname=$db", $user, $pass);
$sth = $dbh->prepare($sql);
$sth->bindParam(':idparm', $id, PDO::PARAM_INT);
$sth->execute();
$result = $sth->fetch(PDO::FETCH_ASSOC);
print_r($result);

or:

while ($result = $sth->fetch(PDO::FETCH_ASSOC))
  echo $result['waitlist_text'];    
Adam Silenko
  • 3,025
  • 1
  • 14
  • 30
-1

You are correct you are getting all of the results instead of just the one you are looking for. In order to get the result you're looking for you need to call fetch_assoc() to get the array of the first result. In this case $row would be an array that would contain all the values you have selected under the indices of the name of the columns you selected from your table. It would look like this:

$row = $waitlist_text->fetch_assoc();
echo $row["waitlist_text"];
Nathan Bierema
  • 1,813
  • 2
  • 14
  • 24
-1

Try

echo $waitlist_text->fetch_row()[0]

Per the PHP manual. fetch_row gets the current row as an array, then you can print the first element, which will be your waitlist_text.

cxw
  • 16,685
  • 2
  • 45
  • 81
-1
<?php
  $host = 'localhost';
  $user = 'root';
  $pass = '';
  $db = 'mydatabase';

  $connection = new mysqli ($host, $user, $pass, $db) or die ("Databse connection failed");
  $sql = "SELECT waitlist_text FROM texts";

  $waitlist_text = $connection->query($sql);

  while($row = $waitlist_text->fetch_assoc()) {
      echo $row["waitlist_text"];
  }
?>
Eugene
  • 547
  • 4
  • 11
  • 1
    Why is this answer accepted over mine? It's pretty much the exact same answer with the only difference being that this answer explains nothing at all. This is just fixing up code with no information whatsoever. - I will check back to see if more information has been added. If not, I will have to downvote as this does not really help anyone in my eyes. – NoobishPro Apr 11 '16 at 18:26
  • 1
    you have right, but don't be sad, I gave you the up vote :) – Adam Silenko Apr 11 '16 at 19:46
  • 1
    But this is bad practice ... you should read [PHP: The Right Way](http://www.phptherightway.com/#pdo_extension) – Adam Silenko Apr 11 '16 at 20:13
  • I agree (kind of) that your way is better. However, I do not believe it's what OP needs, asked for, or in any way intends to use. To be frank with you, I've been into php for ~3,5 years now and I still don't really understand why I would use your syntax ;D (I have my own custom db class anyway) – NoobishPro Apr 11 '16 at 20:17
  • 1
    Adam Silenko it's really simple. Topic starter ask about mysqli, and I answered with mysqli example. If you use php to make serverside worker does anyone say - "Hey your practice is bad, you should use C++, it's faster!" I think no. =) – Eugene Apr 12 '16 at 20:29
  • 1
    i used this as the answer simply cause this was directly useable in my code. maybe that's bad practice of me? I found almost every answer useable, i just ended up using this one even though it had no explanation. – Mischa Apr 12 '16 at 21:38