...

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



Makdaddy
10-29-2012, 11:59 PM
Im trying to connect to my database so I can display information using queries.


<!doctype html>
<?php
//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>";
}
?>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<link rel="stylesheet"href="css/syles.css">
</head>

<body>
<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">
<ul>
<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>
</ul></nav>
</div>
<?php
//4. close the connection to the database
$pdo = NULL;
?>
</body>
</html>


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
[/code]
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.


Thanks!!

Makdaddy
10-30-2012, 12: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

Fou-Lu
10-30-2012, 03: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.

Dormilich
10-30-2012, 07: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