How to query info from distant database, PHP/MySQL - php

I have to code an application on my server that triggers emails based on info that come from a database hosted on another server and I don't know how to query that information. I'm using PHP/MySQL. I can't established a connection to this database like I do normally (when database is host on the same server that the application file), right?
Thanks in advance,

Wrong, there is no difference whether code and database are on the same or different servers. As long as the remote db server is configured to allow conections from foreign hosts that is... But that's a hosting issue not a programming one.

I'm not really sure why you wouldn't be able to. I've hosted databases on servers other than my hosting server before.
Try this
$my_db = mysql_connect('yourdb.com:3307', 'myuser', 'mypassword');
if (!$my_db) die('This might be a problem: ' . mysql_error());

If mysql is on another machine , from mysql, you have to give permission for outsider.

Related

Note: Enabling the database statistics here might cause heavy traffic between the web server and the MySQL server

Note: Enabling the database statistics here might cause heavy traffic
between the web server and the MySQL server.
I got this error when I tried to add a new database in BlueHost phpMyAdmin. How can I solve this issue?
First go on your Cpanel
dont open PhpMyadmin
Click on
MySQL® Databases
There Create New Database then Open Phpmyadmin
where your database present
THANKS

How to send data updates to a hosted MySQL database remotely over the internet?

I have a simple MySQL database (one table with 12 rows of data and 5 columns) sitting on the web-server of my host provider.
On my home PC I create the data programmatically and store it in a free version of SQL Server (on my home PC). I would like to "upload" this data to the MySQL db in real time (or as close as I can get) over the internet (I'm assuming this is the only way to connect the pipes).
I know that opening up a MySQL database to a remote internet connection probably is not a "secure" thing to do, but the resulting data table will be publicly available anyway via an "app" so I'm not too worried about that (I suppose a hacker could "overwrite" my data with their own if they were both industrious and inclined) but I think the risk/reward is so small its not a major concern.
Anyway, what is the easiest way to do this with some semblance of security? I only know how to program in VB (I did a little HTML and ASP back in the day, but that was a long time ago). I could learn a few lines of code in another language if need be.
I do not have a static IP, and I've never actually interacted with a MySQL database before (only SQL server, so my MySQL knowledge/ familiarity is zero...but a db is a db, so how hard can it be?). Because of my home network firewall, I can't allow connections "in". I will have to make the connection to the MySQL db "out" from my home PC --> to the hosted database.
Ok this problem is not actually super simple.
What you will find is most shared hosting providers do not allow just any IP to access their databases.
Solution? set the IP for your computer of course! BUT.....you are probably on home internet connection so your IP address can CHANGE (if you have a static IP you are a lucky person!)
So the best way - create a mini-API!
Basically, you want to post your data to a script (with some security of course) that then inserts this data into the database.
It is a lot of work but having done all this before it seems to be the only way unless you have a dedicated server / advanced access privileges!
You could take a look at WAMP for your home pc. It's simple to use.
And then you should take a look at Mysql remote connections(some details here)
I would try this:
At your local computer install MySQL server, there's a free community
edition available for download, try the web installer since its more lightweight. Use the
custom installation and make sure MySql Workbench is selected too.
Workbench has a migration tool for the most common databases, Try this locally, so you can tell if all your data is correctly migrated from your local SQL Server to a MySQL db and there are no data losses in the process.
Then, You could probably connect through Workbench to your online MySQL db
and migrate your data to it directly from your just created local db. In case you cannot connect, make a backup of your local db and send the files to
your server by ftp or similar process. Then, simply restore DB from the backup file on your
online server.
Hope this helps!

How does the connection between mySQL and a Php website work?

I have successfully setup a localhost (Individual Installs) with Apache 2.2, MySQL and PHP, as well as setup a couple virtual-hosts. Currently, I am trying to teach myself command line in MySQL. The part I am lost on is setting up the connection between the database that I create and the virtual-host website I am working on. In my mind I would want to save the database for a specific website in the same folder that I am creating the website. However, I could be way off base. Could some body explain how this piece of the puzzle and how it works. I tried to Google it and all I get is information regarding Work Bench and phpMyAdmin.
The MySQL server runs as a service (or daemon) on your server - so the folder (or directory) you put it in is set by the system configuration and has nothing to do with your document root for your web site.
In PHP, you will "connect" to the MySQL service using PDO or something similar and then send requests to the MySQL server using PHP commands.
When you are running the MySQL command line interpreter, you can also test those same commands by typing them yourself. This is helpful when you're debugging your PHP program so you can see the data that your program will get yourself in the command line interface.
I hope this helps put you in the right picture.
I tried to Google it and all I get is information regarding Work Bench and myPhpAdmin
this is the first entry on the first page when I google php mysql, it covers everything from installation up to code example.
Very generally:
You need to check with your hosting company if they offer you locally hosted MySQL server access. Using cpanel/phpMyAdmin, or whatever tools they have available for you, you can then login and create multiple databases as you please - each database maybe corresponding to one of your site folders. Once you have created your account and your databases, you can use php commands from your website code to connect to/manipulate whichever database you want.
EDIT: I missed where you said you already have a MySQL localhost setup. Now you need to login to your SQL server and create one or more databases and one or more user accounts for those databases. Create an admin account with all the privileges. Then you can use php in your webpage to connect to a database, using something like this:
$con = mysql_connect("localhost", "your_MySQL_username", "your_MySQL_password") or die ("Unable to connect to MySQL Server " . mysql_error());
if(is_resource($con))
$db = mysql_select_db($MySQL_database, $con) or die( "Unable to select database " . mysql_error());
See the manual for more ways to query the database and handle the results: http://php.net/manual/en/ref.mysql.php

php mysql connection confusion

had a bit of confusion going around in work earlier. Thought id run it by yous to see if anyone knows wats going on.
Were working on an internal administration system for our client, and its rite about to launch. Its comprised of TWO MySQL databases on the server [db_1 and db_2] and a PHP front end. [Both databases contain several tables].
There are maybe 90 different PHP files, some of which require a connection to our databases and ALL of these connections are made via a single PHP function which explicitly connects to the first database mentioned above [db_1] and supplies the login and password. And this works fine.
However, our second database, db_2, does not seem to require its own login and password to access its contents.
As soon as we connect to db_1, we seem to have full access to db_2, as long as we use the Full-Name for our tables [ie: db_2.usersTable] -> ("SELECT * FROM db_2.usersTable WHERE...").
And this is what was causing much confusion.
My question is this: Once you connect to a database on a server, do you have access to other databases on that server, or are we overlooking something???
Any feedback greatly appreciated lads...
You usually don't access a database server by a specific database, but by a user that has access to one or more databases.
for example:
mysql_connect("localhost", "user", "password") or die(mysql_error());
connects to the server and not a specific database.
Once connected to the database server you have access to all databases for which that user has permission. You just need to specify the database name in your queries if there are multiple databases that aren't the default.
mysql_select_db("myTable") or die(mysql_error());
sets myTable as the default, but you can still access other tables that the user has permission to.
I might be wrong, but doesn't PHP only connect and authenticate with the MySQL server with a user? And thus, based on that user's permissions, PHP can select any database that user has access to. I would check the permissions for the user you are connecting with...
Once you connect to a database on a
server, do you have access to other
databases on that server
If you have permissions to other databases, then yes. When you connect, you're connecting to the server and setting your default database to the one specified. Which is why you have to explicitly specify db_2 when you want to access it, but you don't have to specify db_1.

PHP database connection

I need to connect to a remote mysql database in PHP.
Specifically I have this as a constant:
define("DATABASE_SERVER", "localhost");
...
$db = #mysql_connect(DB_SERVER, DB_USER, DB_PASS);
I want to copy the site to my local machine but still connect to the main database.
Can I do this? if so, how? All I have is FTP access.
You need the IP/URL of the DB and put that instead of localhost.
You need to make sure the DB configuration allows remote connections/outside the local network.
Once you get the IP of the DB (should be given by the support team, if it is being supported), you will know.
If all you need to do is copy the data and build it locally on your machine, so you don't destroy live data, use the export tab in PHPmyadmin. I assume you don't have too much data.
PHPmyadmin->select the DB (from the left frame)->press the export TAB (top, right frame).
Should be clear from there.
You can export it as SQL and run this SQL on your local machine.
I would guess that your database server is refusing remote connections. If you have cPanel there's a section called something like "Remote Database Access" where you can enable them to certain IP ranges.
I suggest that you export the database in question, and import it locally. This way, you won't destroy live data if you mess up.
Many, if not most, hosting providers also provide a DB interface like phpMyAdmin. You can export the database from there.
+1 gnud. You shouldn't use the production database for development work, even if you could configure it to listen for external access on a public internet interface(*). One slip of the mouse and you're scribbling over important live data. Instead you should be working on a standalone development server of your own, from which you can push the code changes to the live server when you they're working and tested.
(*: Which is in itself a very bad idea: if the passwords are too weak or you're running an old version of MySQL with exploits available you are going to get hacked by automated probes very quickly.)
You'd normally export the database from the shell using the mysqldump dbname > dbdump.sql command, and import it on your local server by piping it into mysql dbname < dbdump.sql. There might also be a MySQL admin web interface installed on the server you could use. But if all you've got is FTP, you haven't really been given the tools you need to do development work. See if you can get an empty or dummy database to test the site with.
(What's more, FTP is a nasty business... really in this century you should be using SFTP.)
The simplest approach is to have DATABASE_SERVER be defined as database (or something equally meaningful) and put an entry in your hosts file. On your local machine this can point to the remote address, on the server this will point to 127.0.0.1.
define('DATABASE_SERVER', database);
On windows your sites file is:
c:\windows\system32\drivers\etc\hosts
with:
database <remote_ip>
Linux:
/etc/hosts
with:
database 127.0.0.1
Side note: is your remote DB a production DB? If so you shouldn't really be using the remote DB for development work, unless you're using a separate dev DB - even then I wouldn't run the risk of rogue dev queries eating up the remote DB resources.
Update: Doh, missed you're limited to FTP access.

Categories