0% found this document useful (0 votes)
16 views29 pages

PHP Database Connectivity 4

The document provides a comprehensive guide on PHP database connectivity using XAMPP, detailing the steps to set up a local server, connect to a MySQL database, and execute various SQL queries such as INSERT, SELECT, DELETE, and UPDATE. It explains the use of functions like mysqli_connect and mysqli_query for establishing connections and executing queries, along with examples of PHP code for database operations. Additionally, it includes links to video tutorials for further learning on PHP database connectivity.

Uploaded by

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

PHP Database Connectivity 4

The document provides a comprehensive guide on PHP database connectivity using XAMPP, detailing the steps to set up a local server, connect to a MySQL database, and execute various SQL queries such as INSERT, SELECT, DELETE, and UPDATE. It explains the use of functions like mysqli_connect and mysqli_query for establishing connections and executing queries, along with examples of PHP code for database operations. Additionally, it includes links to video tutorials for further learning on PHP database connectivity.

Uploaded by

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

PHP DATABASE

CONNECTIVITY
PHP Database connection
• The collection of related data is called a database. XAMPP stands for
cross-platform, Apache, MySQL, PHP, and Perl. It is among the simple
light-weight local servers for website development.
• Requirements: XAMPP web server procedure:
• Start XAMPP server by starting Apache and MySQL.
• Write PHP script for connecting to XAMPP.
• Run it in the local browser.
• Database is successfully created which is based on the PHP code.
• In PHP, we can connect to the database using XAMPP web server by
using the following path.
• To set up the PHP development environment, you mainly need two
software, a code editor and a web server package.
• You will be using:
• Visual Studio Code as the code editor.
• And XAMPP to set up your local web server.
Open PHPMYADMIN
• “localhost:8090/phpmyadmin”
mysqli_connect
• This method is used to connect a PHP application to the web server by establishing a secure
connection between them. Another method called mysql_connect also does the same task,
but mysqli_connect is more secure and the “i” here represents an improved version.
• The syntax to establish a new connection using the mysqli_connect method:
mysqli_connect ( "host","username","password","database_name" )
• This method accepts 4 parameters:
• host: This is an optional parameter. If you are on a local server, then pass NULL or the keyword
“localhost” to specify that the connection has to be made with the local webserver.
• username: This parameter is used to specify the username of MySQL. This is also an optional
parameter. If you are on a local server, then the username will be “root”.
• password: This parameter is used to specify the password of the user in MySQL. You can skip this
parameter as it is an optional parameter.
• name_of_database: This parameter is the name of the database you want to establish the connection
with. It will perform all the queries on this specified database. This parameter is also optional.
mysqli_query
• This method is used to execute the query that is passed as a parameter to it. The
queries insert, select, update, and delete can be executed using the mysqli_query
method. If the queries are successfully executed, then this method will return an
object or a boolean value TRUE, depending upon the type of query executed.
• The syntax to execute a MySQL query using the mysqli_query method in PHP:
mysqli_query($connection, query, mode)
• This method accepts 3 parameters:
• connection: This is a mandatory parameter, and it represents the object of the server with
which the connection is established.
• query: This parameter is also mandatory, and it represents the SQL query string that needs
to be executed.
• mode: This is an optional parameter. It represents the mode of the result. It is a constant
and is used to store the return value of the method.
move_uploaded_file
• This method is used to move a specified file (that has already been uploaded)
to a new location. This method first validates the specified file, and if it is
valid, then moves it to the destination location. If there is already a file in the
destination location, then it is overwritten by the new file.
• The syntax to move a file to a specific location using the move_uploaded_file
method:
move_uploaded_file(file_name, destination_path)
• This method accepts 2 parameters:
• file_name: This parameter is a string that represents the name of the file to be moved
to a new location.
• destination_path: This is a string specifying the destination location where the file
needs to be moved.
• Open localhost:8090/phpmyadmin/connect.php
2nd record add in database “test”
New database connection:
<?php
$mysqli = new mysqli("localhost","my_user","my_password","my_db");

// Check connection
if ($mysqli -> connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
exit();
}
?>
DATABASE CONNECTION
<?php
$username="your_name";
$password="your_password";
$hostname="localhost";
//connection tothedatabase
$dbConnect= mysql_connect($hostname,$username,$password)
or die("Unable toconnecttoMySQL");
echo"ConnectedtoMySQL";
//selectaspecificdatabase
$dbSelect=mysql_select_db("dbName",$dbConnect)
ordie("CouldnotselectdbName")
2 ND
OPTION:

<?php
$username="your_name";
$password="your_password";
$hostname="localhost";
//connection tothedatabase
$dbConnect= mysqli_connect($hostname,$username,$password)
//anotherway ofcheckingiftheconnectionwassuccessful
if(!$dbConnect){
die("Connectionfailed:".mysqli_connect_error());
}
echo"Connectedsuccessfully";
//selectaspecificdatabase
mysqli_select_db($dbConnect,"dbName")
?
• INSERT INTO statement is used to insert new rows in a database
table. Let’s see the syntax how to insert into table, considering
database already exists.
• Syntax:
INSERT INTO TABLE_NAME
(column1, column2, column3, ... columnN)
VALUES (value1, value2, value3, ...valueN);
• The SQL SELECT statement is used to select the records from database
tables.
SELECT FIRSTNAME,LASTNAME
FROM TABLE-NAME;
DELETE query:
• The DELETE query is used to delete records from a database table.
It is generally used along with the “Select” statement to delete only
those records that satisfy a specific condition.
• DELETE FROM TABLENAME WHERE COLUMN NAME=VALUE
UPDATE QUERY:
The MySQL UPDATE query is used to update existing
records in a table in a MySQL database.
•It can be used to update one or more field at the same
time.
•It can be used to specify any condition using the WHERE
clause.
UPDATE query:
<?php
$link = mysqli_connect("localhost", "root", "", "Mydb");

if($link === false){


die("ERROR: Could not connect. "
. mysqli_connect_error());
}

$sql = "UPDATE data SET Age='28' WHERE id=201";


if(mysqli_query($link, $sql)){
echo "Record was updated successfully.";
} else {
echo "ERROR: Could not able to execute $sql. "
.
mysqli_error($link);
}
mysqli_close($link);
?>
Link for PHP CODE for database
connectivity:
• https://www.youtube.com/watch?v=2HVKizgcfjo

• https://www.youtube.com/watch?v=zcojC6PWNOQ

• https://www.youtube.com/watch?v=gUO56GK0O40

• http://codewithharry.com/videos/php-tutorials-in-hindi-40
• https://www.raghwendra.com/blog/how-to-connect-html-to-databas
e-with-mysql-using-php-example
/

You might also like