How do I connect to mysql database via PHP?
I have a ubuntu VM created on Azure and an SQL database and I am trying to connect to my database via PHP.
I have tried using the PHP connection string provided within the database connection examples and this fails - I get a PDO exception:
Error connecting to SQL Server.PDOException Object ( [message:protected] => could not find driver [string:Exception:private] => [code:protected] => 0 [file:protected] => /var/www/html/index.php [line:protected] => 22 [trace:Exception:private] => Array ( [0] => Array ( [file] => /var/www/html/index.php [line] => 22 [function] => __construct [class] => PDO [type] => -> ) ) [previous:Exception:private] => [errorInfo] => ) 1
For info I am running PHP 8.3.6 on my VM and I have mysqli installed and enabled - I have done all the latest updates, I have the PDO library installed and enabled and pdo_mysql v8.3.6 installed and enabled.
I have checked my databse connection via Windows Powershell as Admin as per the Azure/SQL Connectivity checker code and I was able to connect to my database - it passed all the connection tests.
I have set the firewall permissions to allow my VM ip access to the database.
I have also tried to connect via the code provided here using the SSL certificate as per the answer: https://stackoverflow.com/questions/52425321/mysqli-real-connect-failed
I installed the certificate on my VM and used the following code:
{
$db = mysqli_init(); mysqli_ssl_set($db,NULL,NULL,"/var/www/html/certificate/BaltimoreCyberTrustRoot.crt.pem",NULL,NULL);
$connection = mysqli_real_connect($db, 'citytour.database.windows.net','uname@uname','mypassword','citytour',3306,NULL, MYSQLI_CLIENT_SSL);
if(mysqli_connect_errno($db))
{
echo "<br><b>Failed to connect to MySQL: " . mysqli_connect_error()."<b>";
}
else
{
$sql = 'show tables ';
$result = mysqli_query($db,$sql);
$rows = array();
if(!empty($result))
{
while ($row = mysqli_fetch_assoc($result)){
$rows[] = $row;
echo '<pre>'; print_R($row);
}
}
}
```I didn't get any errors but it didn't work - can anyone tell me how to find the error when mysqli_real_connect fails? That would be really useful - I know it is this line it is failing at.
Also I don't know how to:
1. Enable SSL connection in azure server.
1. Directly run the MySQL command "status" in the database and check **SSL: Cipher in use is AES256-SHA**
Can anyone explain in details the steps I do for the above? Do I need to run a command in PowerShell or similar? Do I do this from my VM or within my database server?
For info my server name is citytour.database.windows.net.
I suspect the issues are one of the following:
a) SSL isn't enabled
b) PDO doesn't support PHP 8.3.6
c) my database is the wrong sort of database for this type of connection
Anyone able to help me troubleshoot this?