cross server access in php - php

Can I get data from three different servers on my php application? Actually I have my data on three different servers and I want to generate a report with having data from all three servers. Can you please suggest me a code for this?
function dbcon(ipaddress,servername,serverpassword,databasename)
{
$con = mysql_connect(ipaddress,servername,serverpassword);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db(databasename) or die ("Cant find DB");
}

Certainly that is possible. I assume (though you are not very clear in this) that you are talking about three database servers? Then all you have to do is:
make sure the database servers are accessible via network (tcp)
create three database connections instead of only one
Since opening a connection to a database server returns a handle you have something you can use to address a specific connection. The syntax required for opening the connection is clearly explained in the manual pages. I suggest you read them and take a look at the provided examples...

First:
Welcome to Stack Overflow! Please, don't use mysql_* functions in new code. They are no longer maintained and the deprecation process has begun on it. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
In order to connect to three different databases, you'll need 3 connection objects, and query each separately. Make sure you have configured the receiving ends to correctly accept connections.

Related

How to connect MySQL to PHP?

So I'm brand spanking new to mysql and php.
I'm set up with Mysql workbench and I'm practicing building a site using Notepad++ and just run it through Chrome. All I want to do is create a sign up page, which I'm assuming I use a .php page on the site, where it would be a username and password. That's it. I can't seem to find any tutorials on how to connect mysql to the .php page, or how to create a sign in page. Any help would be appreciated!
Welcome to PHP!
Typically a connection is established on a PHP page with something along the lines of this:
$conn = mysqli_connect("localhost","[username]","[password]","[databasename]") or die("Error " . mysqli_error($conn));
The "or die" will produce an error if there's a problem establishing a connection. Also, this uses the newer "mysqli_" method for connection; make sure when you call this connection in future that you use mysqli_ methods (there are still traditional "mysql_" methods available, but are depreciated).
Hope this helps!
M
here you go you, here you can find a way to properly connect to the database as well as all the data you need to get set up with your signup form
http://mrbool.com/how-to-create-a-sign-up-form-registration-with-php-and-mysql/28675

Sql Lite connection. Is it possible with Msqli procedural?

I have a project with MySQLi procedural and PHP. People ask me to make it work off line. I have been searching and I could find sql lite. I have installed it with MAMP. I have managed to create the same table that I have in MySql. Now I try to make the connection. I have google it and I could find ways to do it with PDO. It works well for me and I could apply it. But before going further I would like to be sure that there is no way to do that with MySQLi procedural (this would save me a lot of time and problems if I do not have to change everything to PDO).
So, my question: is it possible to use MySQLi procedural to make a connection with Sql Lite? If so, what would be the equivalent of this:
$con = mysqli_connect("localhost","root","root");
if (!$con) {
die('The connection with the server failed: ' . mysqli_error());
}
//Database name is : 'test'
if (!mysqli_select_db($con, 'test')){
echo "The connection with the database failed";
}
(this is where I have the sql lite database: /Applications/MAMP/db/sqlite/test
I have a Mac, I have installed MAMP php lite Admin, and I am very new to all this. I have manage to do it alone, just with Google but I am not sure if I am going in the right direction. I have no experience. I would I appreciate any suggestion of someone with any experience )

Best practice for handling mysql connection in a social network system

Currently, I am dealing with a social network startup. We use php & mysql for back-end development. I have a datamanager.php that have functions which handles the sql connections and queries. I have several functions like signup, login, get_profile, etc.
I have 2 choices for handling mysql connection
1) create a globally reachable $connection variable with mysqli_connect(); and don't close the connections until I finish all the database operations.
2) create a function namely db_connect() that returns the connection variable in each database functions. Also, in each database function like login, signup, I close the connection with mysqli_close(); and get another connection variable with db_connect();
So, which choice is better for handling mysql connections and why ?
It's better to keep the MySQL connection open as of performance. Better use a class to handle all mysql connections and somehow pass the instance of the class (i.e. the object itself) to all other functions/classes which need it. Within the class you could establish the connection ONCE and than keep it!
Firstly, on the assumption that you are building something that needs to have a long shelf life, and scale to lots of users, with a roadmap for new features, I'd encourage you to use an off-the-shelf PHP framework; Zend or Symfony seem to be the front runners. These frameworks provide a lot of the plumbing for you, so you won't have to worry about database connections.
However, specifically answering your question, I'd encourage you to use mysql_pconnect - it frees you up from worrying about opening and closing connections. You do need to read the manual, it's not totally straightforward...

Beginner question on mySQL and PHP to avoid connecting to DB on every page

I code a simple php/mysql web page, that there is page1.php, page2.php and so on. Because I make use of the database on every page (or at least the 90% of them) I place on the top of them the standard
mysql_connect("localhost"," "," ");
mysql_select_db(" ");
.
.
mysql_close();
with my queries.
My question is do I really need to connect to the database on each page or is there any way to avoid this and still stay connected? Some of the pages are linked to the others and I can make use of SESSIONS to post some variables, but my question goes to something more globalized.
The web works in a disconnected state by nature. Meaning that you have no idea if the client is going to come back for a second request or not.
Regardless you absolutely want to connect/disconnect from the database on every single page. This is the only way to ensure you aren't leaking connections and the site can stay responsive.
Most systems have built in ways to handle connection pooling which makes the act of requesting a new connection very very fast and therefore something you don't have to worry about.
You can use mysql_pconnect for a persistent connection, although its not going to help you that much and it can be a big pain to do properly. Its almost just better to connect on every page, especially if the database server is running on the same machine as the php server.
Try using
mysql_pconnect()
From PHP.net
"acts very much like mysql_connect() with two major differences.
First, when connecting, the function would first try to find a (persistent) link that's already open with the same host, username and password. If one is found, an identifier for it will be returned instead of opening a new connection.
Second, the connection to the SQL server will not be closed when the execution of the script ends. Instead, the link will remain open for future use (mysql_close() will not close links established by mysql_pconnect())."
If you just want to make it so that you don't have to hard code it into the top of every file write the connection code in a file then use require /path/to/file/name.php and it will establish it everytime Note: it might be include and not require.

Where is the most secure place to put MySQL connection details for PHP

Where is the most secure place to put MySQL database connection details when connecting via a PHP script?
$connection = mysql_connect($hostname, $username, $password);
if (!$connection) {die('Could not connect: ' . mysql_error());}
mysql_select_db($database, $connection);
I know it's not a good idea to put them directly in the script that is querying the database. However, some say that you should put the connection details in a separate PHP script and include that script to connect.
Is there a more secure place to put the connection details?
The usual practice is to put them into a separate file, and put that outside the web root. See this answer for details.
You may want to put it in a directory that is not in the webapp hierarchy, to prevent a browser from having any chance to access it.
Depending on your level of paranoia, you could write a webservice that is behind a firewall, and call for the credentials, but that may be overkill.
It would help if we understood your architecture, is it just php script goes to database, then the first suggestion would be best, if you have firewalls, for example, then you will have more options.
If your OS has reasonable security features, it doesn't matter so much where, as long as the file belongs to a group with the minimum possible rights.

Categories