php in 1 day: php programming for Beginners by LR ayoub

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



Copyright Disclaimer:
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.