0% found this document useful (0 votes)
30 views

PHP and Mysql

MySQL is a popular open-source database that is used by many large websites. The document discusses how to connect to a MySQL database from PHP, perform queries, retrieve results, and handle errors. It also provides examples of using configuration files to store database credentials.

Uploaded by

Mary Enitek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

PHP and Mysql

MySQL is a popular open-source database that is used by many large websites. The document discusses how to connect to a MySQL database from PHP, perform queries, retrieve results, and handle errors. It also provides examples of using configuration files to store database credentials.

Uploaded by

Mary Enitek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

MYSQL

Prepared By Fadi Sharif


MYSQL
 Very popular SQL-database
 Was developed by Swedish company MySQL AB

 In 2008 Sun Microsystems bought MySQL AB


 Both GNU GPL and Commercial Licence
 Now it’s owned by Oracle
 Has different UIs: CLI, WEB and GUI

 MySQL is used by Google, Wikipedia and Yahoo.


MYSQL TUTORIAL
 MySQL Tutorial can be found:
 http://dev.mysql.com/doc/refman/5.0/en/tutorial.html
PHP AND MYSQL
PHP AND MYSQL
 PHP has functions that allow access to MySQL
Databases
 Access is very easy
 1. Connect
 2. Select Database
 3. Query
 4. Close connection
1. CONNECT
$link = mysql_connect('mysql_server',
'mysql_user','mysql_password');
2. SELECT DATABASE
$link = mysql_connect('mysql_server',
'mysql_user',
'mysql_password');

$db_selected = mysql_select_db('foo',
$link);
3. QUERY
$link = mysql_connect('mysql_server',
'mysql_user',
'mysql_password');

$db_selected = mysql_select_db('foo',
$link);

$result = mysql_query("DELETE FROM table;", $link);


4. CLOSE
$link = mysql_connect('mysql_server',
'mysql_user',
'mysql_password');

$db_selected = mysql_select_db('foo',
$link);

$result = mysql_query("DELETE FROM table;", $link);

mysql_close($link);
MYSQL_QUERY
 INSERT, UPDATE, DELETE, DROP
 Returns true or false
 SELECT, SHOW, DESCRIBE, EXPLAIN
 Returns resource on success, false on error
 The returned resource should be passed to
mysql_fetch_array()
RETRIEVING TABLE
$link = mysql_connect('mysql_server',
'mysql_user',
'mysql_password');
$db_selected = mysql_select_db('foo',
$link);
$result = mysql_query("SELECT * FROM table;", $link);

while ($row = mysql_fetch_array($result))


{
echo($row[0]);
echo($row[1]);
}

mysql_close($link);
RETRIEVING TABLE
$link = mysql_connect('mysql_server',
'mysql_user',
'mysql_password');
$db_selected = mysql_select_db('foo',
$link);
$result = mysql_query("SELECT * FROM table;", $link);

while ($row = mysql_fetch_array($result))


{
echo($row["id"]);
echo($row["name"]);
}

mysql_close($link);
RETRIEVING TABLE
$link = mysql_connect('mysql_server',
'mysql_user',
'mysql_password');
$db_selected = mysql_select_db('foo', Default Value
$link);
$result = mysql_query("SELECT * FROM table;", $link);

while ($row = mysql_fetch_array($result, MYSQL_BOTH))


{
echo($row[0]);
echo($row["name"]);
}

mysql_close($link);
RETRIEVING TABLE
$link = mysql_connect('mysql_server',
'mysql_user',
'mysql_password');
$db_selected = mysql_select_db('foo',
$link);
$result = mysql_query("SELECT * FROM table;", $link);

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


{
echo($row[0]); // Only number indices
echo($row[1]);
}

mysql_close($link);
RETRIEVING TABLE
$link = mysql_connect('mysql_server',
'mysql_user',
'mysql_password');
$db_selected = mysql_select_db('foo',
$link);
$result = mysql_query("SELECT * FROM table;", $link);

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


{
echo($row["id"]); // Only associative indices
echo($row["name"]);
}

mysql_close($link);
ERROR HANDLING
 Every function in the previous code examples could fail
 Connection can fail, sql query can fail etc.
 Usually you exit the script when DB fails.
 With exit($status) – function, you can stop the execution
of the script.
EXAMPLE OF ERROR HANDLING 1
$link = mysql_connect('mysql_server',
'mysql_user',
'mysql_password');
if( ! $link )
{
exit("Error connecting to database");
}
EXAMPLE OF ERROR HANDLING 2
$link = mysql_connect('mysql_server',
'mysql_user',
'mysql_password')
or exit("Error connecting to database");
DESIGNING WEB APPLICATION
WITH DB CONNECTION
CONFIGURATION FILES FOR DB
 Create special configuration file for Database
configuration.
 This file should have constants just for the database
connection
 You could name it conf/database.php
EXAMPLE OF CONF/DATABASE.PHP
<?php
/**
* database.php - holds necessary constants for database connection
*
* Copyright information
*
*
*
* License
*
* Here should be the license...
*
*/

define("MYSQL_HOST", ”localhost");
define("MYSQL_USER", ”root");
define("MYSQL_PASSWD", ”root");
define("MYSQL_DB", ”test");

// End of file
?>

You might also like