0% found this document useful (0 votes)
59 views3 pages

Ans SQL Lab 4 (PHP + Mysql) : Answer/Help

The document provides code snippets for connecting to a MySQL database called "misc" from PHP using PDO. It creates the "users" table with columns for user ID, name, email, and password. Code is given to insert sample users, select data from the users table and display it on a web page in a table. The snippets connect to the database, prepare and execute queries to demonstrate basic CRUD operations in PHP with MySQL.

Uploaded by

Atharva Mishra
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)
59 views3 pages

Ans SQL Lab 4 (PHP + Mysql) : Answer/Help

The document provides code snippets for connecting to a MySQL database called "misc" from PHP using PDO. It creates the "users" table with columns for user ID, name, email, and password. Code is given to insert sample users, select data from the users table and display it on a web page in a table. The snippets connect to the database, prepare and execute queries to demonstrate basic CRUD operations in PHP with MySQL.

Uploaded by

Atharva Mishra
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/ 3

Ans SQL Lab 4 ( php + mysql)

Answer/Help

1. <h1>Hello World !</h1>

2.<h1>Hello from IIT(ISM)</h1>


<p>
<?php
echo "Hi.\n";
$answer = 6 * 7;
echo "The answer is $answer, what was the question again?\n";
?>
</p>
<p>Yes another paragraph.</p>

3.

C:\xampp\mysql\bin\mysql -u root

CREATE DATABASE misc;

GRANT ALL ON misc.* TO 'fred'@'localhost' IDENTIFIED BY


'zap';

USE misc; (if you are in the command line)

CREATE TABLE users (


user_id INTEGER NOT NULL
AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(128),
email VARCHAR(128),
password VARCHAR(128),
INDEX(email)
) ENGINE=InnoDB CHARSET=utf8;

describe users;
INSERT INTO users (name,email,password) VALUES (‘Sachin’,'[email protected]','123');
INSERT INTO users (name,email,password) VALUES (‘Saurav’,'[email protected]','456’');

ques5.php
<?php
echo "<pre>\n";
$pdo = new PDO('mysql:host=localhost;port=3306;dbname=misc',
'fred', 'zap');
$stmt = $pdo->query("SELECT * FROM users");
while ( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) {
print_r($row);
}
echo "</pre>\n";
?>

6. Ques 6.php
<?php
$pdo = new PDO('mysql:host=localhost;port=3306;dbname=misc',
'fred', 'zap');
$stmt = $pdo->query("SELECT name, email, password FROM users");
echo '<table border="1">'."\n";
while ( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) {
echo "<tr><td>";
echo($row['name']);
echo("</td><td>");
echo($row['email']);
echo("</td><td>");
echo($row['password']);
echo("</td></tr>\n");
}
echo "</table>\n";
?>

7.pdo.php
<?php
$pdo = new PDO('mysql:host=localhost;port=3306;dbname=misc',
'fred', 'zap');
// See the "errors" folder for details...
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Ques7.php
<?php
echo "<pre>\n";
require_once "pdo.php";
$stmt = $pdo->query("SELECT * FROM users");
while ( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) {
print_r($row);
}
echo "</pre>\n";
?>

You might also like