2

I'm trying to display individual values from a PDO statement to radio buttons. Here's what I have so far.

    <?php

    session_start();  
    $host = "localhost";  
    $username = "root";  
    $password = "";  
    $database = "training";  
    $result = "";

        $connect = new PDO("mysql:host=$host; dbname=$database", $username, $password);  

            $query = "SELECT username FROM accounts";  
            $statement = $connect->prepare($query);
            $statement->execute();
            $result = $statement->fetchAll(PDO::FETCH_ASSOC);  

    echo "<input type='radio' name ='president'>". $result . "</input>"

    ?>

but it results in this

"Notice: Array to string conversion in C:\xampp\htdocs\testing\vote.php"

I've tried using print_r($result); to check the values

[0] = admin
[1] = operation1

I want those values to be displayed on radio buttons text.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
Kupal Joo
  • 23
  • 3
  • While this isn't directly related to what you're asking about here, `` doesn't need a closing tag, but it _will_ need a `value` attribute if it's really going to be useful as a radio button. – Don't Panic Aug 31 '17 at 18:57

2 Answers2

0

So you are already fetching all the values and there for the values are stored in an array (a list of data). You are currently trying to print that list out but PHP doesn't support that with a simple echo. You will have to loop this array and print it out.

foreach($result as $item) {
    echo "<input type='radio' name ='president'>". $item. "</input>"
}

$result = the list variable, took the name you already have. This loop with for each time it goes take the next value in the list and put it in the variable $item so it's no longer a list but a single value.

EDIT

My inital answer will work only if the fetchAll method call is changed according to Don't panic's answer in the comments with changing it to

fetchAll(PDO::FETCH_COLUMN)
Fyllekanin
  • 274
  • 1
  • 12
  • This will still cause the same error. Each `$item` will be an associative array. – Don't Panic Aug 31 '17 at 18:45
  • Using `PDO::FETCH_COLUMN` in the `fetchAll` call would allow you to do it this way, though. – Don't Panic Aug 31 '17 at 18:46
  • @Don'tPanic It should not as the poster decribed the list with values that are strings it will not complain about associative array because now $item will each be a string. – Fyllekanin Aug 31 '17 at 18:46
  • Not sure exactly what was going on there, but that wasn't really a proper `print_r` output, and it's not the result you'd actually get when using `fetchAll(PDO::FETCH_ASSOC)`. That will be an array of arrays, you know? – Don't Panic Aug 31 '17 at 18:49
  • @Don'tPanic updated the answer to include the change of fetchAll argument – Fyllekanin Aug 31 '17 at 18:53
0

You are getting the notice because you are trying to print an array - instead of an array element - using echo.

You are using fetchAll(). So, the $result array is a two dimensional array. Like this:

Array
(
    [0] => Array
        (
            [username] => John
        )

    [1] => Array
        (
            [username] => Mikaela
        )

    [2] => Array
        (
            [username] => Angela
        )
)

Here is how it should go:

// Fetch the values.
$query = "SELECT username FROM accounts";  
//...
$result = $statement->fetchAll(PDO::FETCH_ASSOC);

/*
 * Just for testing: display results.
 * Note: In the future use this complete line to display an array, if you want to.
 */
echo '<pre>' . print_r($result, TRUE) . '</pre>';

/*
 * Print the radio buttons.
 * Note: Note: Apply apostrophe (') and double quote (") like this.
 */
foreach ($result as $key => $item) {
    $username = $item['username'];
    echo '<input type="radio" name="president" value="' . $username . '">' . $username . '</input>';
}

Try to use exception handling. For a proper use of prepared statements with exception handling see this answer of mine (look into the Add.php (the class)).

Good luck!