0% found this document useful (0 votes)
44 views2 pages

AP Lab

This document connects to a MySQL database, defines a table, inserts data, and selects data. It includes PHP code to: 1) Connect to a MySQL database and select a database. 2) Define a table with name and age columns. 3) Insert a submitted name and age into the table. 4) Select and display all data from the table.

Uploaded by

MOHIT KUMAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views2 pages

AP Lab

This document connects to a MySQL database, defines a table, inserts data, and selects data. It includes PHP code to: 1) Connect to a MySQL database and select a database. 2) Define a table with name and age columns. 3) Insert a submitted name and age into the table. 4) Select and display all data from the table.

Uploaded by

MOHIT KUMAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

< ?

php

include_once oine .php'):


Sconnection = dbConnect ( ) ; ?>
nde.php
<html>
<head>
<title>PHP MySQL</title>
</head>>
<body>
p style="font-size: 1.5rem'"><b><i>User
<hr> Details</i></b></p>
<form action="index.php"
method="get" id="reg form ">
<fieldset>
<legend>Personal Data:</legend>
<label for="name">Name</label><br>
<input type=" text" id="name" name="name'"><br><br>
1 <label for="age">Age</label><br>
<input type="' text" id="age" name="age"><br><br>
<input type="submit" value="Submit">
<fieldset>
</form>
</body>
</html>

<?php
SsqlTest ="selec e r sOn MTT "
$tQuery = emysqli_query (Sconnection, $sqlTest) ;

i f(isset ($_GET[ 'nare ']) && isset ($_GET[ 'a ge' ])) {
Sname s_GET ( 'name'];
=

Sage S_GET['age']
i f ($tQuery) i
$sqlInsert = "INSERT INTO person VALUES( '$name', Sage ";
$iQuery =emysqli_query ($connection, $sqlInsert) or die("<p>Unabi e
eecute the query, <ip><p>Error ode "

mysqi _errno (Sconnection)


mysqli_error($connection) ), "</p>";
echo "<p>SuccessIully inserted data.</ ";
39 }else{
$sqlCreate =
"CREATE TABLE persor (name VARCHAR (25), aa
ScQuery = emysqli_query ($connection, $sqlcreate) or die("

EXeC-Ehe query.</p D>Eror code". mysqli errno ($connectic


mysqli_error($ connection) ) . "</p>"
echo "<p>Successfully created the table.</p>" ;
elsel
echo "p>rlase f - he abve iorn and c i c k suomit

SsqlSelect = "
Omprsin;";
SsQuery= emysqli_query ($connect ion, $sqlSelect) ;
i f(SsQuery)
echo " an! d '40
echo " : t r t h Nam
Srow = mysqli fetch row (SsQuery);

do

d$row
[ 0] } 111";
e c h o "

echo " d ($row [1]}td";


Srow mysqli_fetch_row ($sQuery):
while ($row)

dbClose($connection):
2
Connett php
<?php
define ("HOST", "localhost ");
define("DEUEER", "Io-t") ;
define("DBFAS3", "");
define("DATABASE", "1 ab5")
function dbConnect ())
$connect =emysqli_connect (HOST, DBUSER, DBPASS) or die("p>Unable o
connect to the database server . </p><p>Error code ". mysqli _connect_errno().
"".mysqliconnect_error ()). "</p";
10 echo "<p>Successfully connected to the database server. </p>" ;
11
12 SDBSelect emysqli_select_db ($connect, DATABASE) or die("pJnatie t
select the dat abase.</p><p>Error code ". mysqli_errno (Sconnect) . ": ".
mysgli_error (Sconnect) ) . "</p>":

14 echo "p>Successfully opened the database.</p>";

return Sconnect;

19 function dbClose ($connection) {


mysqli_close($connection)

You might also like