PHP Mysql function
1- mysql_num_rows()

his function returns the number of rows in database. if row not exist in database this returns 0.

Note:-This function is use for only select query. other query insert,delete and update use mysql_affected_rows.

Return Type :- int

<?php

$conn=mysql_connect('localhost','root','');
if(!$conn)
die('Not connected');
$database=mysql_select_db('newportal',$conn);
if(!$database)
die('Database Not connected');

$query="SELECT * FROM new_story";
$data=mysql_query($query);

$num=mysql_num_rows($data);
echo $num;

?>

Output:-
7

2- mysql_affected_rows()

This function returns the number of rows affected by the mysql operation. if operation not success this function -1.

Return Type:- int

<?php

$conn=mysql_connect('localhost','root','');
if(!$conn)
die('Not connected');
$database=mysql_select_db('test',$conn);
if(!$database)
die('Database Not connected');

$query="delete from category where id<3";
mysql_query($query);

$rows=mysql_affected_rows();
echo "Number rows affected by this query ".$rows;
mysql_close($conn);

?>

Output :-
Number rows affected by this query 2
3- mysql_real_escape_string()

When we use special characters(\n,',") in sql query. Then sql query does not work properly. if we use this function then this function ignore these special characters and sql query response properly.

Note:- suppose we insert string with double quotes(") through sql query. so this sql query does not work properly without mysql_real_string() function.

Return Type :- string

<?php

$conn=mysql_connect('localhost','root','');
if(!$conn)
die('Mysql Not Connected');
$database=mysql_select_db('test',$conn);
if(!$database)
die('DataBase Not Connected');

$name=mysql_real_escape_string("This is \'my' name");

$query="insert into product(product_name) values('$name')";

mysql_query($query);

mysql_close($conn);

?>

Output :-

3- microtime()

This function returns the current UNIX timestamp with microseconds.

Note :- microsecond(1/1000000) in 1 second . that is very small unit.

Return Type:- mixed

<?php

echo microtime();

?>

Output :-
0.43751100 1287612083
here 0.43751100 is microsecond and 1287612083 is unixtimestamp
4- mysql_error()

This function display error details of the mysql operation.

Return Type :- string

<?php

$conn=mysql_connect('localhost','root','');
if(!$conn)
die('Mysql Not Connected');
$database=mysql_select_db('test',$conn);
if(!$database)
die('DataBase Not Connected');

$name="This is \'my' name";

$query="insert into product(product_name) values('$name')";

mysql_query($query);

echo mysql_error();

mysql_close($conn);

?>

Output :-
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server
version for the right syntax to use near 'name')' at line 1

5- mysql_errno()

This function display error number of mysql_operation.

Return Type :- int

<?php

$conn=mysql_connect('localhost','root','');
if(!$conn)
die('Mysql Not Connected');
$database=mysql_select_db('test',$conn);
if(!$database)
die('DataBase Not Connected');

$name="This is \'my' name";

$query="insert into product(product_name) values('$name')";

mysql_query($query);
echo mysql_errno();
mysql_close($conn);

?>

Output :-
1064

6- mysql_insert_id()

This function display autogenerate id from previose mysql operation.

Note :- auto generate id means auto increment columns must be in table.

Return Type :- int

<?php

$conn=mysql_connect('localhost','root','');

if(!$conn)
die('Mysql Not Connected');
$database=mysql_select_db('test',$conn);
if(!$database)
die('DataBase Not Connected');

$name=mysql_real_escape_string("This is \'my' name");

$query="insert into product(product_name) values('$name')";

mysql_query($query);

echo mysql_insert_id();

mysql_close($conn);

?>

Output :-
15

Some Important Mysql function(We have discuss these function in previous classes)

mysql_close()
mysql_connect()
mysql_fetch_array()
mysql_fetch_assoc()
mysql_fetch_object()
mysql_fetch_row()
mysql_query()
mysql_select_db()

Some Important Mysql function(Brief discussion)

mysql_list_dbs() :- This function display database name in mysql. Return type (resource).
mysql_num_fields() :- This function display total number of fields in database. Return type (int)
mysql_field_name() : This function return field name in a recordset. Return type (string)


Previous
Next