I have a complete setup of PHP4.4 and MySQL database how would I make connection with the PHP to MySQL this connection would be important point of view as would update my database or upload data to my database.
I have a complete setup of PHP4.4 and MySQL database how would I make connection with the PHP to MySQL this connection would be important point of view as would update my database or upload data to my database.
MySQL is a very popular database and there are lot of tutorials available on the web about using PHP with MySQL.If you want to use a database in your application, you have to make config.php file which will contain basic database data. Here we will declare database path, username, password, database name and create connection string. Put the code below into config.php file and put it in the root folder of your project.
First 4 lines are basic database data. 2 lines below is connection string which connects to server and then mysql_select_db selects database. The last line is optional but it will suggest that data will be in utf8 format. Now we have config.php file created and saved in the root folder of our project.PHP Code:
<?php
$host = "localhost"; //database location
$user = "User1"; //database username
$pass = "Newuser123"; //database password
$db_name = "practise_data"; //database name
//database connection
$link = mysql_connect($host, $user, $pass);
mysql_select_db($db_name);
//sets encoding to utf8
mysql_query("SET NAMES utf8");
?>
To connect to the database, I did the following steps:
I created my own MySQL database on my computer, using the prompt screen of mySQL (you can also do it via phpMyAdmin). I added a database called database1 (the same as the real database) and two tables, Name and Addresses. I also added some data. MySql Database without showing any detailed information about connection setting, But if you want to show the success or error message then write code in following way
$conn = mysql_connect ($host, $username, $pass);
if (! $conn) {
die (”A connection to the Server could not be established!”);
} else {
echo “Root login”,$host,” successful.”;
To include config.php into application (lets say it’s a website) simply put next line on the top of the source code of index.php. That will connect to the mySQL database with the currently defined variables. So, obviously, if $connect does that, what would $connect2 do? This will do the following.
PHP Code:
<?php
$connect2=mysql_connect($host2,$user2,$pass2); //Connect to the database.
?>
Bookmarks