简体   繁体   中英

How to get a specific value from my database with PHP

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

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

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'];    

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

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM