B.SC - SE SY PHP - Unit-V
B.SC - SE SY PHP - Unit-V
The first three arguments sent to the function (hostname, username, and
password) are based upon the users of MySQL database.
Example
<?php
$con = mysqli_connect("localhost","root","",”my_db”);
if (!$con)
{
Print 'Could not connect: ' . mysqli_error();
}
Else
{
Print 'Connect ‘;
}
// some code
?>
Closing a Connection
The connection will be closed automatically when the script ends. To close the
connection before, use the mysqli_close() function:
Selecting Database:
PHP uses mysqli_select_db function to select the database on which queries are
to be performed. This function takes two parameters and returns TRUE on
success or FALSE on failure.
Ex. $result=mysqli_query($conn,$sql);
The mysqli_query() function takes the database connection as its argument and
the query itself.
For simple queries like INSERT, UPDATE, DELETE, etc. (which do not return
records), the
$r variable short for result will be either TRUE or FALSE, depending upon
whether the query executed successfully.
Example
After a database and a table have been created, we can start adding data in
them.
The INSERT INTO statement is used to add new records to a MySQL table:
Syntax:
INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)
Example
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
mysqli_close($conn);
?>
Multiple SQL statements must be executed with the mysqli_multi_query()
function.
Prepared by: Mr. G.P.Shinde , COCSIT Latur Page 4
Notes: PHP, Class: B.Sc. SE. SY, Unit V: Using PHP with MySQL
Syntax
Then, the function num_rows() checks if there are more than zero rows
returned.
If there are more than zero rows returned, the function fetch_assoc() puts all
the results into an associative array that we can loop through.
The while() loop loops through the result set and outputs the data from the no
of column name.
Example
<?php
// Create connection
$conn = mysqli_connect(“localhost”,”root”,”” ,”myDB”);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"].
"<br>";
}
} else { echo "0 results";}
mysqli_close($conn);?>
Prepared by: Mr. G.P.Shinde , COCSIT Latur Page 5
Notes: PHP, Class: B.Sc. SE. SY, Unit V: Using PHP with MySQL
You can accomplish the first objective by securing the MySQL connection script
outside of the Web directory so that it is never viewable through a Web
browser.
The second objective is achieved by not letting the user see PHP’s error
messages or your queries (in these scripts, that information is printed out for
your debugging purposes; you’d never want to do that on a live site).
For the third objective, there are numerous steps you can and should take, all
based upon the premise of never trusting user supplied data.
First, validate that some value has been submitted, or that it is of the proper
type (number, string, etc.).
Second, use regular expressions to make sure that submitted data matches what
you would expect.
Third, you can typecast some values to guarantee that they’re numbers.
Syntax:
mysqli_num_rows( result );
Example:
// Display result
printf("Total rows in this table : %d\n", $rowcount);
}
Output:
We count the table rows using MySQL count () function. It’s an aggregate
function used to count rows.
Syntax:
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
Example:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if (mysqli_query($conn, $sql)) {
echo "Record updated successfully";
}
else {
echo "Error updating record: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
The End
Prepared by: Mr. G.P.Shinde , COCSIT Latur Page 8