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
Free Courses
HTML
JavaScript
CSS
ASP
PHP
JSP
C++
ASP.NET
SQL
AJAX
FTP
XML

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


Online Education, Online Training for Your Job, Distance Education, E-learning Programs for Your Career!Online Colleges, Online Universities, E-learning & Distance Education for Your Career! Easy Job Training & Online Courses.


   Email A Friend  
 
Earn Online Degree
for Your Job
I found a job after I earned an online degree!
Online classes for finding a Job. Online College, Online University, Degrees, E-learning school, online certificate Earn your
online degree now!
Online Training
for Good Job
Why I work so hard but earn so little? Why I cannot find a high paying job?
Online training for a Good Jobs. Online Degree, E-learning school, online certificate Go to
online school now!

Copyright ©2024, itechcollege.com. All rights reserved
iTechCollege | About iTech | Contact iTech