PHP App/Website wont connect to database, but MySQL Workbench connects to database.

Chris Fauskanger 25 Reputation points
2024-01-28T19:57:43.6966667+00:00

Hi. My website is live and hosted through Azure/Git. I can connect to my database using MySQL Workbench and make tables as normal. But I try to connect my website to the database using the PHP connection string provided to me by Azure. Ive also uploaded the SSL file to my Git directory.
Here is the code azure wants me to connect through.

$con = mysqli_init(); mysqli_ssl_set($con,NULL,NULL, "DigiCertGlobalRootCA.crt.pem", NULL, NULL); mysqli_real_connect($conn, "appname.mysql.database.azure.com", "username", "password", "databasename", 3306, MYSQLI_CLIENT_SSL); Ive tried writing $conn with 1 and 2 n's. Ive tried some different troubleshooting. When I have a <h1> above the php code, it outputs on the website. When have it below the php code it does not out on the website. Also only 1 php tag will display on the webpage. If i try to echo 2 statements within two different php tags only the first tag will output. Please help.

Azure Database for MySQL
Azure Database for MySQL
An Azure managed MySQL database service for app development and deployment.
986 questions
{count} votes

Accepted answer
  1. SSingh-MSFT 16,371 Reputation points Moderator
    2024-02-05T12:55:26.9266667+00:00

    Hi Chris Fauskanger •,

    I'm glad that you were able to resolve your issue and thank you for posting your solution so that others experiencing the same thing can easily reference this! Since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others ", I'll repost your solution in case you'd like to "Accept " the answer.

    Solution verbatim by customer:

    I found the problem! I enabled error logging and downloaded the zip to read out the php errors. The SSL file I downloaded from the Azure Portal had a problem.

    I uploaded the file to my Github without checking the contents of the file. I noticed today that it was completely empty. My local file on my computer was ok, so I re-uploaded the file to my Git and everything is working now. By the way, the "connect from you app" code from the Azure Portal is wrong. There are 2 instances of $con, and 1 instance of $conn. They should all be the same.

    For the second part, support case has been provided.

    If you have any other questions or are still running into more issues, please let me know.
    Thank you again for your time and patience throughout this issue.

    Please remember to "Accept Answer" if any answer/reply helped, so that others in the community facing similar issues can easily find the solution.

    Thanks

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Chris Fauskanger 25 Reputation points
    2024-02-01T20:17:11.6433333+00:00

    I found the problem! I enabled error logging and downloaded the zip to read out the php errors. The SSL file I downloaded from the Azure Portal had a problem.
    I uploaded the file to my Github without checking the contents of the file. I noticed today that it was completely empty. My local file on my computer was ok, so I re-uploaded the file to my Git and everything is working now. By the way, the "connect from you app" code from the Azure Portal is wrong. There are 2 instances of $con, and 1 instance of $conn. They should all be the same

    1 person found this answer helpful.
    0 comments No comments

  2. SSingh-MSFT 16,371 Reputation points Moderator
    2024-01-29T08:28:28.5533333+00:00

    Hi Chris Fauskanger •,

    Welcome to Micrsoft Q&A forum and thanks for using Azure Services.

    As I understand, you are trying to Use PHP to connect and query data in Azure Database for MySQL and could not do so.

    Could you please refer to the official Microsoft documentation here:

    https://learn.microsoft.com/en-us/azure/mysql/single-server/connect-php#step-1-connect-to-the-server

    SSL is enabled by default. You may need to download the DigiCertGlobalRootG2 SSL certificate to connect from your local environment. This code calls:

    $host = 'mydemoserver.mysql.database.azure.com';
    $username = 'myadmin@mydemoserver';
    $password = 'your_password';
    $db_name = 'your_database';
    
    //Initializes MySQLi
    $conn = mysqli_init();
    
    mysqli_ssl_set($conn,NULL,NULL, "/var/www/html/DigiCertGlobalRootG2.crt.pem", NULL, NULL);
    
    // Establish the connection
    mysqli_real_connect($conn, $host, $username, $password, $db_name, 3306, NULL, MYSQLI_CLIENT_SSL);
    
    //If connection failed, show the error
    if (mysqli_connect_errno())
    {
        die('Failed to connect to MySQL: '.mysqli_connect_error());
    }
    

    If the application persistently fails to connect to Azure Database for MySQL, it usually indicates an issue with one of the following:- Server firewall configuration: Make sure that the Azure Database for MySQL server firewall is configured to allow connections from your client, including proxy servers and gateways.

    • Client firewall configuration: The firewall on your client must allow connections to your database server. IP addresses and ports of the server that you cannot to must be allowed as well as application names such as MySQL in some firewalls.
    • User error: You might have mistyped connection parameters, such as the server name in the connection string or a missing @servername suffix in the user name.

    Steps to resolve persistent connectivity issues

    1. Set up firewall rules to allow the client IP address. For temporary testing purposes only, set up a firewall rule using 0.0.0.0 as the starting IP address and using 255.255.255.255 as the ending IP address. This will open the server to all IP addresses. If this resolves your connectivity issue, remove this rule and create a firewall rule for an appropriately limited IP address or address range.
    2. On all firewalls between the client and the internet, make sure that port 3306 is open for outbound connections.
    3. Verify your connection string and other connection settings. Review How to connect applications to Azure Database for MySQL.
    4. Check the service health in the dashboard. If you think there's a regional outage, see Overview of business continuity with Azure Database for MySQL for steps to recover to a new region.

    Let us know if above helps. If not, please share screenshot of the error and more details so that we can investigate.Thanks

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.