Вы находитесь на странице: 1из 2

WEB- ADVANCED PROGRAMMING

CONNECTING TO A MYSQL DATABASE


Before you can access and work with data in a database, you must create a
connection to the database.

SYNTAX:
mysql_connect(servername,username,password);
Parameter Description
servername Optional. Specifies the server to connect to.
username Optional. Specifies the username to log in with. Default value is the
name of the user that owns the server process.
password Optional. Specifies the password to log in with. Default is

MYSQL FUNCTION
Database Function What it does
mysql_connect() Opens a connection to a MYSQL server.
mysql_selectdb() Select the default database
mysql_query() Send a query or command to a MYSQL connection

QUERY FUNCTIONS AFFECTING ROWS


mysql_fetch_assoc() Returns one result row, as an associative array.
mysql_fetch_row() Returns one result row, as an array
mysql_affected_rows() Returns number of rows affected by query
mysql_num_rows() Returns number of rows selected
QUERY FUNCTIONS AFFECTING COLUMNS
mysql_fetch_field() Gets column information from a result and returns as an
object
mysql_field_name() Gets the name of the specified field in a result
mysql_list_fields() Sets result pointer to a specified field offset.
mysql_num_fields() Gets number of fields in a result.
mysql_field_seek() Sets result pointer to a specified field offset
mysql_field_type() Gets the type of the specifies field in a result
mysql_field_len() Returns the length of the specifies field
mysql_field_table() Gets name of the table the specified field
mysql_tablename() Gets table name of field

FUNCTIONS FOR ERROR HANDLING


mysql_error() Returns the numerical value of the error message from
previous MYSQL operation.
mysql_error() Returns the text of the error message from previous
MYSQL operation.
<?php
$con = mysql_connect("localhost", "root", "mypass") or
die("Could not connect: " . mysql_error());
mysql_select_db("tutorials");
$result = mysql_query("select * from tutorials");
echo "<table border='1' style='border-collapse: collapse'>";
echo "<th>Name of the
Tutorial</th><th>Pages</th><th>Examples</th><th>Author</th>";
while ($row = mysql_fetch_assoc($result))
{
echo"<tr><td>".$row['name']."</td><td>".$row['no_of_pages']."</td><td>".
$row['no_of_examples']."</td><td>".$row['author']."</td></tr>";
}
echo "</table>";
mysql_close($con);
?>

Вам также может понравиться