0

first of all my problem is the following.

I am trying to connect to a database in my local network using a php script.

my php script includes the following lines

<?php
echo "1";
$conn = new mysqli("192.168.xxx.xxx", "user", "password", "testdb");
echo "2";
?>

I am only getting output the first echo statment. The second one is missing. It seems to me as if the script stops and can not continue for reasons unknown to me.

My Setup is: -Two lubuntu VM's in Virtualbox connected via the Host-Only option

-First VM - Apache2, php7.3, mysql-server & mysql-client, phpmyadmin.

-Second VM - Apache2, php7.3, mysql-client.

Everything is up-to-date and on the exact same version.

basically Im trying to establish a connection from VM2 to VM1 via php to use that on a website (running on VM2) and manipulate db entries from this website.

Things I have found out and done until now:

-If I am using this script on the machine with the db installed and use "localhost" it displays both echo while viewing the website which means to me that it is successfully connected.

-the db user im using is definitely allowed to connect from remote and is not bound to localhost.

-the both VMs are reachable for eachother and I can establish a mysql connection from VM2 to VM1 through CLI.

What am I missing?

  • Can you put a try catch block around the connection and update? You may also try this: `$mysqli = new mysqli("localhost", "user", "password", "database"); if ($mysqli->connect_errno) { echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; }` – Santhosh J Apr 16 '20 at 10:38
  • not really familiar with try catch blocks in php. tried your other suggestion and now it isnt even displaying my first echo – Bigos Igos Apr 16 '20 at 10:44
  • 1
    @SanthoshJ adding such blocks will be 1. Absolutely useless in this particular case. 2. Considered a bad practice in general. – Your Common Sense Apr 16 '20 at 10:56
  • "What I am missing? - an error message. Every time your code doesn't work, you are missing the error message. Always look for one. Unlike random strangers, *It can tell you what is wrong* with your code. – Your Common Sense Apr 16 '20 at 10:58
  • Thank you! yeah I was wondering why there is no message! – Bigos Igos Apr 16 '20 at 11:01
  • To know what is the error, you need to have an exception handler or at the minimum, check the connect_errno, as I gave in my first comment. And, no exception handling is useless. – Santhosh J Apr 16 '20 at 11:15
  • 1
    @YourCommonSense I am not really familiar with php as a language trying to learn it. so I simply have to add the one line "mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);" infront of my php file and edit the php.ini? – Bigos Igos Apr 16 '20 at 11:50
  • @BigosIgos yes. – Your Common Sense Apr 16 '20 at 11:51
  • @YourCommonSense Thank you! but where exactly do I find the output of this? it seems to stop after the first line with `"mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);"`` put some echo statements to test this out. – Bigos Igos Apr 16 '20 at 12:03
  • read the linked post a little further. you need to enable error reporting in PHP – Your Common Sense Apr 16 '20 at 12:16
  • @SanthoshJ please read this article https://phpdelusions.net/articles/error_reporting – Your Common Sense Apr 16 '20 at 12:17
  • used "error_reporting(E_ALL); ini_set('display_errors', 1); " and now it shows something! thank you! – Bigos Igos Apr 16 '20 at 12:27

0 Answers0