Page 1 of 1

mysql connect not connecting

Posted: Fri Feb 16, 2018 8:31 am
by markcarter1974
Hello PHP Dev Network

I have a database on localhost that works fine.
I have tried to get this onto Google Cloud with XXAMP and Bitnami following all the steps on the following link:
https://www.apachefriends.org/docs/host ... oogle.html

The php files have been transferred to the google cloud. I am trying to access them in the google cloud, and I can access them there. However, the connection to the db and/or the mySQL query I using in the php file does not work. Phpmyadmin works and I am able to log into it in the cloud and amend things and run SQL from within phpmyadmin.

Code: Select all

<?php
echo"<b>MY SITE";

$dbhost = '12.345.67.890'; // this is the URL, and it works successfully to give me access to phpmyadmin, so I know it is correct
$dbuser = 'root';
$dbpass = 'AbcdEFghIJk'; // this is the password I use to get into phpmyadmin
$db = 'pred'; // this is the name of the database

echo"<p>blah blah";

mysql_connect($dbhost,$dbuser, $dbpass) or die ('Unable to connect to database! Please try again later.');
mysql_select_db($db);

?> 
However, this runs only the first part of the code and doesn't connect to the db:
"MY SITE

blah blah"

Any suggestions on what I need to do to get this working?
Much appreciated

Mark

Re: mysql connect not connecting

Posted: Fri Feb 16, 2018 3:40 pm
by Christopher
1. Do not user the mysql database extension. User the mysqli extension. They are very similar. the mysql extension is not longer supported.

2. Display the error message that the database is reporting by using mysqli_error()

Re: mysql connect not connecting

Posted: Sat Feb 17, 2018 1:21 pm
by benanamen
Better yet, use PDO. Here is a tutorial to get you going.
https://phpdelusions.net/pdo

Re: mysql connect not connecting

Posted: Tue Feb 20, 2018 3:56 am
by markcarter1974
Thank you Christopher

First, I have changed to mysqli, so my code now looks like:

Code: Select all

<?php
echo"<b>MY SITE";

$dbhost = '12.345.67.890'; // this is the URL, and it works successfully to give me access to phpmyadmin, so I know it is correct
$dbuser = 'root';
$dbpass = 'AbcdEFghIJk'; // this is the password I use to get into phpmyadmin
$db = 'pred'; // this is the name of the database

echo"<p>blah blah";

mysqli_connect($dbhost,$dbuser, $dbpass, $db);

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

echo"<p>blah blah2";
?> 
The response I get is:
MY SITE

blah blah1
Failed to connect to MySQL: Connection timed out

blah blah2
Does that help to diagnose the problem?

(I will now look into PDO, benanamen)

Mark

Re: mysql connect not connecting

Posted: Tue Feb 20, 2018 4:14 am
by markcarter1974
Thanks benanamen.
I have tried to use PDO, as follows:

Code: Select all

<?php
echo"<b>MY SITE";

$dbhost = '12.345.67.890'; // this is the URL, and it works successfully to give me access to phpmyadmin, so I know it is correct
$dbuser = 'root';
$dbpass = 'AbcdEFghIJk'; // this is the password I use to get into phpmyadmin
$db = 'pred'; // this is the name of the database
echo"<p>blah blah1</p>";

$dsn = "mysql:host=$dbhost;dbname=$db;charset=$charset";
$opt = [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES   => false,
];
$pdo = new PDO($dsn, $dbuser, $dbpass, $opt);

echo"<p>blah blah2";
?>
And I get only:
MY SITE

blah blah1
So there still seems to be problems connecting to the db.
Any help or suggestions much appreciated.

Mark

Re: mysql connect not connecting

Posted: Tue Feb 20, 2018 4:40 am
by Celauran
$charset is not defined. Also, turn on error reporting while you're developing.

Re: mysql connect not connecting

Posted: Tue Feb 20, 2018 2:11 pm
by markcarter1974
Thanks - I have added and included the $charset, and I have turned on error reporting.
This is the output I now get:
MY SITE

blah blah1

Fatal error: Uncaught PDOException: SQLSTATE[HY000] [2002] Connection timed out in /opt/bitnami/apache2/htdocs/testing_connection2.php:21 Stack trace: #0 /opt/bitnami/apache2/htdocs/testing_connection2.php(21): PDO->__construct('mysql:host=12.3...', 'root', 'AbcdEFghIJk', Array) #1 {main} thrown in /opt/bitnami/apache2/htdocs/testing_connection2.php on line 21
Any ideas?

Re: mysql connect not connecting

Posted: Tue Feb 20, 2018 4:10 pm
by Christopher
Typically that error is if you do not have remote access enabled in you MySQL server. But you said you have phpmyadmin running on the same site connecting to this server. Are you sure that this code and phpmyadmin are on the same site? It sounds like a configuration problem.

Re: mysql connect not connecting

Posted: Mon Feb 26, 2018 3:20 am
by markcarter1974
Yes, phpmyadmin works fine on this connection
So the connection is working, and I believe the SQL and PHP are corerct also

How would I test some of the common configuration issues and diagnose and fix the problem?
Help much appreciated!

Re: mysql connect not connecting

Posted: Tue Feb 27, 2018 8:01 pm
by Christopher
So when you say in your code "$dbpass = 'AbcdEFghIJk'; // this is the password I use to get into phpmyadmin" I assume that is the password that you put into the config.php file? Not HTTP access or some other password?

And that phpMyAdmin and the script above are in the same web server public directory?

Is MySQL on a different server or the same server?

Re: mysql connect not connecting

Posted: Fri Mar 09, 2018 3:49 am
by markcarter1974
Hi Christopher,

Apologies, I know these should be easy questions to answer - but I am quite new to all this!

First, I have not changed anything in the config.php file directly. The password I am using in the MySQL is the password to Bitnami Cloud Hosting. It is the password supplied in the end of Step 4 of the instructions I have followed:
https://www.apachefriends.org/docs/host ... oogle.html

Your second and third questions are tricky for me to answer, sorry - I assume that phpmyadmin and the php scripts are in the same place, and that MySQL is on the same server. If you can tell me how I might check these things, then I could check and confirm.

Mark

Re: mysql connect not connecting

Posted: Fri Mar 09, 2018 2:02 pm
by Christopher
markcarter1974 wrote:Your second and third questions are tricky for me to answer, sorry - I assume that phpmyadmin and the php scripts are in the same place, and that MySQL is on the same server. If you can tell me how I might check these things, then I could check and confirm.
Check the URL bar in the browser to see where phpmyadmin and the php scripts. It is possible that phpmyadmin is part of a control panel somewhere and they have auto-configured it to work.

Re: mysql connect not connecting

Posted: Tue Mar 13, 2018 5:15 am
by markcarter1974
Thanks for your help, really appreciated.

phpmyadmin works at:
http://127.0.0.1:8888/phpmyadmin/
But not at:
http://12.345.67.890/phpmyadmin/

phpinfo.php works at both:
http://127.0.0.1:8888/phpinfo.php
http://12.345.67.890/phpinfo.php

The SQL scripts seem to work the same at both:
http://127.0.0.1:8888/home.php
http://12.345.67.890/home.php
However with the connection problems we have already discussed.

Does this help you to help me?

Mark

Re: mysql connect not connecting

Posted: Wed Mar 14, 2018 2:00 pm
by Christopher
Given those URLs, I think you may need to specify the port in the DSN and use 127.0.0.1. See the manual:

http://php.net/manual/en/ref.pdo-mysql.connection.php