logo

Database Assignment: PHP and My SQL

   

Added on  2020-06-04

14 Pages4437 Words116 Views
REFLECTION
Database Assignment: PHP and My SQL_1
Table of ContentsPHP and My SQL Exercise – 1.......................................................................................................1PHP and My SQL Exercise – 2.......................................................................................................3PHP and My SQL Exercise – 3.......................................................................................................4PHP and My SQL Exercise – 4.......................................................................................................5JavaScript and Ajax Exercise -1......................................................................................................7JavaScript and Ajax Exercise -2......................................................................................................8JavaScript and Ajax Exercise -3....................................................................................................10
Database Assignment: PHP and My SQL_2
PHP and My SQL Exercise – 1After login in the database server X10host My SQL, I entered my user id along with thepassword for signing in because I knew without logging in, I would not been able to perform theSQL commands. Without operating the SQL commands, I would not be able to see the outputs.So, for that I logged in. So to connect to the database server from a PHP script, there were fewthings that I had to remember while login in the X10host My SQL. My username for thedatabase server is same as that of the X10host My SQL, so I always try to remember it. Thesame is with the password as well. So, I have to remember my user id as well as domain forfurther login as well. My user name is elboudab and my domain is elboudab.x10host.com. As forevery time, it was difficult for me to insert the same information, so I made a function in the fileso that every time, when I have to use that information, I can include the function along with thespecific code. Then, on the basis of it, I made a file named dbutils.php and placed in the libraryfolder. The code is given as under :<?phpfunctionconnect(){$username='elboudab';$password='********';$mysqlhost='localhost';$dbname='';$pdo=newPDO('mysql:host='.$mysqlhost.';dbname='.$dbname.';charset=utf8',$username,$password);if($pdo){//makeerrorsthrowexceptions$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);return$pdo;}else{die("CouldnotcreatePDOconnection.");}}?>Along with it, I made another file dbtest.php for checking whether there are any errors in thefollowing, so that after running the code, I can be able to know about the errors. The code for theimplementation of this file is given as under :<?phprequire_once('lib/dbutils.php');1
Database Assignment: PHP and My SQL_3
?><!DOCTYPEhtml><html><head><title>DBTest</title></head><body><?php$pdo=connect();if($pdo){print"<h1>Successfullyconnected!</h1>\n";}else{print"<h1>Didnotconnect.</h1>\n";}?></body></html>In this, I used connect () function so that at the time of interpretation, it can be easilydone by calling connect () function, so I used it as well. Also, this function is used for returningthe object, so at the time of returning of the object, we can call connect () function. After that Iadded some functions in the file, dbutils.php. These functions are tableExists ($pdo, $table),createDemoTable ($pdo) and dumpTable($pdo, $table). So, I added all these functions in thedbutils.php and the code was as under :functiontableExists($pdo,$table){$sql="SHOWTABLESLIKE'$table'";return($pdo->query($sql)->rowCount()>0);}functioncreateDemoTable($pdo){if(!tableExists($pdo,'demotable')){$sql="CREATETABLEIFNOTEXISTS`demotable`(`idnumber`int(11)NOTNULLauto_increment,`firstname`varchar(255)NOTNULL,`lastname`varchar(255)NOTNULL,`age`int(11)NOTNULL,PRIMARYKEY(`idnumber`))";$pdo->exec($sql);$sql="INSERTINTO`demotable`(`firstname`,`lastname`,`age`)VALUES('John','Smith',23),('Sue','Davis',13),('Peter','Young',45),('Alice','Brown',56),('Frank','Gray',22),('Betty','Redwood',27),2
Database Assignment: PHP and My SQL_4

End of preview

Want to access all the pages? Upload your documents or become a member.

Related Documents
Photographs and Accepts Uploaded Photographs
|5
|2266
|500

It’s a php CodeIgniter and the first part completed.
|2
|448
|466

Dynamic Web Programming: Creating a RESTful JSON back-end API using PHP
|6
|547
|114

Code Functionality and Description
|5
|688
|46

Comparison of Apache and Nodejs with Nginx Configuration
|21
|1497
|479

Digital Business Application – Lab Exercises
|5
|2014
|387