Connecting to mysql from your server - php

I am trying to upload a portfolio site which uses a mysql Database, I have been just Xampp for testing it, but now I wanna upload and I keep getting can't connect to DB when I upload to my site, let me know what I need to put instead of local to connect to schellshockdesign.com
thanks
<?php
//connect to database
$db = mysql_connect( 'localhost', 'blog_ss_user', 'secret' );
//choose the right DB
mysql_select_db( 'schells_portfolio', $db ) or die( 'could not connect to DB' );

http://www.cyberciti.biz/tips/how-do-i-enable-remote-access-to-mysql-database-server.html
You have to enabled remote access so that you can connect remotely.

You have to do/ check two things:
Check that you have permissions to access the MySQL server remotely.
Type the correct domain name that the MySQL server is located at.

Start reading about json_encode. Crete a secret php file on your live server. Pass the data from local to live like http://www.domain.com/secretfile.php?data=$json; Now, catch with $_GET['data'], decode the json with json_decode($_GET['data']); and establish the Db connectiuon and insert your data.

Insert your website address into the connection process
$db = mysql_connect( 'schellshockdesign.com', 'blog_ss_user', 'secret' );

Related

mysql - create event and load data infile to Server automatically

I need to know if it's possible to Create an Event in MySQL with its code statement using Load Data In file syntax.
Here's my current situation.
I can get the database (MS Access) using PHP from a Local Biometric Database.
I have updated the database in my Local Database Server which is MySQL now.
I can update my target Online Database Server via Exporting it first then Importing the database in the Online Database Server.
My Main Question is how I can manage this without any user interaction
such as MySQL Event Features?
I am trying to upload a data from my PC (Local server) into our Domain (Web Server)
Already done this guys, What i did is I've built a local application to my local server at our office then that application is doing a FTP service to upload the updated database from our Local Biometric Database into the online web server.
Once all this is done, I've created a php script which will load the .SQL file using a Cron Job. You all know cron job are time scheduled events for controlling scripts and doing other linux command.
I'll share my code of php file so everyone who encounter this can use this in the future.
date_default_timezone_set('Asia/Taipei');
$CurTime = date('Y-m-d H:i:s');
$link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link){
die('Failed to connect to server: ' . mysqli_error());
}
//Select database
$db = mysqli_select_db($link,DB_DATABASE);
if(!$db){
//Do auto download of database in the Server automatically
if(file_exists('Database/'.DB_DATABASE.'.sql')){
mysqli_query($link,"CREATE DATABASE ".DB_DATABASE);
$db = mysqli_select_db($link,DB_DATABASE);
$readContent = file_get_contents("Database/".DB_DATABASE.".sql");
$res = mysqli_multi_query($link,$readContent) or die(mysqli_error($link));
mysqli_close($link);
} else {
die("Unable to select database, Please call your system administrator. Thank you.");
}
}
function Reload_Database($DBLink){
if(file_exists($DBLink)){
//mysqli_query($link,"DROP DATABASE ".DB_DATABASE);
//mysqli_query($link,"CREATE DATABASE ".DB_DATABASE);
$link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);
$db = mysqli_select_db($link,DB_DATABASE);
$readContent = file_get_contents($DBLink);
$res = mysqli_multi_query($link,$readContent) or die(mysqli_error($link));
mysqli_close($link);
return 'Database Reloaded';
} else {
die("Unable to reload database, Please call your system administrator. Thank you.");
}
}
echo Reload_Database('../employee/database/'.DB_DATABASE.'.sql');
This is basic script that will reload the database of the uploaded database given from my local application.
From here the cron job will do its part. Expect that my local application is also a service that will just do the uploading of the updated database of the Local Biometric Database.

Database connection phpmyadmin

I'm working for an e-commerce that has the db on phpmyadmin. In another website I'd like to connect to that database. I have password, username and db name, so I'm using this "connection string":
<?php
$nomehost = "localhost";
$nomeuser = "user";
$password = "pass";
// connection
$conn=mysql_connect($nomehost,$nomeuser,$password);
if (!$conn) exit ("Error connection<br>");
// db name
if (!mysql_select_db("db_name",$conn)) exit("Error db name<br>");
?>
The result is "Error db name". What can I do? Have I to set some oprion in the phpmyadmin?
First of all:
this error is caused by the fact that you are selecting the wrong database in your MySql server. Is your db called db_name???
EDIT: based on the comments you are making: is the server that hosts the php page the same as the mysql server?
Then:
phpmyadmin is just a tool to connect and handle MySql databases and is not a database server itself.
Last but most important:
you are using a deprecated library (mysql) in php to connect to a MySql server. Please consider moving to mysqli or better to PDO

