Post reply

The message has the following error or errors that must be corrected before continuing:
Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.

Name:
Email:
Subject:
Message icon:
Verification:
Please leave this box empty:

Type the letters shown in the picture
Listen to the letters / Request another image

Type the letters shown in the picture:

shortcuts: alt+s submit/post or alt+p preview

Topic Summary

Posted by VelMurugan
 - May 19, 2009, 06:09 PM
Here is a code in full:

Code: [Select]
<?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:

Code: [Select]
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');
Posted by VelMurugan
 - May 19, 2009, 06:08 PM
How to connect to MySQL database using PHP

Before you can get content out of your MySQL database, you must know how to establish a connection to MySQL from inside a PHP script. To perform basic queries from within MySQL is very easy. This article will show you how to get up and running.

Let's get started. The first thing to do is connect to the database.The function to connect to MySQL is called mysql_connect. This function returns a resource which is a pointer to the database connection. It's also called a database handle, and we'll use it in later functions. Don't forget to replace your connection details.

Code: [Select]
<?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 well, you should see "Connected to MySQL" when you run this script. If you can't connect to the server, make sure your password, username and hostname are correct.

Once you've connected, you're going to want to select a database to work with. Let's assume the database is called 'examples'. To start working in this database, you'll need the mysql_select_db() function:

Code: [Select]
<?php
//select a database to work with
$selected mysql_select_db("examples",$dbhandle
  or die(
"Could not select examples");
?>


Now that you're connected, let's try and run some queries. The function used to perform queries is named - mysql_query(). The function returns a resource that contains the results of the query, called the result set. To examine the result we're going to use the mysql_fetch_array function, which returns the results row by row. In the case of a query that doesn't return results, the resource that the function returns is simply a value true or false.

A convenient way to access all the rows is with a while loop. Let's add the code to our script:

Code: [Select]
<?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.

Code: [Select]
<?php
//close the connection
mysql_close($dbhandle);
?>



Source : webcheat
IT Acumens Web Designing Chennai | GinGly :: Build your Personal Website | CineBuzz :: Cinema News | My Kids Diary :: Gift your Kids Memories :: Book Website @ 349 Rs monthly
Copyright 2005 - 2021 :: IT Acumens :: All Rights Reserved. :: XML Sitemap
ITAcumens Discussion Forum with 2 lakhs post running for 15 years - Powered by IT Acumens Pro Dedicated Server

My Kids Diary