Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8121663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:33:57+00:00 2026-06-06T05:33:57+00:00

I am trying to connect to a MySQL server using PHP’s ‘mysql_connect()’ function, but

  • 0

I am trying to connect to a MySQL server using PHP’s ‘mysql_connect()’ function, but the connection fails. This is my code:

$con = mysql_connect("example.net", "myusername","") or die("Could not connect: ".mysql_error()); 

I placed this code inside a PHP script, which I try to open using a web browser (the script is stored on a remote host which has PHP enabled) but it doesn’t work. It doesn’t return the die error either. Echoing something before the $con successfully outputs in the browser, whereas nothing outputs after that line. If I type:

mysql -h example.net -u myusername

from a remote machine, I could connect to the DB without any problem and do queries and other modifications.

Update :
I also tried this after some suggestion, but no improvement:

<?php
    $usern = "myusername";
    $dbh = new PDO('mysql:host=servername.net;dbname=test', $usern, "");
    echo $usern;
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T05:33:59+00:00Added an answer on June 6, 2026 at 5:33 am

    What operating system is the remote host running PHP using? Perhaps MySQL isn’t enabled in php.ini. Also, please don’t use mysql_* functions for new code. They are no longer maintained and the community has begun the deprecation process (see the red box). Instead, you should learn about prepared statements and use either PDO or MySQLi. If you can’t decide which, this article will help you. If you care to learn, this is a good PDO tutorial.

    Have you tried using PDO or the MySQLi interface? If you’re trying to learn PHP, you should not be using the mysql_* functions regardless. See if you can access the database by using a line similar to this:

    $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
    

    If you need more detailed documentation, this code comes directly from the documentation itself.

    EDIT: Also, try using PDO’s error checking functionality. This example creates a database connection using PDO and tries to perform a simple query. It doesn’t use prepared statements or any of those features, so it’s not production-ready code (i.e. *don’t just throw this into your code without understanding how to improve it) and you’ll need to edit it to include a SELECT query that’s relevant to your database, but it should at least tell PDO to provide more information about the errors it encounters.

    <?php
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    
    $dbhost     = "localhost";
    $dbname     = "test";
    $dbuser     = "root";
    $dbpass     = "admin";
    
    // database connection
    $conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
    
    // query
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "SELECT * FROM booksa";
    $q = $conn->query($sql) or die("ERROR: " . implode(":", $conn->errorInfo()));
    
    $r = $q->fetch(PDO::FETCH_ASSOC);
    
    print_r($r);
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to connect to remote MySQL server with SSL from PHP using mysql_connect:
My php code fails to connect to the mysql database on my web server.
I'm trying to connect to SQL Server using PHP, I'm learning this language. I
I'm trying to connect to a mysql server at dreamhost from a php scrip
I have been trying to connect to mysql all evening using PHP. I know
When I am trying to connect to MySQL server on my debian server, it
I'm trying to connect to a MySQL 5 database using the MySQL ODBC 5.1
i get this following error when i am trying to connect my localhost mysql
Iam trying to make a connection between my database on the MYSQL(iam using the
I am trying to connect to a remote mysql database but I get the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.