bill-lancaster Posted September 18, 2015 Share Posted September 18, 2015 I'm very new php. Have been using mysql for many years but only on a pc (ie not networked). I have set up a pc on my lan as a server and can access it from the local network. I'm having trouble connecting to the mysql db installed on the server pc. The following simple code echos 'start' as in first php line, nothing is output after that. Not even 'Connection failed:' <!DOCTYPE html> <html> <body> <h1>test php</h1> <?php echo "start"; $servername = "localhost"; $username = "username"; $password = "password"; // Create connection $conn = mysql_connect($servername, $username, $password); echo "after"; // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } echo "Connected successfully";?> </body> </html> The server is running Kubuntu 14.04 mysql V14.14 php V5.5.9-1ubuntu4.11 Hope someone has some ideas. Quote Link to comment Share on other sites More sharing options...
Solution mac_gyver Posted September 18, 2015 Solution Share Posted September 18, 2015 do you have php's error_reporting set to E_ALL and display_errors set to ON so that php would help you by reporting and displaying all the errors it detects? the most likely problem is that the mysql_ extension is not installed/enabled in your php configuration. next, the mysql_ functions are obsolete and will be removed from php soon. you should be using the PDO (the best option as it has a more consistent interface) or mysqli_ database functions in your php code. the php version you are using will be throwing a php error at the mysql_connect() statement to alert you about this. lastly, the mysql_ functions don't have an OOP style interface, so there is no ->connect_error properly at all to test. this in itself should be throwing a php error. Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted September 18, 2015 Share Posted September 18, 2015 I have set up a pc on my lan as a server and can access it from the local network.I'm having trouble connecting to the mysql db installed on the server pc. Are you saying MySQL is installed on a different computer to the one you are using to connect to mysql with? Then you cannot use localhost as the hostname. Instead you must specify the hostname/ipaddress for the server pc mysql is installed on. To allow remote connections to MySQL you may need to configure your firewall on the server pc to allow remote connections on port 3306. configure the host permissions for the MySQL user account you are using to allow for remote connections. Quote Link to comment Share on other sites More sharing options...
bill-lancaster Posted September 18, 2015 Author Share Posted September 18, 2015 Thanks, have set /etc/php5/apache2/php.ini as sugested then restarted apache I added this to a simple index.php file $db = new PDO('mysql:host=localhost;dbname=gwl', 'root', 'alex23'); echo "after"; Still no error reported and no echo after the call to PDO Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted September 18, 2015 Share Posted September 18, 2015 there's no guarantee of what php.ini php is using. to find the php.ini that php is using, create a .php script file with the code - <?php phpinfo(); ?> in it and browse to the url of the file. the Loaded Configuration File line in the output is the php.ini that php is using. Quote Link to comment Share on other sites More sharing options...
bill-lancaster Posted September 18, 2015 Author Share Posted September 18, 2015 Yes, it shows /etc/php5/apache2/php.ini as the loaded config file Quote Link to comment Share on other sites More sharing options...
bill-lancaster Posted September 18, 2015 Author Share Posted September 18, 2015 How stupid of me, I assumed that php-mysql was installed - it wasn't. sudo apt-get install php5-mysql Results in a successful connection. Sorry to have wasted your time - at least I learned to use PDO! Thanks again. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted September 18, 2015 Share Posted September 18, 2015 (edited) you should have been getting php errors from your code. having php's errors turned on is critical for debugging problems. if you still are not seeing any php errors, after deliberately introducing problems into your code, the settings for error_reporting/display_errors may be commented out or are using invalid (false values) settings. if you need help with the error settings, post the actual lines for them from your php.ini file. Edited September 18, 2015 by mac_gyver Quote Link to comment Share on other sites More sharing options...
bill-lancaster Posted September 19, 2015 Author Share Posted September 19, 2015 I'm getting errors reported as well. Thanks again. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.