PHP interacts with MySQL
PHP and MySQL interaction
is demonstrated with a simple example. The code logic is: connect, create a table, insert data, obtain data and display the results.
(1): Create a test database from the command line
(2): The following is a php script (Connect, create a table, insert data, get data and display the results)
<?php //连接数据库 $mysqli = new mysqli("localhost", "root", "", "test"); if(mysqli_connect_errno()){ printf("Connect failed:%\n",mysqli_connect_error()); exit(); } else { //建立表testtable $sql="create table testtable(id int not null primary key auto_increment,testField varchar(75))"; $res=mysqli_query($mysqli, $sql); if($res=true){ echo "Table testtable successfully created."; //插入一条记录 $sql_1="insert into testtable(testField) values ('some value')"; $res_1=mysqli_query($mysqli, $sql_1); if ($res_1=true) { echo "<br/>A record has been inserted."; //查询记录 $sql_2="select * from testtable"; $res_2=mysqli_query($mysqli, $sql_2); if ($res_2){ //显示记录条数 $number_of_rows = mysqli_num_rows($res_2); printf("<br/>Result set has %d rows.\n",$number_of_rows); //获取数据并显示结果 while ($newArray = mysqli_fetch_array($res_2, MYSQLI_ASSOC)){ $id = $newArray['id']; $testField = $newArray['testField']; echo "<br/>The ID is ".$id." and the text is ".$testField."<br/>"; } } else { printf("Could not retrieve records:%s\n",mysqli_error($mysqli)); } } else { printf("Could not insert record:%s\n",mysqli_error($mysqli)); } } else { printf("Could not create table:%s\n",mysqli_error($mysqli)); } mysqli_close($mysqli); }
Access through the server , the effect is as follows:
Enter the command line to view the database
- Size: 176.6 KB
-
- Size: 20.4 KB
- Size: 145.7 KB
- View image attachment
The above has introduced the interaction between PHP and MySQL, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

When writing web applications using PHP, a MySQL database is often used to store data. PHP provides a way to interact with the MySQL database called MySQLi. However, sometimes when using MySQLi, you will encounter an error message, as shown below: PHPFatalerror:Calltoundefinedfunctionmysqli_connect() This error message means that PHP cannot find my

PDOPDO is an object-oriented database access abstraction layer that provides a unified interface for PHP, allowing you to use the same code to interact with different databases (such as Mysql, postgresql, oracle). PDO hides the complexity of underlying database connections and simplifies database operations. Advantages and Disadvantages Advantages: Unified interface, supports multiple databases, simplifies database operations, reduces development difficulty, provides prepared statements, improves security, supports transaction processing Disadvantages: performance may be slightly lower than native extensions, relies on external libraries, may increase overhead, demo code uses PDO Connect to mysql database: $db=newPDO("mysql:host=localhost;dbnam

Solution to php unable to connect to mysqli: 1. Open the "php.ini" file; 2. Find "mysqli.reconnect"; 3. Change "mysqli.reconnect = OFF" to "mysqli.reconnect = on".

If you encounter the following error message when using PHP to connect to a MySQL database: PHPWarning:mysqli_connect():(HY000/2002):Connectionrefused, then you can try to solve this problem by following the steps below. To confirm whether the MySQL service is running normally, you should first check whether the MySQL service is running normally. If the service is not running or fails to start, it may cause a connection refused error. you can

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

The running file of mysql is mysqld; mysqld is an executable file, which represents the Mysql server program. Executing this file can directly start a server process; and mysqld_safe is a startup script, which will indirectly call mysqld and also start a monitor. process.

When using the mysqli extension to connect and operate a MySQL database, you sometimes encounter the error PHPFatalerror:Calltoundefinedmethodmysqli::prepare(). This error is usually caused by the following reasons: PHP has insufficient support for the mysqli extension; the mysqli extension is not loaded or configured correctly; there are syntax errors in the PHP code; the MySQL server is not correctly configured or running

linux printf is used in the command line. This command is used to format print data; the command format of printf is "printf FORMAT [ARGUMENT]...printf OPTION", where the "help" option means displaying help information, and the "version" option means Display version information.
