-1

My goal is to get the country names to print out in alphabetical order. This is the function I wrote for that...

function getCountries(){
    $namesQ = 'SELECT Name FROM `country` ORDER BY Name ASC';
    global $myPdo;
    $command = $myPdo -> prepare('namesQ');
    $command -> execute();  
    return $command;    
}  

Then, I echo out the names from an array in HTML...

<!DOCTYPE html>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Country Names</title>

        <link type="text/css" rel="stylesheet" href="primary.css" />
    </head>

    <body>
        <div id="main" action="controller.php" method="post">
            <?php
                $countries = getCountries();
                $countryNames = $countries->fetchAll();

                foreach( $countryNames as $countryName )
                {
                    echo 'test';
                    echo '<p> ' . $countryName['Name'] . '</p>';
                }
            ?>
        </div>
    </body>
</html>

But it seems that the foreach loop does not get accessed at all, because even...

 echo 'test';

... does not print to screen.

I changed the index in $countryName to fhsdjk since there is no such index, but I don't even get an error message or anything at all. How can I get it to echo out whatever is inside the foreach loop?

toonice
  • 2,211
  • 1
  • 13
  • 20
Dima
  • 27
  • 1
  • 8

2 Answers2

1

your passing string you need to pass variable

$command = $myPdo -> prepare('namesQ');
(To)
$command = $myPdo->prepare($namesQ);
JYoThI
  • 11,977
  • 1
  • 11
  • 26
0

It seems, that you are preparing the string 'namesQ', but you actually want to prepare the sql statement assigned to $namesQ. So, replace

$command = $myPdo->prepare('namesQ');

with

$command = $myPdo->prepare($namesQ);

I suggest you to include the fetchAll() call into the getCountries() function and to just call:

$countryNames = getCountries();

And, since you have some problems discovering the db-access errors, I suggest you to always implement exception handling. Especially when you are using PDO as data-access abstraction. Here an example - in analogy with your code:

function getCountries() {
    try {
        $namesQ = 'SELECT Name FROM `country` ORDER BY Name ASC';

        global $myPdo;

        // Hier: replaced 'namesQ' with $namesQ.
        $command = $myPdo->prepare($namesQ);

        if (!$command) {
            throw new Exception('The SQL statement can not be prepared!');
        }

        if (!$command->execute()) {
            throw new Exception('The PDO statement can not be executed!');
        }

        return $command->fetchAll();
    } catch (PDOException $pdoException) {
        echo '<pre>' . print_r($pdoException, true) . '</pre>';
        exit();
    } catch (Exception $exception) {
        echo '<pre>' . print_r($exception, true) . '</pre>';
        exit();
    }
}

Maybe an early answer of mine, regarding exception handling, will be helpful too:

Exception handling for PDO::prepare() and PDOStatement::execute() + A generalized exception handling scheme