SoFunction
Updated on 2025-03-02

PHP Learning Notes 3 Basic Database Operations

The following is the process of logging in to mysql on Linux, creating databases and creating tables.

yin@yin-Ubuntu10:~$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 360
Server version: 5.1.41-3ubuntu12.1 (Ubuntu)

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database UseCase;
Query OK, 1 row affected (0.00 sec)

mysql> use UseCase;
Database changed

mysql> create table User(UserName varchar(20) primary key,Password varchar(20) not null,CreateTime timestamp default current_timestamp);
Query OK, 0 rows affected (0.01 sec) Let’s create a page to complete the creation of the new user’s page. First is a simple form:
Copy the codeThe code is as follows:

<form action="" method="post">
<dl>
<dt>UserName</dt><dd><input name="UserName" maxlength="20" type="text"/></dd>
<dt>Password</dt><dd><input name="Password" maxlength="20" type="password"/></dd>
<dt>Confirm Password</dt><dd><input name="ConfirmPassword" maxlength="20" type="password"/></dd>
</dl>
<input type="submit" name="ok" value="ok"/>
</form>

PHP obtains the data in the form submitted through the post method through the $_POST array. In PHP programs, we must first determine whether there is an OK field, so as to determine whether the page is accessed for the first time, or whether the user clicks OK and submits it, and then determine whether the password inputs are unified between the two times. Then you can get the username and password and insert it into the database. PHP connects to MySQL databases generally using mysql extension or mysqli extension. Mysqli extension is a bit newer, so we use this method here. MySQL may need to be installed and configured, but it is installed by default in my environment. Using mysqli to extend the operation database is generally divided into the following steps: constructing mysqli object, constructing statement, binding parameters, executing, and closing. The code is as follows:
Copy the codeThe code is as follows:

<?php
$match=true;
if(isset($_POST["ok"])) {
$pwd=$_POST["Password"];
$pwdConfirm=$_POST["ConfirmPassword"];
$match=($pwd==$pwdConfirm);
$conn=new mysqli("localhost","root","123","UseCase");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query="insert into User(UserName,Password) values(?,?)";
$stmt=$conn->stmt_init();
$stmt->prepare($query);
$stmt->bind_param('ss',$name,$pwd);
$name=$_POST["UserName"];
$pwd=$_POST["Password"];
$stmt->execute();
if($stmt->errno==0) {
$success=true;
}else {
$success=false;
}
$stmt->close();
$conn->close();
}
?>

The bind_param method needs to be explained slightly, and the meaning of the first parameter is the parameter type. Each character corresponds to a parameter, s represents a string, i represents an integer, d represents a floating point number, and b represents a blob. Finally, add a little tip for this page:
Copy the codeThe code is as follows:

<?php
if(!$match) { ?>
<p>Password and Confirm Password must match.</p>
<?php
}
?>
<?php
if(isset($success)) {
if($success) {
echo '<p>User Created Successfully!';
}elseif($sucess==false) {
echo '<p>User Name existed.';
}
}
?>

Next, we write a user list page.
Copy the codeThe code is as follows:

<table>
<tr><th>User Name</th><th>CreateTime</th><th>Action</th>
</tr>
<?php
include '';
$query="select * from User;";
$res=$mysql->query($query);
while($row=$res->fetch_array()) {
?>
<tr>
<td><?= $row['UserName'] ?></td>
<td><?= date('Y-m-d',strtotime($row['CreateTime']))?> </td>
<td><a href="?action=update&ID=<?= $row['UserName'] ?>">Edit</a>
<a href="action=delete&ID=<?= $row['UserName'] ?>">Delete</a>
</td>
</tr>
<?php
}
$res->close();
$mysql->close();
?>
</table>