PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Wed Oct 17, 2018 9:36 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 14 posts ] 
Author Message
PostPosted: Fri Feb 16, 2018 9:31 am 
Offline
Forum Newbie

Joined: Fri Feb 16, 2018 9:23 am
Posts: 7
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.

Syntax: [ Download ] [ Hide ]
<?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


Top
 Profile  
 
PostPosted: Fri Feb 16, 2018 4:40 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13580
Location: New York, NY, US
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()

_________________
(#10850)


Top
 Profile  
 
PostPosted: Sat Feb 17, 2018 2:21 pm 
Offline
Forum Newbie

Joined: Sun Nov 15, 2015 12:57 pm
Posts: 18
Better yet, use PDO. Here is a tutorial to get you going.
https://phpdelusions.net/pdo


Top
 Profile  
 
PostPosted: Tue Feb 20, 2018 4:56 am 
Offline
Forum Newbie

Joined: Fri Feb 16, 2018 9:23 am
Posts: 7
Thank you Christopher

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

Syntax: [ Download ] [ Hide ]
<?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:

Quote:
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


Top
 Profile  
 
PostPosted: Tue Feb 20, 2018 5:14 am 
Offline
Forum Newbie

Joined: Fri Feb 16, 2018 9:23 am
Posts: 7
Thanks benanamen.
I have tried to use PDO, as follows:

Syntax: [ Download ] [ Hide ]
<?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:

Quote:
MY SITE

blah blah1


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

Mark


Top
 Profile  
 
PostPosted: Tue Feb 20, 2018 5:40 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6424
Location: Montreal, Canada
$charset is not defined. Also, turn on error reporting while you're developing.

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Tue Feb 20, 2018 3:11 pm 
Offline
Forum Newbie

Joined: Fri Feb 16, 2018 9:23 am
Posts: 7
Thanks - I have added and included the $charset, and I have turned on error reporting.
This is the output I now get:

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


Top
 Profile  
 
PostPosted: Tue Feb 20, 2018 5:10 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13580
Location: New York, NY, US
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.

_________________
(#10850)


Top
 Profile  
 
PostPosted: Mon Feb 26, 2018 4:20 am 
Offline
Forum Newbie

Joined: Fri Feb 16, 2018 9:23 am
Posts: 7
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!


Top
 Profile  
 
PostPosted: Tue Feb 27, 2018 9:01 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13580
Location: New York, NY, US
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?

_________________
(#10850)


Top
 Profile  
 
PostPosted: Fri Mar 09, 2018 4:49 am 
Offline
Forum Newbie

Joined: Fri Feb 16, 2018 9:23 am
Posts: 7
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


Top
 Profile  
 
PostPosted: Fri Mar 09, 2018 3:02 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13580
Location: New York, NY, US
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.

_________________
(#10850)


Top
 Profile  
 
PostPosted: Tue Mar 13, 2018 5:15 am 
Offline
Forum Newbie

Joined: Fri Feb 16, 2018 9:23 am
Posts: 7
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


Top
 Profile  
 
PostPosted: Wed Mar 14, 2018 2:00 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13580
Location: New York, NY, US
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

_________________
(#10850)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group