php in 1 day: php programming for Beginners by LR ayoub
Author:LR, ayoub [LR, ayoub]
Language: eng
Format: epub
Publisher: UNKNOWN
Published: 2020-12-03T16:00:00+00:00
Click âCreateâ to create the database. Youâll be directed to the âpawszoneâ database. You can verify that by checking the gray bar at the top.
Next, click on âPrivilegesâ and youâll be directed to the page for creating a new user. At the bottom of the page, youâll see a link that says âAdd user accountâ. Click on it and enter the information shown in the screenshot below (leave other fields unchanged). Enter âABCDâ (without quotes) into the two password fields.
Scroll to the bottom of the page and clickâGoâ to create the user.
Once you are done, youâll see a message that saysâYou have added a newuserâ. You are now ready to connect to the database you have just created.
Create a new file in Brackets and save it as sql_cud.php . Add the following code to it (you can download the code at https://learncodingfast.com/php ).
<?php
//SECTION A - CONNECT TO DATABASE
$pdo = new PDO("mysql:host=localhost;dbname=pawszone", "pz_admin",
"ABCD");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //SECTION B - CREATE TABLE
$sql = "CREATE TABLE IF NOT EXISTS pets ( owner VARCHAR(255) NOT NULL, petname VARCHAR(255) NOT NULL , breed VARCHAR(255) NOT NULL, microchip VARCHAR(20), PRIMARY KEY(owner, petname))";
$stmt = $pdo->prepare($sql);
$stmt->execute();
//SECTION C - INSERT DATA
$sql = "INSERT INTO pets (owner, petname, breed)
VALUES (:owner, :petname, :breed)";
$stmt = $pdo->prepare($sql);
$owner = array('Ted', 'Jamie', 'En', 'En');
$pname = array('Angel', 'Max', 'Boots', 'Dora');
$breed = array('Labradoodle', 'Domestic Shorthair', 'Domestic Shorthair', 'Munchkin');
for ($i = 0; $i < 4; ++$i)
{
$stmt->bindValue(':owner', $owner[$i]);
$stmt->bindValue(':petname', $pname[$i]);
$stmt->bindValue(':breed', $breed[$i]);
$stmt->execute();
}
//SECTION D - UPDATE DATA
$sql = "UPDATE pets SET microchip = :micro WHERE owner = :owner AND petname = :petname";
$stmt = $pdo->prepare($sql);
$stmt->bindValue(':micro', '121342345');
$stmt->bindValue(':owner', 'Jamie');
$stmt->bindValue(':petname', 'Max');
$stmt->execute();
//SECTION E - DELETE DATA
$sql = "DELETE FROM pets WHERE owner = :owner AND petname = :petname";
$stmt = $pdo->prepare($sql);
$stmt->bindValue(':owner', 'Ted');
$stmt->bindValue(':petname', 'Angel');
$stmt->execute();
In the code above, we first connect to the âpawszoneâ database in Section A.
Next, in Section B, we use a SQL CREATE statement to create a table called âpetsâ in our database. As this SQL statement does not have any placeholder, we do not need to call the bindValue() method. Instead, we simply prepare the statement and execute it.
In Section C, we have an INSERT statement for inserting data into the âpetsâ table. We first use the prepare() method to prepare this statement. Although weâll be executing the INSERT statement four times using a for loop later, note that we only need to prepare the statement once. This is another advantage of using prepared statements as it can result in faster execution, especially if the query is complex or needs to be run many times.
After preparing the statement, we declare three arrays $owner , $pname and $breed - and assign the data to be inserted to these arrays. Next, we use a for loop to loop through the arrays.
Each time the loop runs, we bind one value in each array to the relevant placeholder. After binding the data, we call the execute() method to execute the INSERT statement.
After inserting the data in Section C, we update a row (where owner = âJamieâ and petname = âMaxâ) in Sections D and delete another (where owner = âTedâ and petname = âAngelâ) in Section E.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Hello! Python by Anthony Briggs(9911)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9794)
The Mikado Method by Ola Ellnestam Daniel Brolund(9775)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8292)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7775)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7758)
Grails in Action by Glen Smith Peter Ledbrook(7693)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Windows APT Warfare by Sheng-Hao Ma(6778)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(6505)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6409)
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition by Marcos Romero & Brenden Sewell(6372)
Kotlin in Action by Dmitry Jemerov(5058)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4313)
Functional Programming in JavaScript by Mantyla Dan(4037)
Solidity Programming Essentials by Ritesh Modi(3975)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3758)
Unity 3D Game Development by Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel(3702)
The Ultimate iOS Interview Playbook by Avi Tsadok(3676)
