How to connect to mysql server from php page using Synology?

I have an earlier Synology device (DSM v5.2-5967 Update 4, phpmyadmin v4.4.7-0103) which has some local websites with working php pages. I want to transfer this 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 the package processing center, I also need to download Maria DB and PHP 5.6, while this is not a requirement in DSM 5. In addition, DSM 6 now supports settings in Web Station, where I can configure the http server and PHP version. I installed this on 5.6, the one that is installed with phpmyadmin.

Here is the code that worked 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");
?>

" ". , , 500. localhost Synology, . PHP , . Maria DB , , - . , root.

Apache 2.4 - NAS. phpyadmin - "nginx/1.11.10" -. , NAS Apache -, Apache.

. - mysql Synology DSM 6 , ?

+4
3

DSM6 DS716 +, . . , .. , phpinfo() . mysqli mysql, :

<?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

PS: , ,

+1

mysql-server ? #service mysql status #mysql . #mysql -u USERNAME -p #mysql -u USERNAME -h localhost -p . . mysql man

+1

you need to add a port.

I use PDO, but you can usemysql_connect

  try
  {
     $pdo = new PDO("mysql:host=servernameport=3307;dbname=database", "username", "password");
  }
  catch(PDOException $e)
  {
     echo $e->getMessage();
  }
      return $pdo;

To find out which port you need, go to maria db if im right this package is installed automatically during installationphpmyadmin

+1
source

Source: https://habr.com/ru/post/1683792/


All Articles