PHP数据库连接mysql与mysqli对比分析_php技巧
一、mysql与mysqli的概念相关
1、mysql与mysqli都是php方面的函数集,与mysql数据库关联不大。
2、在php5版本之前,一般是用php的mysql函数去驱动mysql数据库的,比如mysql_query()的函数,属于面向过程3、在php5版本以后,增加了mysqli的函数功能,某种意义上讲,它是mysql系统函数的增强版,更稳定更高效更安全,与mysql_query()对应的有mysqli_query(),属于面向对象,用对象的方式操作驱动mysql数据库
二、mysql与mysqli的区别
1、mysql是非持继连接函数,mysql每次链接都会打开一个连接的进程。
2、mysqli是永远连接函数,mysqli多次运行mysqli将使用同一连接进程,从而减少了服务器的开销。mysqli封装了诸如事务等一些高级操作,同时封装了DB操作过程中的很多可用的方法。
三、mysql与mysqli的用法
1、mysql(过程方式):
$conn = mysql_connect('localhost', 'user', 'password'); //连接mysql数据库 mysql_select_db('data_base'); //选择数据库 $result = mysql_query('select * from data_base');//第二个可选参数,指定打开的连接 $row = mysql_fetch_row( $result ) ) //只取一行数据 echo $row[0]; //输出第一个字段的值
PS:mysqli以过程式的方式操作,有些函数必须指定资源,比如mysqli_query(资源标识,SQL语句),并且资源标识的参数是放在前面的,而mysql_query(SQL语句,'资源标识')的资源标识是可选的,默认值是上一个打开的连接或资源。
2、mysqli(对象方式):
<pre name="code" class="php">$conn = new mysqli('localhost', 'user', 'password','data_base'); //要使用new操作符,最后一个参数是直接指定数据库 //假如构造时候不指定,那下一句需要$conn -> select_db('data_base')实现 $result = $conn -> query( 'select * from data_base' ); $row = $result -> fetch_row(); //取一行数据 echo row[0]; //输出第一个字段的值
使用new mysqli('localhost', usenamer', 'password', 'databasename');会报错,提示如下:
Fatal error: Class 'mysqli' not found in ...
一般是mysqli是没有开启的,因为mysqli类不是默认开启的,win下要改php.ini,去掉php_mysqli.dll前的;,linux下要把mysqli编译进去。
四、mysql与mysqli举例
1、mysql
<span style="font-size:14px;">$con=mysql_connect($dbhostip,$username,$userpassword); $db = mysql_select_db($dbdatabasename,$con); //执行语句 $qres=mysql_query("SELECT id,GoodsName FROM user"); //提取一条数据 $row=mysql_fetch_row($result); //mysql_fetch_row只能提取出查询结果的第一条记录 //提取多条记录 $reslist = array(); $i=0; while($row = mysql_fetch_row($res)){ $reslist[$i] = $row; $i++; } mysql_close($con); </span> //mysql_fetch_row 提取的结果是没有查询中的字段名了(也就是没有键id,GoodsName,只有值) //mysql_fetch_assoc 提取的结果有键值 //mysql_fetch_array提取的结果有键值,是前面两种的综合
在mysql_connect()、mysql_select_db()等函数之前使用@(错误控制运算符),可以忽略掉系统产生的错误信息,然后我们用die()来自定义错误信息;
对于mysql_query()函数的返回值,如果执行的语句有返回值(如SELECT、SHOW、DESCRIBE等),则返回相应数据(成功时)或FALSE(失败时);如果执行的语句没有返回值(如DELETE、DROP、INSERT、UPDATE等),则返回TRUE(成功时)或FALSE(失败时)。
2、mysqli
$db=new mysqli($dbhostip,$username,$userpassword,$dbdatabasename); if(mysqli_connect_error()){ echo 'Could not connect to database.'; exit; } $result=$db->query("SELECT id,GoodsName FROM user"); $row=$result->fetch_row();
五、php中mysqli用法举例
<?php $variable = $_POST['variable']; //Post从表单中提取变量 if(!$variable) //如果变量为空,输出错误信息并退出 { echo 'You hava not entered search details.Please go back and try again.'; exit; } if(!get_magic_quotes_gpc()) //该函数用于判断get_magic_quotes_gpc是否开启,get_magic_quotes_gpc参数是用于确定是否将从post,get,cookie过来的数据增加转义字符和从数据库出来的数据去掉转义字符 { $variable = addlashes($variable);//在特殊文本字符前增加转义字符 //stripslashes()用于在去掉转义字符 } $localhost = 'hostname';//主机名 $user = 'username';//用户名 $pwd = 'password';//密码 $db = 'databasename';// @$link = new mysqli($localhost,$user,$pwd,$db);//连接数据库 if(mysqli_connect_errno())//如果数据库连接失败,输出错误信息并退出 { echo 'Error: Coulid not connect to database. Please try again later.'; exit; } $query = "SELECT row from table where some situation";//查询语句 $result = $link -> query($query);//查询并返回结果 $num_results = $result -> num_rows; //结果行数 echo "<p>Number of row found: ". $num_results ."</p>";//输出行数 for($i = 0;$i < $num_results;$i++)//循环输出每组元素 { $row = $result -> fetch_assoc();//提取元素,一次一行,fetch_assoc()提取出的元素,有属性以及值 echo stripslashes($row['attributename']);//按属性(键)提取值 echo "<br/>"; } $result -> free();//释放内存 $link -> close();//断开数据库连接 ?>
以上就是关于PHP数据库连接mysql与mysqli的区别与用法,希望对大家的学习有所帮助。

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

AI Hentai Generator
Generate AI Hentai for free.

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



The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

PHP is not dying, but constantly adapting and evolving. 1) PHP has undergone multiple version iterations since 1994 to adapt to new technology trends. 2) It is currently widely used in e-commerce, content management systems and other fields. 3) PHP8 introduces JIT compiler and other functions to improve performance and modernization. 4) Use OPcache and follow PSR-12 standards to optimize performance and code quality.

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

How to connect to MySQL using phpMyAdmin? The URL to access phpMyAdmin is usually http://localhost/phpmyadmin or http://[your server IP address]/phpmyadmin. Enter your MySQL username and password. Select the database you want to connect to. Click the "Connection" button to establish a connection.
