0% found this document useful (0 votes)
22 views6 pages

Chapter 3 Practical

Chapter 3 provides PHP code snippets for connecting to a MySQL database, selecting a database, executing queries, fetching data, and handling errors. It includes examples of inserting records, retrieving field metadata, and displaying results from a 'student' table in a 'college' database. The chapter emphasizes the use of MySQL functions for database operations and error handling.

Uploaded by

syogesh1102
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)
22 views6 pages

Chapter 3 Practical

Chapter 3 provides PHP code snippets for connecting to a MySQL database, selecting a database, executing queries, fetching data, and handling errors. It includes examples of inserting records, retrieving field metadata, and displaying results from a 'student' table in a 'college' database. The chapter emphasizes the use of MySQL functions for database operations and error handling.

Uploaded by

syogesh1102
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

Chapter 3:-

1) Connection file:-
<?php
$servername = "localhost";
$username = "root";
$password = ""; // <== Make sure this is correct
(probably blank for WAMP)

$conn = new mysqli($servername, $username,


$password);

if ($conn->connect_error) {
die("Connection failed: " . $conn-
>connect_error);
}
echo "Connected successfully to MySQL!";
?>

2) Select Database :-
<?php
$host = "localhost";
$user = "root";
$password = "";
$db = "college";

$con=mysql_connect($host,$user,$password,$db)
or die ("Unable to connect");

$selected = mysql_select_db("college",$con)
or die("Could not select college");

?>

3) Mysql Query:-
<?php
$host = "localhost";
$user = "root";
$password = "";
$db = "college";

$con=mysql_connect($host,$user,$password,$db)
or die ("Unable to connect");

$selected = mysql_select_db("college",$con)
or die("Could not select examples");
$result=mysql_query("insert into student values
('7','hello','hello123@[Link]','123456')");

if($result>0){
echo "Record Inserted Successfully";
}else{
echo "Please check entered data";
}
?>

4) Mysql fetch field :-


<?php
$con = mysql_connect("localhost","root","");
$selectdb = mysql_select_db("college",$con);
$result = mysql_query('select * from student');
/* get information about the column */
$i = 0;
while ($i < mysql_num_fields($result)) {
$meta = mysql_fetch_field($result, $i);
echo "<pre>
max_length: $meta->max_length
name: $meta->name
not_null: $meta->not_null
numeric: $meta->numeric
primary_key: $meta->primary_key
table: $meta->table
type: $meta->type
unique_key: $meta->unique_key
</pre>";
$i++; }

5) Mysql data seek:-


<?php
$con = mysql_connect("localhost","root","");
$selectdb = mysql_select_db("college",$con);
$result = mysql_query('select * from student');
/* get information about the column */
$i = 0;
while ($i < mysql_num_fields($result)) {
$meta = mysql_fetch_field($result, $i);
echo "<pre>
max_length: $meta->max_length
name: $meta->name
not_null: $meta->not_null
numeric: $meta->numeric
primary_key: $meta->primary_key
table: $meta->table
type: $meta->type
unique_key: $meta->unique_key
</pre>";
$i++; }
6) Mysq-error :-
<?php
$con = mysql_connect("localhost", "root", "");
if (!mysql_select_db("college", $con)) {
echo mysql_errno($con) . ": " . mysql_error($con).
"<br />"; }
?>

7) Mysql fetch array :-


<?php
$con = mysql_connect("localhost", "root", "") or
die("Could not connect: " . mysql_error());
mysql_select_db("college",$con);
$result = mysql_query("SELECT * FROM student");
while ($row = mysql_fetch_array($result))
{
echo "Id : ".$row['id']." |Name : " .
$row['name123'] ." |Email :".$row['email'].
"|Mobile No" . $row['mobile_no']."<br>";
}
?>
8) Mysql Affected rows :-
<?php
$con = mysql_connect("localhost", "root", "") or
die("Could not connect: " . mysql_error());
mysql_select_db("college",$con);
$result = mysql_query("insert into student
values('demo','demo12@[Link]','123456')");
echo "Inserted records:".mysql_affected_rows();
?>

9)

You might also like