3. Functions may be used in CRUD
mysql_connect()
mysql_select_db()
include()
mysql_query()
mysql_fetch_array()
mysql_num_rows()
4. mysql_connect()
mysql_connect()is designed to handle the
connection.
You can use this function internally or you can
include it from an external source.
This function takes 3 arguments by default.
host, username, password
mysql_connect("localhost","admin",
“admin");
5. mysql_connect()
To check whether the connection is established or
not we store this function into a variable and check
it with IF statement.
<?php
$con=mysql_connect("localhost","admin","admin");
if(!$con){
die('Could not connect: ' . mysql_error());
}
?>
You can use this piece of code anywhere in your
code anytime, but that’s might be used several
times. Then you can put your code in a separate file
6. mysql_connect() In a Separated File
You code up your connection and db selection into
a separate file, then you include that file into the
main file. Ex: connect.php contains the connection.
<?php
....
include („connect.php‟);
....
?>
7. mysqlSelect_db()
Once you have successfully connected to My SQL,
then you must chose a database to work on.
Another PHP fucnction is used to select a database
from database server.
mysql_select_db(“users”);
8. Query Conversion
We have SELECT and INSERT queries in Mysql
and simply use them during inserting and
retrieving. BUT if we want to use them with PHP
we must convert them into PHP code.
This conversion is done by a built-in PHP function
called mysql_query()
mysql_query ("INSERT INTO users (username,
password) VALUES ('$uname', '$pword')")
9. Query Conversion
mysql_query("SELECT * FROM users
WHERE username= „ahmad'");
When we use SELECT statement, each time it returns
one row of a table.
To store the returned data into a variable we must
fetch them one by one and store them.
This can be done by a function called
mysql_fetch_array()