Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    May 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    The process for calling MySQL commands using PHP Data Objects.

    Im trying to connect to my database so I can display information using queries.

    Code:
    <!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!!

  • #2
    New Coder
    Join Date
    May 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    First error was simply bad credentials.
    Second error is that you have used an alias in your query, so full_name doesn't exist.

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,344
    Thanks
    13
    Thanked 349 Times in 345 Posts
    btw, you can use PDO’s statements in a foreach() loop.
    PHP Code:
    $rs $pdo->query($strSQL);
    foreach(
    $rs as $row)
    {
        
    // ...

    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •