Connecting to database echo rows problems.

Discussion in 'PHP' started by blktallos, Sep 4, 2014.

  1. #1
    
    <?php 
    $host = "localhost"; // your MySQL host i.e. the server on which the database is, usually localhost
    $user = "example"; // your MySQL username 
    $pass = "example"; // your MySQL password 
    $db = "blakesdatabase"; // the database to which you're trying to connect to 
    
    $result = mysql_query("SELECT * FROM blakesdatabase");
    
    echo $result;
    ?> 
    
    Code (markup):
    I am trying to connect to a database I set up. And I am having problems with this piece of code. I was wondering if someone could help me out. I am trying to echo out the first 2 rows in the database.

    I am getting this error -
    Warning: mysql_query(): Access denied for user 'wwwspike'@'localhost' (using password: NO) in /home/wwwspike/public_html/andulicsdesign.com/Blakes/Index.php on line 12
    
    Warning: mysql_query(): A link to the server could not be established in /home/wwwspike/public_html/andulicsdesign.com/Blakes/Index.php on line 12
    
    Code (markup):
    New to PHP

    Thank you
     
    blktallos, Sep 4, 2014 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Cool, maybe it's not too late for you to forget everything you've learned and switch to a newer tutorial (or wherever else you got this code from). Seriously, don't continue learning where you're currently learning from.

    This code is very very old, very very slow, very very insecure, and very very deprecated, and you should not use it at all. Take a look at PDO:
    http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers

    As for the error you're getting, it means that you're not connecting to the database to begin with. Take a look at the Connecting to MySQL section of the link above.
     
    nico_swd, Sep 4, 2014 IP
  3. abhicyco

    abhicyco Active Member

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    75
    #3
    You are probably passing wrong values for username and password.
     
    abhicyco, Sep 5, 2014 IP
  4. Krellen

    Krellen Greenhorn

    Messages:
    38
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    18
    #4
    The issue is most likely that you aren't even attempting to connect to the database. In your example, you'd need to use mysql_connect() before running the query.

    I'll second the motion to use PDO. You really, really should.
     
    Krellen, Sep 10, 2014 IP