iTech College - Internet Technology, Online Education, E-Learning, Technician Training, Tech Tutorials, Distance Education, Virtual Learning PHP Course: Use Database - www.itechcollege.com PHP Course: Use Database - www.itechcollege.com
Online Degrees Scholarships & Fellowships Internships Student Financial Aid Study Abroad Itech Bookstore
Free Courses
HTML
JavaScript
CSS
ASP
PHP
JSP
ASP.NET
SQL
AJAX
FTP
XML
Career Center
Job List
Web Jobs
Student Jobs
Computer Jobs
Engineer Jobs
Developer Jobs
Job Search
Resume Writing
Job Interview
Salary Info


itech > courses > PHP > Use Database

Use Database

Let's use MySql. database. To use a database, the first thing is to connect to the database server:

$host     = "server_name"; 
$user     = "database_user";    
$password = "password"; 
$dbn      = "database_name"; 

mysql_connect($host, $user, $password) or 
  die ("Cannot connect to the database server");
mysql_select_db($dbn) or die ("Cannot connect to the database");

After opne the database, we use the mysql_query funcation and a SQL query:

$q_sql = mysql_query("select * from members"); 
while ($rs=mysql_fetch_row($q_sql)) {
 ... 
}

Usually, this function is used with mysql_fetch_row or mysql_fetch_array to get the result of the SQL query row after row in an array:

$q_sql = mysql_query("select * from members"); 
while ($rs=mysql_fetch_row($q_sql)) { 
$lastName = $rs[3]; 
$firstName = $rs[4]; 
echo $lastName . ", " . $firstName;
}

Or:

$q_sql = mysql_query("select * from members"); 
while ($rs=mysql_fetch_array($q_sql)) { 
$lastName = $rs["lastName"]; 
$firstName = $rs["firstName"]; 
echo $lastName . ", " . $firstName;
}

When we have finished to use the database, we should close the connection with the server:

mysql_close();

To insert into a table, we use a statement something like below:

$query = "INSERT INTO  user_comments(lastName, firstName, email, comments, date)"; 
$query .= " VALUES ("$lastName", \"$firstName\", \"$email\", \"$comments\", $date)";    
mysql_query($query) or die ("cannot insert the message");
name description
mysql_connect used to connected to a MySQL server
mysql_select_db used to connect to a database
mysql_query performs the SQL query on a given database
mysql_close close the connection to a database server
mysql_fetch_row gets the result of the query row after row in an enumerated array
mysql_fetch_array gets the result of the query row after row in an associative array
mysql_num_rows returns the number of rows of the result
mysql_data_seek moves the internal pointer of a MySQL result to a given position
  Back to Index Page
Next Page >>   Basic Tags
Statements Comments and Characters
echo
Variables
Array
Loop
The If Statement
Function
Send and get Query String
Date and Time
Cookies
Environment Variables
Use Database
Create Graphics
 

PHP Books
PHP Jobs



Bookmark:  del.icio.us digg facebook Reddit Mixx BlinkList Fark Furl NewsVine Simpy Spurl Segnalo Technorati blogmarks Google YahooMyWeb Windows Live



PHP Course: Use Database - www.itechcollege.com
Copyright ©2008, itechcollege.com. All rights reserved
iTechCollege | About iTech | Contact iTech