connecting a mysql database using php to a host

Okay I'm having trouble connecting my database to my host..
Im using myPHPAdmin and I made a database on my host
the problem is in my php code where I define my connection to the host
everything worked fine when I did this on the localhost.
But now that I want to use it on my personal domain it wont work
it doesnt access the data in the database.
Im new at this so dont shoot me ;)
$connect = mysql_connect("host", "user", "pass");
so for my host/domain I enter my domain the-bloodgod.com ?
what do I enter for user and pass? is it just the login I normally use to access the Cpanel? Or do I have to create special permissions in myphpadmin?
also on my hosts myphpadmin it shows that it stores all created tables in the_bloodgod_com database collection
so would this be correct if I put it the code bellow?
mysql_select_db("the_bloodgod_com");
$sql="SELECT * FROM tablename";
MOST hosting companies you have to use "localhost" as the host name for your database connection code. PHP:
$link = mysqli_connect("localhost","username","password","database") or die("Error " . mysqli_error($link));

Having Trouble Connecting to MySQL Database

The following code successfully connects me to my mySQL database.
$conn = mysql_connect("localhost", "root", "") or die("Failed to Connect!");
$db = mysql_select_db("MyDB");
I have been experimenting on localhost using XAMPP and phpmyadmin, and everything works correctly. However, today I uploaded my website to my domain (I have bought a domain and web hosting through GoDaddy) and I keep getting "Failed to Connect!" whenever this code runs. The HTML/CSS work correctly, but I cannot connect to mySQL. How can I connect to my database on the server?
You'll need to change your connection information here:
mysql_connect("localhost", "root", "")
to include your GoDaddy database details instead. Contact GoDaddy for more information on what to use for your account.
You have not mentioned anything about MySQL database hosting.
If you are hosting the database also in godaddy, you should be able to get the connection string and host name from information mentioned here
First, You need to create database in your hosting server (phpmyadmin cpanel) and supply details in your database connect file.
**Looking at your database credentials I can say that you haven't created one yet since
the username is definitely not 'root' and password will be required.**
Sample db_connect file:
<?php
$hostname = 'e.g: internal-ra.db-server-123';
$username = 'e.g: serverone1234d4';
$password = 'e.g: your_own_password';
try {
$pdo = new PDO("mysql:host=$hostname;dbname=database_name_here", $username, $password);
/*** echo a message saying we have connected ***/
}
catch(PDOException $e){
echo 'Exception -> ';
var_dump($e->getMessage());
}
?>
And try to use so less as possible the key DIE()

Moving MySQL from localhost to remote server

I’ve just finished writing a website which is working very well on my local server (xampp).
The following is my connection database definitions for the local server:
<?php
$host="localhost";
$username="root";
$password="1234";
$db_name="partnership";
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
?>
I’ve loaded my files successfully and put them on the remote server (www.mydomain.com) and I’ve created the relevant database ‘partnership’ (successfully imported all Tables from phpMySQL local server to the remote server).
I can view the website (www.mydomain.com) but with no connection to the database (MySQL).
The error message is of course "cannot connect".
I’ve changed $host="localhost" to $host=”www.mydomain.com” but still getting the same error message.
Any assistance on the above issue would be greatly appreciated.
Shouldn't you still use localhost as $host if you're trying to connect via the application? The script is on the same server as the DB.
If you are using cPanel, when you create the MySQL table, make sure you assign your user to it (trivial, I know, but Ive forgotten about it a few times myself.)
If your script is running from your remote server, just stick with localhost. Will work fine.
Last, my only suggestion until a better answer is said, try using
$host = 'localhost';
$user = 'username'; // Im pretty sure your username isn't root.
$pass = '1234';
$db = 'partnership';
/* Alot of hosts like to append your cPanel login to your db and username fields.
Check to see what your table is. It might actually be 'youruser_partnership' */
$mysqli = new mysqli($host, $user , $pass, $db);
if (mysqli_connect_error()) {
die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
}
If your host supports mysqli (most do). If not, when you do your die statement, still use die(mysql_error()); to get the exact error. It will more than likely be your username/password you created and assigned to your database
Ah, this is always the most annoying part of the process. Try this and see if it works:
$host="127.0.0.1"; // "localhost" should work, but don't count on it
$username="user"; // username you use to log into phpMyAdmin
$password="password"; // password you use to log into phpMyAdmin
$db_name="dbname"; // database you want to connect to
You'll need to have created this user/password pair and given them the appropriate permissions to access the database. Also note that the mysql PHP functions are deprecated and should not be used. PDO or mysqli are the preferred ones going forward.

Categories