Ce diaporama a bien été signalé.
Le téléchargement de votre SlideShare est en cours. ×

3 php-connect-to-my sql

Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Prochain SlideShare
Intro to PHP
Intro to PHP
Chargement dans…3
×

Consultez-les par la suite

1 sur 17 Publicité

Plus De Contenu Connexe

Similaire à 3 php-connect-to-my sql (20)

Publicité

Plus récents (20)

3 php-connect-to-my sql

  1. 1. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology National Diploma in Information and Communication Technology PHP :3-PHP-CONNECT-TO-MySQL> K72C001M07 - Web Programming Y. Achchuthan Department of Information & Communication Technology, Sri Lanka – German Training Institute 11/23/2018 3-PHP-CONNECT-TO-MySQL 1
  2. 2. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology What is MySQL? • MySQL is a database system used on the web • MySQL is a database system that runs on a server • MySQL is ideal for both small and large applications • MySQL is very fast, reliable, and easy to use • MySQL uses standard SQL • MySQL compiles on a number of platforms • MySQL is free to download and use • MySQL is developed, distributed, and supported by Oracle Corporation 11/23/2018 3-PHP-CONNECT-TO-MySQL 2
  3. 3. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology Connect to MySQL • Open a Connection to MySQL • Syantax $con = mysqli_connect(host,username,password,dbname,port,socket); Return Value: Returns an object representing the connection to the MySQL server 11/23/2018 3-PHP-CONNECT-TO-MySQL 3 Parameter Description host Optional. Specifies a host name or an IP address username Optional. Specifies the MySQL username password Optional. Specifies the MySQL password dbname Optional. Specifies the default database to be used port Optional. Specifies the port number to attempt to connect to the MySQL server socket Optional. Specifies the socket or named pipe to be used
  4. 4. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology Closing a Connection • Close the created database connection as follows mysql_close($con); 11/23/2018 3-PHP-CONNECT-TO-MySQL 4
  5. 5. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology Example - mysqli_connect() //config.php define('DB_HOST','localhost'); define('DB_USER','root'); define('DB_PASS',''); define('DB_NAME',''); $conn=mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME) ; if (mysqli_connect_errno()){ echo "Failed to connect to MySQL: " . mysqli_connect_error(); } echo "Connected successfully"; 11/23/2018 3-PHP-CONNECT-TO-MySQL 5
  6. 6. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology mysqli_query() Function • Perform queries against the database: • Syntax mysqli_query(connection,query,resultmode); 11/23/2018 3-PHP-CONNECT-TO-MySQL 6 Parameter Description connection Required. Specifies the MySQL connection to use query Required. Specifies the query string resultmode Optional. A constant. Either: •MYSQLI_USE_RESULT (Use this if we have to retrieve large amount of data) •MYSQLI_STORE_RESULT (This is default)
  7. 7. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology Create a MySQL Database $sql = "CREATE DATABASE NVQ5"; if (mysqli_query($conn, $sql)) { echo "Database created successfully"; } else { echo "Error creating database: " . mysqli_error($conn); } 11/23/2018 3-PHP-CONNECT-TO-MySQL 7
  8. 8. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology Create MySQL Tables $sql = "CREATE TABLE MyGuests ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, firstname VARCHAR(30) NOT NULL, lastname VARCHAR(30) NOT NULL, email VARCHAR(50), reg_date TIMESTAMP )"; if (mysqli_query($conn, $sql)) { echo "Table MyGuests created successfully"; } else { echo "Error creating table: " . mysqli_error($conn); } 11/23/2018 3-PHP-CONNECT-TO-MySQL 8
  9. 9. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology Notes on the table above • NOT NULL - Each row must contain a value for that column, null values are not allowed • DEFAULT value - Set a default value that is added when no other value is passed • UNSIGNED - Used for number types, limits the stored data to positive numbers and zero • AUTO INCREMENT - MySQL automatically increases the value of the field by 1 each time a new record is added • PRIMARY KEY - Used to uniquely identify the rows in a table. The column with PRIMARY KEY setting is often an ID number, and is often used with AUTO_INCREMENT 11/23/2018 3-PHP-CONNECT-TO-MySQL 9
  10. 10. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology Insert Data Into MySQL After a database and a table have been created, we can start adding data in them. • Here are some syntax rules to follow: • The SQL query must be quoted in PHP • String values inside the SQL query must be quoted • Numeric values must not be quoted • The word NULL must not be quoted Note: I f a column is AUTO_INCREMENT (like the "id" column) or TIMESTAMP (like the "reg_date" column), it is no need to be specified in the SQL query; MySQL will automatically add the value. 11/23/2018 3-PHP-CONNECT-TO-MySQL 10
  11. 11. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology Insert Data Into MySQL $sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', 'john@example.com')"; if (mysqli_query($conn, $sql)) { echo "New record created successfully"; } else { echo "Error: " . $sql . "<br>" . mysqli_error($conn); } 11/23/2018 3-PHP-CONNECT-TO-MySQL 11
  12. 12. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology Insert Multiple Records Into MySQL $sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', 'john@example.com');"; $sql .= "INSERT INTO MyGuests (firstname, lastname,email) VALUES ('Mary', 'Moe', 'mary@example.com');"; $sql .= "INSERT INTO MyGuests (firstname, lastname,email) VALUES ('Julie', 'Dooley', 'julie@example.com')"; if (mysqli_multi_query($conn, $sql)) { echo "New records created successfully"; } else { echo "Error: " . $sql . "<br>" . mysqli_error($conn); } 11/23/2018 3-PHP-CONNECT-TO-MySQL 12
  13. 13. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology Select Data From a MySQL $sql = "SELECT * FROM MyGuests"; //$sql = "SELECT id, firstname, lastname FROM MyGuests"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { // output data of each row while($row = mysqli_fetch_assoc($result)) { //var_dump($row); echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>"; } } else { echo "0 results"; } 11/23/2018 3-PHP-CONNECT-TO-MySQL 13
  14. 14. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology Delete Data From a MySQL $sql = "DELETE FROM MyGuests WHERE id=3"; if (mysqli_query($conn, $sql)) { echo "Record deleted successfully"; } else { echo "Error deleting record: " . mysqli_error($conn); } 11/23/2018 3-PHP-CONNECT-TO-MySQL 14
  15. 15. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology Update Data In a MySQL $sql = "UPDATE MyGuests SET lastname='Mugund' WHERE id=9"; if (mysqli_query($conn, $sql)) { echo "Record updated successfully"; } else { echo "Error updating record: " . mysqli_error($conn); } 11/23/2018 3-PHP-CONNECT-TO-MySQL 15
  16. 16. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology Limit Data Selections From a MySQL $sql = "SELECT * FROM MyGuests ORDER BY id ASC LIMIT 10 OFFSET 15"; //$sql = "SELECT * FROM MyGuests ORDER BY id ASC LIMIT 10 OFFSET 10"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { while($row = mysqli_fetch_assoc($result)) { echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>"; } } else { echo "0 results"; } Note: The SQL query above says "return only 10 records, start on record 16 (OFFSET 15)": 11/23/2018 3-PHP-CONNECT-TO-MySQL 16
  17. 17. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology Reference www.w3schools.com www.php.net www.slgti.com Friday, November 23, 2018 17

×