You are on page 1of 5

1)Connection creation::

$dbhost = localhost;
$dbuser = guest;
$dbpass = ;
$conn = mysql_connect($dbhost, $dbuser, $dbpass);

a)mysql_conncetion(localhost,root,);

2)Connection close:: mysql_close($conn);

3)create databse:: $sql = CREATE DATABASE TUTORIALS;


$retval = mysql_query( $sql, $conn );// for executing

4)drop database:: $sql = DROP DATABASE TUTORIALS;


$retval = mysql_query( $sql, $conn );

5)select database::my_select_db(admin);

6)create table:: $sql = "CREATE TABLE tutorials_tbl( ".


"tutorial_id INT NOT NULL AUTO_INCREMENT, ".
"tutorial_title VARCHAR(100) NOT NULL, ".
"tutorial_author VARCHAR(40) NOT NULL, ".
"submission_date DATE, ".
"PRIMARY KEY ( tutorial_id )); ";
mysql_select_db( 'TUTORIALS' );
$retval = mysql_query( $sql, $conn );

7)drop table:: $sql = "DROP TABLE tutorials_tbl";


mysql_select_db( 'TUTORIALS' );
$retval = mysql_query( $sql, $conn );

8)insert into:: $sql = "INSERT INTO tutorials_tbl ".


"(tutorial_title,tutorial_author, submission_date) ".
"VALUES ".
"('$tutorial_title','$tutorial_author','$submission_date')";
mysql_select_db('TUTORIALS');
$retval = mysql_query( $sql, $conn );

9)fetch:: $sql = 'SELECT tutorial_id, tutorial_title,


tutorial_author, submission_date
FROM tutorials_tbl';
mysql_select_db('TUTORIALS');
$retval = mysql_query( $sql, $conn );

10)mysql_query()//for inserting data..used to exexcute sql command

11) mysql_fetch_array() can be used to fetch all the selected data. This function returns row as an associative array, a numeric
array, or both. This function returns FALSE if there are no more rows.

while($row = mysql_fetch_array($retval, MYSQL_ASSOC))


{
echo "Tutorial ID :{$row['tutorial_id']} <br> ".
"Title: {$row['tutorial_title']} <br> ".
"Author: {$row['tutorial_author']} <br> ".
"Submission Date : {$row['submission_date']} <br> ".
"--------------------------------<br>";

12) Always remember to put curly brackets when you want to insert an array value directly into a string.
In above example, the constant MYSQL_ASSOC is used as the second argument to PHP function
mysql_fetch_array(), so that it returns the row as an associative array. With an associative array, you can access
the field by using their name instead of using the index.

while($row = mysql_fetch_array($retval, MYSQL_ASSOC))


{
echo "Tutorial ID :{$row['tutorial_id']} <br> ".
"Title: {$row['tutorial_title']} <br> ".
"Author: {$row['tutorial_author']} <br> ".
"Submission Date : {$row['submission_date']} <br> ".
"--------------------------------<br>";
}
echo "Fetched data successfully

13)PHP provides another function called mysql_fetch_assoc(), which also returns the row as an associative array.

while($row = mysql_fetch_assoc($retval))
{
echo "Tutorial ID :{$row['tutorial_id']} <br> ".
"Title: {$row['tutorial_title']} <br> ".
"Author: {$row['tutorial_author']} <br> ".
"Submission Date : {$row['submission_date']} <br> ".
"--------------------------------<br>";
}

14)mysql_num:: You can also use the constant MYSQL_NUM as the second argument to PHP function
mysql_fetch_array(). This will cause the function to return an array with numeric index.
Example:
Try out the following e

while($row = mysql_fetch_array($retval, MYSQL_NUM))


{
echo "Tutorial ID :{$row[0]} <br> ".
"Title: {$row[1]} <br> ".
"Author: {$row[2]} <br> ".
"Submission Date : {$row[3]} <br> ".
"--------------------------------<br>";
}
mysql_free_result($retval);

15) mysql_affected_rows Get number of affected rows in previous MySQL operation

mysql_select_db('mydb');

Eg. /* this should return the correct numbers of deleted records */


mysql_query('DELETE FROM mytable WHERE id < 10');
printf("Records deleted: %d\n", mysql_affected_rows()); 10

b) /* with a where clause that is never true, it should return 0 */


mysql_query('DELETE FROM mytable WHERE 0');
printf("Records deleted: %d\n", mysql_affected_rows()); 0

16)update record
/* Update records */
mysql_query("UPDATE mytable SET used=1 WHERE id < 10");
printf ("Updated records: %d\n", mysql_affected_rows());

mysql_query("COMMIT");

Note: Transactions
If you are using transactions, you need to call mysql_affected_rows() after your
INSERT, UPDATE, or DELETE query, not after the COMMIT.
Note: SELECT Statements
To retrieve the number of rows returned by a SELECT, it is possible to
use mysql_num_rows().
Note: Cascaded Foreign Keys
mysql_affected_rows() does not count rows affected implicitly through the use of ON
DELETE CASCADE and/or ON UPDATE CASCADE in foreign key constraints.

18) mysql_client_encoding Returns the name of the character set

19) mysql_client_encoding ([ resource $link_identifier = NULL ] )

Retrieves the character_set variable from MySQL

20) mysql_close::

mysql_close Close MySQL connection

21) mysql_connect Open a connection to a MySQL Server

22) mysql_create_db() attempts to create a new database on the server associated with
the specified link identifier.

$sql = 'CREATE DATABASE my_db';


23) mysql_query() - Send a MySQL query
mysql_num_rows() - Get number of rows in result
mysql_fetch_row() - Get a result row as an enumer

24) mysql_db_name
(PHP 4, PHP 5)

mysql_db_name Retrieves database name from the call to mysql_list_dbs()

25) Returns the database name on success, and FALSE on failure. If FALSE is returned,
use mysql_error() to determine the nature of the error.

26) Note:
For backward compatibility, the following deprecated alias may be
used: mysql_dbname()

27) mysql_db_query Selects a database and executes a query on it

28) mysql_drop_db Drop (delete) a MySQL database

29) $sql = 'DROP DATABASE my_db';

30) For backward compatibility, the following deprecated alias may be


used: mysql_dropdb()
31) mysql_errno

mysql_errno Returns the numerical value of the error message from previous MySQL
operation

32) mysql_error

mysql_error Returns the text of the error message from previous MySQL operation

33) mysql_escape_string

mysql_escape_string Escapes a string for use in a mysql_query

34)

You might also like