lecture – 3 php mysqlvip.gatech.edu/wiki/images/7/73/php-mysqlintro_part1.pdf · mysql is a...
TRANSCRIPT
! 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).
! 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".
! 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.
! 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
! 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];
! <?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
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>";}
?>
! Check PHP Setup for MySQL support
! <?!phpinfo()!?>
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()!?>
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);?>
! PHP configuration configures no of max Connection and max Clients