02-03-2012, 02:54 PM
PHP MySQL Functions+
Definition and Usage
The mysql_connect() function opens a non-persistent MySQL connection.
This function returns the connection on success, or FALSE and an error on failure. You can hide the error output by adding an '@' in front of the function name.
Definition and Usage
The mysql_error() function returns the error description of the last MySQL operation.
This function returns an empty string ("") if no error occurs.
Syntax
mysql_error(connection)
Parameter Description
connection Optional. Specifies the MySQL connection. If not specified, the last connection opened by mysql_connect() or mysql_pconnect() is used.
________________________________________
Example
In this example we will try to log on to a MySQL server with the wrong username and password:
<?php
$con = mysql_connect("localhost","wrong_user","wrong_pwd");
if (!$con)
{
die(mysql_error());
}
mysql_close($con);
?>
The output of the code above could be:
Access denied for user 'wrong_user'@'localhost'
(using password: YES)
PHP mysql_fetch_array() Function
________________________________________
Complete PHP MySQL Reference
________________________________________
Definition and Usage
The mysql_fetch_array() function returns a row from a recordset as an associative array and/or a numeric array.
This function gets a row from the mysql_query() function and returns an array on success, or FALSE on failure or when there are no more rows.
Syntax
mysql_fetch_array(data,array_type)
Parameter Description
data Required. Specifies which data pointer to use. The data pointer is the result from the mysql_query() function
array_type Optional. Specifies what kind of array to return.
Possible values:
• MYSQL_ASSOC - Associative array
• MYSQL_NUM - Numeric array
• MYSQL_BOTH - Default. Both associative and numeric array
________________________________________
Tips and Notes
Note: After the data is retrieved, this function moves to the next row in the recordset. Each subsequent call to mysql_fetch_array() returns the next row in the recordset.
Tip: Field names returned by this function are case-sensitive.
________________________________________
Example
<?php
$con = mysql_connect("localhost", "peter", "abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db("test_db",$con);
$sql = "SELECT * from Person WHERE Lastname='Refsnes'";
$result = mysql_query($sql,$con);
print_r(mysql_fetch_array($result));
mysql_close($con);
?>
The output of the code above could be:
Array
(
[0] => Refsnes
[LastName] => Refsnes
[1] => Kai Jim
[FirstName] => Kai Jim
[2] => Taugata 2
[Address] => Taugata 2
[3] => 22
[Age] => 22
)
PHP mysql_fetch_row() Function
________________________________________
Complete PHP MySQL Reference
________________________________________
Definition and Usage
The mysql_fetch_row() function returns a row from a recordset as a numeric array.
This function gets a row from the mysql_query() function and returns an array on success, or FALSE on failure or when there are no more rows.
Syntax
mysql_fetch_row(data)
Parameter Description
data Required. Specifies which data pointer to use. The data pointer is the result from the mysql_query() function
________________________________________
Tips and Notes
Note: After the data is retrieved, this function moves to the next row in the recordset. Each subsequent call to mysql_fetch_assoc() returns the next row in the recordset.
________________________________________
Example
<?php
$con = mysql_connect("localhost", "peter", "abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db("test_db",$con);
$sql = "SELECT * from Person WHERE Lastname='Refsnes'";
$result = mysql_query($sql,$con);
print_r(mysql_fetch_row($result));
mysql_close($con);
?>
The output of the code above could be:
Array
(
[0] => Refsnes
[1] => Kai Jim
[2] => Taugata 2
[3] => 22
)
PHP mysql_query() Function
________________________________________
Complete PHP MySQL Reference
________________________________________
Definition and Usage
The mysql_query() function executes a query on a MySQL database.
This function returns the query handle for SELECT queries, TRUE/FALSE for other queries, or FALSE on failure.