12
Lecture – 3 PHP Mysql Himani Manglani [email protected]

Lecture – 3 PHP Mysqlvip.gatech.edu/wiki/images/7/73/PHP-MysqlIntro_part1.pdf · MySQL is a database. ! The data in MySQL is stored in database objects called tables. ! A table

  • Upload
    others

  • View
    14

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lecture – 3 PHP Mysqlvip.gatech.edu/wiki/images/7/73/PHP-MysqlIntro_part1.pdf · MySQL is a database. ! The data in MySQL is stored in database objects called tables. ! A table

Lecture – 3

PHP Mysql

Himani Manglani [email protected]

Page 2: Lecture – 3 PHP Mysqlvip.gatech.edu/wiki/images/7/73/PHP-MysqlIntro_part1.pdf · MySQL is a database. ! The data in MySQL is stored in database objects called tables. ! A table

!  A database is a structure that comes in two flavors: a flat database and a relational database

!  In a relational structured database there are tables that store data.

!  The columns define which kinds of information will be stored in the table

!  A row contains the actual values for these specified columns. Each row will have 1 value for each and every column

!  For example a table with columns (Name, Age, Weight-lbs) could have a row with the values (Bob, 65, 165).

Page 3: Lecture – 3 PHP Mysqlvip.gatech.edu/wiki/images/7/73/PHP-MysqlIntro_part1.pdf · MySQL is a database. ! The data in MySQL is stored in database objects called tables. ! A table

!  MySQL is a database.

!  The data in MySQL is stored in database objects called tables.

!  A table is a collections of related data entries and it consists of columns and rows.

!  Databases are useful when storing information categorically. A company may have a database with the following tables: "Employees", "Products", "Customers" and "Orders".

Page 4: Lecture – 3 PHP Mysqlvip.gatech.edu/wiki/images/7/73/PHP-MysqlIntro_part1.pdf · MySQL is a database. ! The data in MySQL is stored in database objects called tables. ! A table

!  A database most often contains one or more tables. Each table is identified by a name (e.g. "Customers" or "Orders"). Tables contain records (rows) with data.Below is an example of!a table called "Persons":

!  Queries : A query is a question or a request.With MySQL, we can query a database for specific information and have a recordset returned.

!  Some of the basic functions of SQL are !  Inputting, !  modifying, and !  dropping data from databases.

Page 5: Lecture – 3 PHP Mysqlvip.gatech.edu/wiki/images/7/73/PHP-MysqlIntro_part1.pdf · MySQL is a database. ! The data in MySQL is stored in database objects called tables. ! A table

!  Very easy to setup and use

!  But still quite powerful

!  Robust and fast: It can easily outperform e.g. Oracle in simple tasks

!  Open Source, but also commercially backed Cross-platform

Page 6: Lecture – 3 PHP Mysqlvip.gatech.edu/wiki/images/7/73/PHP-MysqlIntro_part1.pdf · MySQL is a database. ! The data in MySQL is stored in database objects called tables. ! A table

!  create database [databasename];

!  show databases;

!  use [db name];

!  show tables;

!  describe [table name];

!  drop database [database name];

!  drop table [table name];

!  SELECT * FROM [table name];

Page 7: Lecture – 3 PHP Mysqlvip.gatech.edu/wiki/images/7/73/PHP-MysqlIntro_part1.pdf · MySQL is a database. ! The data in MySQL is stored in database objects called tables. ! A table

!  <?php

$database = <database name>;

$username = <User Name>;

$password = <password>;

$hostname = <hostname>;

$link = mysql_connect($hostname, $username, $password);

if (!$link)

{ die('Not connected : ' . mysql_error());}

// make foo the current db$db_selected = mysql_select_db($database, $link);

if (!$db_selected)

{ die ('Can\'t connect : ' . mysql_error());}

// if you've got this far, the database connection has been made

Page 8: Lecture – 3 PHP Mysqlvip.gatech.edu/wiki/images/7/73/PHP-MysqlIntro_part1.pdf · MySQL is a database. ! The data in MySQL is stored in database objects called tables. ! A table

echo "<p>You have successfully connected to $database</p>";

// quick script to display all the tables in your database// run the mysql query and check the result$result = mysql_query('show tables');

if (!$result) { die('Invalid query: ' . mysql_error());} // loop through the result and display

echo "<p>Tables within in $database</p>";

if(is_array(mysql_fetch_array($result))) {while ($row = mysql_fetch_array($result)) { echo $row[0] . '<br>';}}

else {echo "<p>No tables found</p>";}

?>

Page 9: Lecture – 3 PHP Mysqlvip.gatech.edu/wiki/images/7/73/PHP-MysqlIntro_part1.pdf · MySQL is a database. ! The data in MySQL is stored in database objects called tables. ! A table

!  Check PHP Setup for MySQL support

!  <?!phpinfo()!?>

Page 10: Lecture – 3 PHP Mysqlvip.gatech.edu/wiki/images/7/73/PHP-MysqlIntro_part1.pdf · MySQL is a database. ! The data in MySQL is stored in database objects called tables. ! A table

We can check the mysql on our system using mysql command or

prompt:~> mysqlshow

+-----------+

| Databases |

+-----------+

| mysql |

| test |

+-----------+

Check MySql using PHP script

<?!echo!mysql_stat()!?>

Page 11: Lecture – 3 PHP Mysqlvip.gatech.edu/wiki/images/7/73/PHP-MysqlIntro_part1.pdf · MySQL is a database. ! The data in MySQL is stored in database objects called tables. ! A table

Connect MySQL in PHP script

<?$conn!=!mysql_connect('localhost');

echo!$conn;?>

We can also use

<?

mysql_connect('db.domain.com:33306','rasmus','foobar');

mysql_connect('localhost:/tmp/mysql.sock');

mysql_connect('localhost','rasmus','foobar',!!!!!!!!!!!!!!true,MYSQL_CLIENT_SSL|MYSQL_CLIENT_COMPRESS);?>

Page 12: Lecture – 3 PHP Mysqlvip.gatech.edu/wiki/images/7/73/PHP-MysqlIntro_part1.pdf · MySQL is a database. ! The data in MySQL is stored in database objects called tables. ! A table

!  PHP configuration configures no of max Connection and max Clients