How can I connect to a mysql server from a php page using Synology?

Freakishly picture Freakishly · Aug 16, 2017 · Viewed 14.3k times · Source

I have an older Synology device (DSM v5.2-5967 Update 4, phpmyadmin v4.4.7-0103) that has some local websites with working php pages. I want to migrate this over to my new Synology device (DSM V6.1.3-15152 Update 1, phpmyadmin v4.6.6-0172). When installing the new version of phpmyadmin from Package Center, I am required to download Maria DB and PHP 5.6 as well, whereas this is not a requirement in DSM 5. Further, DSM 6 now supports options in Web Station, where I can configure the http back-end server and PHP version. I set this to 5.6, the one installed alongside phpmyadmin.

Here is the code that used to work for me in the old DSM:

<?php
define ("DB_HOST", "localhost"); // Your database host name
define ("DB_USER", "root"); // Your database user
define ("DB_PASS", ""); // Your database password
define ("DB_NAME", "groceries"); // Your database name

$link = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("Couldn't make connection.");
$db = mysql_select_db(DB_NAME, $link) or die("Couldn't select database");
?>

This code now returns "Couldn't make connection". Other sites that don't handle this return a 500 server error. I've tried replacing localhost with the name of the Synology device, to no avail. PHP files themselves work fine but I cannot connect to a database. I cannot see the control panel for Maria DB anywhere, so I don't know if I'm missing any settings. This is a new device, with a fresh install of packages so I haven't even changed the root password yet.

I even tried installing Apache 2.4 from Package Center, and setting that as the back-end server in the new Web Station settings, and rebooting the NAS. But phpyadmin still shows "nginx/1.11.10" as the web server. For reference, my old NAS shows Apache under web server, but I don't have the Apache package installed.

I'm at a loss. Has anyone tried to connect to a mysql database using Synology DSM 6 and lived to tell the tale?

Answer

Allen Chak picture Allen Chak · Aug 16, 2017

I am using DSM6 with DS716+, and following code is work for me. There are too many cause to reach the connection issue. Such as the extension does not enable, etc. Please use phpinfo() to verify before any other steps. Try to use mysqli extension instead of mysql, attach an example code for you:

<?php
$mysqli = new mysqli("localhost", "dbuser", "dbpassword", "dbname");

$query = "SHOW TABLES";
if ($result = $mysqli->query($query)) {
    while ($row = $result->fetch_row()) {
        printf("%s <br />\n", $row[0]);
    }
    $result->close();
}

$mysqli->close();

?>

enter image description here