Koozali.org: home of the SME Server

PHP Connect to MySQL/Mariadb on SME Server 10

Offline rrizley

  • 16
  • +0/-0
PHP Connect to MySQL/Mariadb on SME Server 10
« on: April 13, 2022, 11:57:12 PM »
So after thoroughly searching Google and this forum I have not found a resolution.  I am attempting to connect to my MySQL/MariaDB with PHP on my freshly installed SME Server 10 I get a connection refused.  This I have tried

config setprop mysqld LocalNetworkingOnly no
expand-template /etc/my.cnf
sv t /service/mysqld

but the sv command returns 'unable to change to service directory; file does not exist'

I see on one post that LocalNetworkingOnly no is strongly discouraged I will change that to yes if I get my connection
working.  At this point I am not too concerned about security I am using SME 10 primarily for my own purposes

this I have tried

$mysqliconn = new mysqli("<my host ip address>", "<my user>", "<my user password","<mydatabase>");

and this

$mysqliconn = mysqli_connect("<my host ip address>", "<my user>", "<my user password","<mydatabase>");

I am running my PHP script using putty from the command line: php <my php script>

php version 5.4.16
« Last Edit: April 14, 2022, 12:00:54 AM by rrizley »

Offline Jean-Philippe Pialasse

  • *
  • 2,762
  • +11/-0
  • aka Unnilennium
    • http://smeserver.pialasse.com
Re: PHP Connect to MySQL/Mariadb on SME Server 10
« Reply #1 on: April 14, 2022, 01:21:10 AM »
be carreful that while php cli default is 5.4,  in your ibays via webserver it is 7.4.


that said you are using an Unix/ Linux server so you are expected to user socket connection. 

$host="localhost";
$socket="/var/lib/mysql/mysql.sock";
$port="";

$mysqli = new mysqli($host, $user, $password, $dbname, $port, $socket);


Offline Jean-Philippe Pialasse

  • *
  • 2,762
  • +11/-0
  • aka Unnilennium
    • http://smeserver.pialasse.com
Re: PHP Connect to MySQL/Mariadb on SME Server 10
« Reply #2 on: April 14, 2022, 01:28:58 AM »
$host="localhost:/var/lib/mysql/mysql.sock";

should also work

Offline rrizley

  • 16
  • +0/-0
Re: PHP Connect to MySQL/Mariadb on SME Server 10
« Reply #3 on: April 19, 2022, 11:38:37 PM »
Thank you. I am now getting a Connected successfully but I am having difficulty with my query.  If there is a manual I could review that would be helpful.  I have not found a suitable PHP manual for my version nor a MySQL manual

This is what I have tried

 $QueryString = "SELECT * FROM faxstorage.faxstorage";
 echo "Begin query...\n";
 $QueryResult = $mysqli->query('SELECT * FROM faxstorage.faxstorage');
 echo "End query... $QueryResult\n";
foreach ($QueryResult->fetch_assoc() as $row)
    echo " id = " . $row['filename'] . "\n";
perhaps not good practice to name my DATABASE name same as my TABLE name but it has worked in the past

Offline Jean-Philippe Pialasse

  • *
  • 2,762
  • +11/-0
  • aka Unnilennium
    • http://smeserver.pialasse.com
Re: PHP Connect to MySQL/Mariadb on SME Server 10
« Reply #4 on: April 20, 2022, 04:09:44 AM »
you are using mariadb 5.5
and php 7.4 if running in a web browser. 

if you are testing with cli, the. you need to use the command php74 in place of php.

php74 myscript.php

Offline rrizley

  • 16
  • +0/-0
Re: PHP Connect to MySQL/Mariadb on SME Server 10
« Reply #5 on: April 21, 2022, 11:49:56 PM »
got it to work with this and thought that someone else might find the code useful

 $filename = "'" . "zzzfilename234" . "'";
 $QueryString = "SELECT * FROM faxstorage WHERE filename = " . $filename;
 $QueryResult = $mysqli->query($QueryString);
 foreach($QueryResult AS $rowarray)
  foreach($rowarray AS $rowval)
   echo '~' . $rowval . "~\n";