connecting website database from localhost, error !!!

Discussion in 'PHP' started by mahdi_fci3, Dec 14, 2010.

  1. #1
    Hi all,
    I woudl like to connect to website database from localhost,which means displaying the databse of the site to my localhost pages, and here is the code:
    
    $con =@ mysql_connect("http://www.sitename.com" ,"username" ,"password" ) or die (mysql_error()) ;			
    $db = mysql_select_db ( "Website_DB" , $con)or die ("Database not found" ) ;
    
          $result = mysql_query("SELECT * FROM Customers ") or die(mysql_error());
               while($row = mysql_fetch_array($result)){
    	       echo $row['FirstName'] . " " . $row['LastName'];
    	        echo "<br />";
                   }
    
    PHP:
    so excuting this code I get an error: "Unknown MySQL server host 'http' (11001)" .
    Any help please ????
     
    mahdi_fci3, Dec 14, 2010 IP
  2. shofstetter

    shofstetter Well-Known Member

    Messages:
    178
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    120
    #2
    remove http:// and just use www.sitename.com http:// is a protocol specification and not part of the host name.

    for localhost you could also use the word localhost or 127.0.0.1, but you will also need to enter the username and password not just the words "username" and "password" something like:

    $con = mysql_connect("127.0.0.1" ,"mysql_user" ,"xpd911" ) or die (mysql_error()) ;
     
    shofstetter, Dec 14, 2010 IP
  3. mahdi_fci3

    mahdi_fci3 Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks shofstetter,
    if I use $con = mysql_connect("127.0.0.1" ,"mysql_user" ,"mypassword" ) or die (mysql_error()) ;
    the local serever (xampp) think that I am calling to it, and I am not colling to it I would like to call the website server (www.mysitename.com), and I get this error
    "Access denied for user 'mysql_user'@'MyIPaddress' (using password: YES)".
     
    mahdi_fci3, Dec 14, 2010 IP
  4. shofstetter

    shofstetter Well-Known Member

    Messages:
    178
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    120
    #4
    your mysql user does not have permission to access the database from your location. You will have to add the permissions to your database user to grant it remote access.

    http://www.debianhelp.co.uk/remotemysql.htm
     
    shofstetter, Dec 14, 2010 IP
  5. mahdi_fci3

    mahdi_fci3 Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thank u again,
    but I need to ask , when I do all the steps , can I use my localhost server and wwwsitename.com server at the same time , because there is some pages using internet database and some aother pahes using localhost database. dont surprise my question , but the system that I am doing needs both database( localhost DB and Site name DB )
     
    mahdi_fci3, Dec 14, 2010 IP
  6. mahdi_fci3

    mahdi_fci3 Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks shofstetter very much
    I enabled remote accecss from the CPanel then it work . thank u again 4 ur time
     
    mahdi_fci3, Dec 14, 2010 IP
  7. backlinkneeded

    backlinkneeded Member

    Messages:
    285
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #7
    @shofstetter, you are good. Thanks for good advise. Can I add you in my friend list?
     
    backlinkneeded, Dec 22, 2010 IP