MySQL

Posted on Updated on

Image result for MySQL Running ProcessMySQL Running Process

Before you can get content out of your MySQL database, you should know how to build up an association with MySQL from inside a PHP script. To perform fundamental questions from inside MySQL is simple. This article will demonstrate to you proper methodologies to get up and running.

How about we begin. The primary thing to do is interface with the database.The capacity to associate with MySQL is called mysql_connect. This capacity gives back an asset which is a pointer to the database association. It’s additionally called a database handle, and we’ll utilize it in later capacities. Bear in mind to supplant your association points of interest.

<?php
$username = “your_name”;
$password = “your_password”;
$hostname = “localhost”; 

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) 
  or die(“Unable to connect to MySQL”);
echo “Connected to MySQL<br>”;
?>

All going great, you ought to see “Associated with MySQL” when you run this script. On the off chance that you can’t associate with the server, ensure your secret word, username and hostname are right.

Once you’ve associated, will need to choose a database to work with. We should expect the database is called ‘illustrations’. To begin working in this database, you’ll require the mysql_select_db() work:

<?php
//select a database to work with
$selected = mysql_select_db(“examples”,$dbhandle) 
  or die(“Could not select examples”);
?>

Now that you’re associated, we should attempt and run a few questions. The capacity used to perform questions is named – mysql_query(). The capacity gives back an asset that contains the aftereffects of the inquiry, called the outcome set. To analyze the outcome will utilize the mysql_fetch_array work, which gives back the outcomes push by column. On account of an inquiry that doesn’t return comes about, the asset that the capacity returns is essentially an esteem genuine or false.

<?php
//execute the SQL query and return records
$result = mysql_query(“SELECT id, model, year FROM cars”);
//fetch tha data from the database
while ($row = mysql_fetch_array($result)) {
echo “ID:”.$row{‘id’}.” Name:”.$row{‘model’}.”
“.$row{‘year’}.”<br>”;
}
?>

Finally, we close the connection. Although this isn’t strictly speaking necessary, PHP will automatically close the connection when the script ends, you should get into the habit of closing what you open.

<?php
//close the connection
mysql_close($dbhandle);
?>

Here is a code in full:

<?php
$username = “your_name”;
$password = “your_password”;
$hostname = “localhost”;//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die(“Unable to connect to MySQL”);
echo “Connected to MySQL<br>”;//select a database to work with
$selected = mysql_select_db(“examples”,$dbhandle)
or die(“Could not select examples”);

//execute the SQL query and return records
$result = mysql_query(“SELECT id, model,year FROM cars”);

//fetch tha data from the database
while ($row = mysql_fetch_array($result)) {
echo “ID:”.$row{‘id’}.” Name:”.$row{‘model’}.”Year: “. //display the results
$row{‘year’}.”<br>”;
}
//close the connection
mysql_close($dbhandle);
?>

To create ‘examples’ database on your MySQL server you should run the following script:

CREATE DATABASE `examples`;
USE `examples`;
CREATE TABLE `cars` (
`id` int UNIQUE NOT NULL,
`name` varchar(40),
`year` varchar(50),
PRIMARY KEY(id)
);
INSERT INTO cars VALUES(1,’Mercedes’,’2000′);
INSERT INTO cars VALUES(2,’BMW’,’2004′);
INSERT INTO cars VALUES(3,’Audi’,’2001′);

For more details and queries please feel free to email, visit or call us. Wishing you the very best for all your future endeavors.

Helpline: 9814666333, 8699444666

Email:info@technocampus.co.in

**************************************************************************************************************

Please fill the FORM and we shall contact you soon.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s