Help with basic PDO connection
Does $row['Name_F'] have to be an index as I don't use indexes in this table?
otuatail TRhis results in crashing the web page with
This page isn’t working
That will always be printed because you put it as the first thing the page should do, before you've even attempted to connect to the datatabase.
I'm also guessing that what you're posting isn't actually what you're running, based on the spurious text in
$stmt = $pdo->query("SELECT * FROM PDO");
ecPDO");
while ($row = $stmt->fetch())
otuatail
No, but First_N
has to be a column in the table you're querying.
Not now. I did change the names in the table.
Notice: Undefined index: Name_F in /vhost/vhost15/d/e/s/desmond-otoole.co.uk/www/music/music.php on line 37
Field Type
Name_F varchar(20)
Name_S varchar(20)
DOB date
<?
$pdo = connectDB();
$stmt = $pdo->query("SELECT * FROM PDO");
while ($row = $stmt->fetchAll())
{
echo $row['Name_F'] . "<br/>";
}
?>
DROP TABLE IF EXISTS `PDO`;
CREATE TABLE IF NOT EXISTS `PDO` (
`Name_F` varchar(10),
`Name_S` varchar(10),
`DOB` date
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `PDO` VALUES
('Bruce' , 'Springsteen', '1946-09-05'),
('Feddy' , 'Mercury', '1946-09-05'),
('Bruce' , 'Willis', '1955-03-19'),
('Marvin' , 'Gaye', '1939-04-02'),
('Diana' , 'Ross', '1944-03-26');
[/code']
<?
$pdo = connectDB();
$stmt = $pdo->query("SELECT * FROM PDO");
while ($row = $stmt->fetchAll())
{
echo $row['Name_F'] . "<br/>";
}
?>
Notice: Undefined index: Name_F in /vhost/vhost15/d/e/s/desmond-otoole.co.uk/www/music/music.php on line 37
So execution is getting that far: rows are being returned, but something is wrong with the contents of those rows. Do some debugging: replace the echo with a simple var_dump($row)
to see what exactly it contains.
(Incidentally, MyISAM
isn't appropriate in most situations for safety reasons, and mixing character encodings (UTF8 in one place, latin1 in another) is a good way to corrupt text. And you've misspelled "Freddie" ).
$row = $stmt->fetchAll()
The fetchAll() method returns an array consisting of all the rows in the result set, so of course there's no main index named Name_F.
Oh yeah, I'm so used to using foreach
to iterate over a collection that I completely missed that it said fetchAll
and not fetch
. It used to say fetch
but the OP changed that for whatever reason.