mysql_list_tables

This function return List of tables in a MySQL database.

Syntax: mysql_list_tables (database, connection)

Description:

The first argument “database” is Required.It specifies the name of the database.

The second argument “connection” is Optional.It specifies the MySQL connection. If not specified, the last connection opened by mysql_connect() or mysql_pconnect() is used.

This function deprecated. It is preferable to use mysql_query() to issue TABLES [FROM db_name] [LIKE ‘pattern’] statement instead.

Example:
Code:

$database_name = 'company';

if (!mysql_connect('localhost','mysql_user', 'mysql_password')) {
   echo 'Could not connect to mysql';
    exit;
}

$sql = "SHOW TABLES FROM $database_name";
$result = mysql_query($sql);

if (!$result) {
    echo "DB Error, could not list tables\n";
    echo 'MySQL Error: ' . mysql_error();
    exit;
}

while ($row = mysql_fetch_row($result))  {
    echo "Table: {$row[0]}\n";
}

mysql_free_result($result);

Output:

Table: companydata

mysql_insert_id()

This function returns the AUTO_INCREMENT ID generated from the previous INSERT operation.

Syntax: mysql_insert_id(connection)

Description:

The argument “connection” is Optional. It specifies the MySQL connection. If not specified, the last connection opened by mysql_connect() or mysql_pconnect() is used.

This function returns 0 if the previous operation does not generate an AUTO_INCREMENT ID, or FALSE on
MySQL connection failure.

Example:
Code:

$con = mysql_connect("localhost", "mysql_user", "mysql_password");
if (!$con)
   {
  die('Could  not connect:' . mysql_error());
   }

$db_selected = mysql_select_db("company",$con);

$sql = "INSERT INTO companydata VALUES (5,'smith',3000)";
$result = mysql_query($sql,$con);
echo "ID of last inserted record is: " . mysql_insert_id();

mysql_close($con);

Output:

ID of last inserted record is: 5

mysql_real_escape_string()

This function escapes special characters in a string for use in an SQL statement

The following characters are affected:

* \x00
* \n
* \r
* \
* ‘
* ”
* \x1a

Syntax: mysql_real_escape_string(string,connection)

Description:

The first argument “string” is Required. It specifies the string to be escaped.

The second argument “connection” is Optional. It specifies the MySQL connection. If not specified, the last connection opened by mysql_connect() or mysql_pconnect() is used.

This function returns the escaped string on success, or FALSE on failure.

Example1:
Code:


$connection = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
    OR die(mysql_error());


$query = sprintf("SELECT * FROM users WHERE user='%s' AND password='%s'",
            mysql_real_escape_string($user),
            mysql_real_escape_string($password));

Example2:
Code:


$query = "SELECT * FROM users WHERE user='{$_POST['username']}' AND password='{$_POST['password']}'";
mysql_query($query);


$_POST['username'] = 'Tom';
$_POST['password']  = "' OR ''='";

echo $query;

mysql_escape_string()

This function Escapes a string for use in a mysql_query

Syntax: mysql_escape_string (unescaped_string )

Description:

The argument “unescaped_string” is Required.It specifies the string that is to be escaped.

This function will escape the unescaped_string, so that it is safe to place it in a mysql_query(). This function is deprecated.

This function is identical to mysql_real_escape_string() except that mysql_real_escape_string() takes a connection handler and escapes the string according to the current character set. mysql_escape_string() does not take a connection argument and does not respect the current charset setting.

Example:
Code:

$item = "samsung's mobile";
$escaped_item = mysql_escape_string($item);
printf("Escaped string: %s\n", $escaped_item);

Output:

Escaped string: samsung\’s mobile

mysql_drop_db()

This function Drop (delete) a MySQL database.

Syntax: mysql_drop_db (database_name , connection )

Description:

The first argument “database_name” is Required.It specifies to drop the database.

The second argument “connection” is Optional. It specifies the MySQL connection to close. If not specified, the last connection opened by mysql_connect() is used.

It is preferable to use mysql_query() to issue a sql DROP DATABASE statement instead.

Example:
Code:

$connection = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if  (!$connection) {
    die('Could   not connect: ' . mysql_error());
}

$sql = 'DROP DATABASE library';
if  (mysql_query($sql, $connection)) {
    echo "Database library was successfully dropped\n";
} else {
    echo 'Error dropping database: ' . mysql_error() . "\n";
}

Output:

Database library was successfully dropped

Page 1 of 812345...Last »
eXTReMe Tracker