View Full Version : The process for calling MySQL commands using PHP Data Objects.

10-30-2012, 12:59 AM
Im trying to connect to my database so I can display information using queries.

<!doctype html>
//1. Connect to the database
$dbname = "abcd0001";
$dbuser = "abcd0001";
$dbpass = "password";
$dbhost = "localhost";

$pdo = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
//2. Run the desired query / queries
$strSQL = "SELECT full_name AS fn, email FROM users WHERE user_type=1";
$rs = $pdo->query($strSQL);
while($row = $rs->fetch(PDO::FETCH_ASSOC)){
echo "<p>" . $row["full_name"] . " - " . $row["email"] . "</p>";
<meta charset="utf-8">
<title>Untitled Document</title>
<link rel="stylesheet"href="css/syles.css">

<div id="wrapper">
<header id="masthead">
<h1>Useless Crap You Want to Buy</h1>
<h2>Give us your money and we'll give you crap</h2> </header>
<nav id="navbar">
<li><a href="podphp/default.asp">Home</a></li>
<li><a href="podphp/news.asp">News</a></li>
<li><a href="podphp/contact.asp">Contact</a></li>
<li><a href="podphp/about.asp">About</a></li>
//4. close the connection to the database
$pdo = NULL;

Thats my code, im trying to display some information name email all that from my table thats created in my database.

Im getting these 2 errors when I run the php code.

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [1045] Access denied for user 'abcd0001'@'localhost' (using password: YES)' in D:\wamp\www\pod\pod.php on line 9
PDOException: SQLSTATE[HY000] [1045] Access denied for user 'abcd0001'@'localhost' (using password: YES) in D:\wamp\www\pod\pod.php on line 9

Im not sure on how to fix these errors. Im going by the code that the teacher gave us. We have to use pdo not mysqli.

Any thoughts on this matter would be greatly appreciated.


10-30-2012, 01:17 AM
Fixed those error messages. Mow getting this error Undefined index: Notice: Undefined index: full_name in D:\wamp\www\pod\pod.php on line 14

10-30-2012, 04:05 AM
First error was simply bad credentials.
Second error is that you have used an alias in your query, so full_name doesn't exist.

10-30-2012, 08:52 AM
btw, you can use PDO’s statements in a foreach() loop.

$rs = $pdo->query($strSQL);
foreach($rs as $row)
// ...

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum