I am fairly new at web development; I am currently trying to create a website that allows users to Submit information and have it stored in a database. I have GoDaddy for my domain name and Hosting, and I use Google Cloud Platform for the cloud database storage.
I have successfully figured out how to store HTML form data into a local database (PhpMyAdmin); I have also successfully connected my Google Cloud instance with another local database using MySQL Workbench.
I cannot figure out how to get everything connected; do I need to connect my Google Cloud instance with GoDaddy? Should I just use GoDaddy's Database offering? I tried putting the IPv4 address of my instance as the 'hostname' in my PHP connection code, which I uploaded to GoDaddy, but it is not connecting and I don’t know why. I don't even know if this is what you are supposed to do?
I’ve read about having to allow permissions for particular IP addresses to store information, but that makes no sense to me as in the real world, I can simply go to a website, fill out a form, and my information is then sent to that company's’ remote database somewhere. That’s what I’m trying to accomplish for my website. Thank you.
PS: I'm using PHP as my server-side language.
Here is a general function that you can use to connect to a Google Cloud SQL instance:
function connectSQL( $dbProject, $dbRegion, $dbInstance, $dbIP, $dbName, $dbUsername, $dbPassword, $ssl = TRUE, $persistent = FALSE, $appEngine = FALSE ) {
// $dbProject is the project that owns the database, which may differ from the project using the database
// Create a connection
$db = NULL;
if ( $appEngine ) {
// Connect from App Engine
if ( $persistent ) {
try {
$db = new pdo( "mysql:unix_socket=/cloudsql/$dbProject:$dbRegion:$dbInstance;dbname=$dbName", $dbUsername, $dbPassword, array( PDO::ATTR_PERSISTENT => TRUE ) );
} catch(PDOException $ex) {
echoArr( $ex );
return FALSE;
}
} else {
try {
$db = new pdo( "mysql:unix_socket=/cloudsql/$dbProject:$dbRegion:$dbInstance;dbname=$dbName", $dbUsername, $dbPassword );
} catch(PDOException $ex) {
echoArr( $ex );
return FALSE;
}
}
} else {
// Connect from a non-App Engine environment (localhost, other hosting, etc.)
if ( $persistent ) {
if ( !$ssl ) {
try{
$db = new pdo( 'mysql:host='.$dbIP.':3306;dbname='.$dbName, $dbUsername, $dbPassword, array( PDO::ATTR_PERSISTENT => TRUE ) );
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $ex) {
echoArr( $ex );
return FALSE;
}
} else {
try{
$db = new pdo( 'mysql:host='.$dbIP.':3306;dbname='.$dbName, $dbUsername, $dbPassword, array(
PDO::MYSQL_ATTR_SSL_KEY=>'/path/to/client-key.pem',
PDO::MYSQL_ATTR_SSL_CERT=>'/path/to/client-cert.pem',
PDO::MYSQL_ATTR_SSL_CA=>'/path/to/server-ca.pem',
PDO::ATTR_PERSISTENT => TRUE
)
);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $ex) {
echoArr( $ex );
return FALSE;
}
}
} else {
if ( !$ssl ) {
try{
$db = new pdo( 'mysql:host='.$dbIP.':3306;dbname='.$dbName, $dbUsername, $dbPassword );
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $ex) {
echoArr( $ex );
return FALSE;
}
} else {
try{
$db = new pdo( 'mysql:host='.$dbIP.':3306;dbname='.$dbName, $dbUsername, $dbPassword, array(
PDO::MYSQL_ATTR_SSL_KEY=>'/path/to/client-key.pem',
PDO::MYSQL_ATTR_SSL_CERT=>'/path/to/client-cert.pem',
PDO::MYSQL_ATTR_SSL_CA=>'/path/to/server-ca.pem'
)
);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $ex) {
echoArr( $ex );
return FALSE;
}
}
}
}
return $db;
}
You can use this regardless of where your script is hosted.
Related
This script
<?php
/* Connect to a MySQL database using driver invocation */
$dsn = 'mysqldg';
$user = 'odbc_dg';
$password = '999999999';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>
gives the error *Connection failed: invalid data source name*
I have created an entry in /etc/odbc.ini as follows:
[mysqldg]
Description = DGDB
Driver = mysql537
Database = dg1
Servername = 99.99.99.99
UID = odbc_dg
PWD = 999999
SSLKeyFile = /etc/mysql/ssl/ck.pem
SSLCertFile = /etc/mysql/ssl/cc.pem
SSLCAFile = /etc/mysql/ssl/c1.pem
/etc/odbcinst.ini has the following entry:
[mysql537]
Description = MySQL driver for Plesk
Driver = /usr/lib/odbc2/lib/libmyodcb5w.so
Setup = /usr/lib/odbc2/lib/libmyodbc5w.so
The entry in odbcinst.ini works with a non-DSN connection.
I'm obviously missing something, can anyone help? Thanks.
UPDATED....
I have tried Your Common Sense's code as follows:
<?php
$host = '46.99.199.199';
$db = 'dg';
$user = 'odbc_dg';
$pass = '999999';
$charset = 'utf8mb4';
$options = array(
PDO::MYSQL_ATTR_SSL_KEY => '/etc/mysql/ssl/ck.pem',
PDO::MYSQL_ATTR_SSL_CERT => '/etc/mysql/ssl/cc.pem',
PDO::MYSQL_ATTR_SSL_CA => '/etc/mysql/ssl/c1.pem'
);
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
try {
$pdo = new PDO($dsn, $user, $pass, $options);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>
... but I get a connection fail message - Connection failed: SQLSTATE[HY000] [2002]
I think the problem is that the keys I have supplied only work with ODBC
For example this code, which uses odbc_connect works....
<?php
ini_set ('error_reporting', E_ALL);
ini_set ('display_errors', '1');
error_reporting (E_ALL|E_STRICT);
$user = "odbc_dg";
$pass = "99999";
$connection = "Driver= {mysql537};Server=46.99.199.199;Database=dgdb;UID=dgdb;PWD=999999;sslca=/etc/mysql/ssl/c1.pem;sslkey=/etc/mysql/ssl/ck.pem;sslcert=/etc/mysql/ssl/cc.pem";
$con = odbc_connect($connection, $user, $pass);
$sql="SELECT Id from stk_item";
$rs=odbc_exec($con,$sql);
if (!$rs) {
exit("Error in SQL");
}
echo "<table><tr>";
echo "<th>Companyname</th>";
echo "<th>Contactname</th></tr>";
while (odbc_fetch_row($rs)) {
echo odbc_result($rs, "Id"), "\n";
}
odbc_close($con);
echo "</table>";
?>
My problem is that I want to connect to the remote database via pdo because that's the only type of connection allowed in the Drupal synchronising code.
I am developing a dynamic website and I'm using both local and remote host. I've tried to implement a code in which I stablish connection first with the localhost. If connection to the locahost is false or can't be established, I go to the second connection (remote server or host). But this time, I want to do something different. Now I want to know if it's possible to connect to two hosts/servers at the same time with no errors. The databases and tables are the same. I'm asking this, because I just want to avoid sql backup. What do you say? Moreover, I want to use the same variable $pdo, so I don't need to change or repeat all connections to the tables.
This is my current code:
<?php
try {
//Local Host - XAMPP
$dsn = 'mysql:host=localhost;dbname=bananas';
$user = 'root';
$pw = '';
$sessionpath = 'C:/xampp/tmp';
$pdo = new PDO($dsn, $user, $pw);
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) { //$e
//echo 'Error: '.$e->getMessage();
if(!isset($pdo) || $pdo == false){
try{
//Remote Host
$dsn = 'mysql:host=bananas123;dbname=bananas';
$user = 'mybigbanana';
$pw = '6969';
$sessionpath = '/php_sessions';
$pdo = new PDO($dsn, $user, $pw);
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e) {
echo 'Error: '.$e->getMessage();
}
}
}
?>
Note: I don't want to do something like having two variables for each connection, like, for example, $pdo and $pdo2. I want only one variable for both connections, which is $pdo.
$dbname='database_name';
$dbuser='database_user';
$dbpass='database_password';
$dbhost='localhost';
$con = new PDO("mysql:host=$dbhost;dbname=$dbname;charset=utf8", $dbuser, $dbpass);
$con->query("SET NAMES 'utf8'");
How can I check if database_name, database_user, database_password are correct or if the database exists.
As it has been suggested, use a try/catch:
$dbname='database_name';
$dbuser='database_user';
$dbpass='database_password';
$dbhost='localhost';
try {
$dbh = new PDO("mysql:host=$dbhost;dbname=$dbname;charset=utf8", $dbuser, $dbpass);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
// if you do not want to output message you can log the errors
echo $e->getMessage();
$errorCode = $e->getCode();
}
I have a download script which get id of file and search in database and find it's name. But when I include my db connection the files get corrupted on download.
when I comment my db connection and give file name manually file downloading work fine.
I test my db connection and there wasn't any excpetion or any html output , what do you think my problem is?
<?php
session_start();
try{
$db= new PDO("mysql:host=localhost;dbname=dbname","user","pass");
$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute( PDO::ATTR_EMULATE_PREPARES, false );
$db->exec("SET NAMES 'utf8'");
}catch (Exception $e){
//echo "something wrong in db.php";
echo $e->getMessage();
exit;
}
?>
I run my code on windows server IIS, if it does matter
Just try with this code..
error_reporting(E_ALL);
ini_set('display_errors','1');
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';
try {
$dbh = new PDO($dsn, $user, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
It will return your error.
after few days struggling and headache finaly problem resolved by deleting try catch block
I can not connect to a remote server with PDO:
public function dbConnection ($dbHost, $dbName, $dbUsername, $dbPass){
try{
$dbChain = 'mysql:host='.$dbHost.';dbname='.$dbName;
$dbh = new PDO($dbChain, $dbUsername, $dbPass);
print_r($dbh->errorInfo());
die;
...
errorInfo() only returns something if I have for instance the wrong user or password. With the right one it does not report anything, but $dbh remains empty.
errorInfo()
Array
(
[0] =>
[1] =>
[2] =>
)
$dbh
object(PDO)#5 (0) {
}
It's a remote server, but I have access using for instance MySQl Workbench, from the same machine. For the local MySQL server DB the same code works just fine.
What could be wrong with the remote connection?
Edit:
public function dbConnection ($dbHost, $dbName, $dbUsername, $dbPass){
try{
$dbChain = 'mysql:host='.$dbHost.';dbname='.$dbName;
$dbh = new PDO($dbChain, $dbUsername, $dbPass);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e){
echo $e->getMessage();
$errorCode = $e->getCode();
var_dump($errorCode);
}
I tried this and I get the same empty result.
object(PDO)#5 (0) {
}
Edit2: Same result.
$dbh = new PDO($dbChain, $dbUsername, $dbPass
, [PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
You should use a catch, after setting the proper error attributes, to see if there are errors -
try {
$dbChain = 'mysql:host='.$dbHost.';dbname='.$dbName;
$dbh = new PDO($dbChain, $dbUsername, $dbPass);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
echo $e->getMessage();
$errorCode = $e->getCode();
}