1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Error While Connect MySQL DAtabase

Discussion in 'Databases' started by suwandichen13, Mar 22, 2016.

  1. #1
    Hi guys i want to ask you about connect mysql database from different hosting servers using php. and here my php code
    SEMrush
    
    <?php
    
    $db_host   = 'myurl.com:3306'; << Usually is Localhost.
    $username = 'mycpanel usernam';
    $password = 'smycpanel pass';
    $db_usr      = 'my database users';
    $db_pwd      = 'db pass';
    $db_db      = 'database name';
    
    $con = mysqli_connect($db_host,$db_usr,$db_pwd,$db_db);
    
    if (mysqli_connect_errno())
      die("Unable to connect to Mysql Server");
    
    
    function getblogList($con,$limit=5){
      $resblogData = "";
      $query = mysqli_query($con, "SELECT * FROM blog_content ORDER BY id DESC LIMIT $limit");
      
      while($row = mysqli_fetch_array($query)) {
      //populate and display results data in each row
      $post_title = $row['post_title'];
      $posted_by = $row['posted_by'];
      $post_date = $row['date'];
      $post_date_raw=date_create($post_date);
      $post_month = date_format($post_date_raw,"M");
      $post_day = date_format($post_date_raw,"j");
      $post_year = date_format($post_date_raw,"Y");
      $post_url = $row['post_url'];
      
      $resblogData .= "<li><span><a href='myurl path here'>$post_title</a></span>
       <div class='blogListData'>$post_month $post_day, $post_year | Posted By $posted_by</div>
       </li>";  
      }
      return $resblogData;
    }
    
    ?>
    
    
    <?php echo getblogList($con,5); ?>
    
    Code (markup):

    Warning: mysqli_connect(): (HY000/1045): Access denied for user 'myusername'@'ajax.vivawebhost.com' (using password: YES) in /home/sligscom/public_html/index.php on line 10
    Unable to connect to Mysql Server

    i do allow IP access from mysql database remote. so where the problem?
     
    Last edited by a moderator: Mar 29, 2016
    suwandichen13, Mar 22, 2016 IP
    SEMrush
  2. jestep

    jestep Prominent Member Premium Member

    Messages:
    3,661
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    I'd double check your login credentials and double check the user is allowed to connect from the server the code is being hosted from.
     
    jestep, Mar 29, 2016 IP
  3. Bitpalace

    Bitpalace Greenhorn

    Messages:
    53
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    13
    #3
    You have entered a domain name in the database server name field. This may be directed to your host, but MySQL makes a difference between the domain name and localhost. If your database is located on the same server, use localhost. Else you will need to add extra credentials for your login name (user name) to link it with the domain name, too.
    ----
    Edited: Sorry, I overlooked that you said you are on a different host. In that case your grant need to list the host you are connecting from, for example:
    grant all privileges on dbname.* to user@"%" identified by 'password';
    Instead of the % wildcard you can limit access to the IP that the connection is coming from. However, this setting is probably missing from your user table in MySQL and that is why you cannot login with your credentials.
    -----
    And have you checked that the MySQL service on the database server is not bound to 127.0.0.1? This is also a frequently made mistake. Remove the bind-statement from /etc/my.cnf, because if the service is bound to 127.0.0.1 it won't listen on packets that are coming from other addresses.
     
    Bitpalace, Mar 29, 2016 IP