How do I connect to the database after php opens the mysqli extension

  • 2020-05-14 05:13:18
  • OfStack

Mysqli is a feature only available after php5. Friends who do not have the extension open can open your php.ini configuration file.
Look for the following statement:; extension= php_mysqli.dll change it to: extension= php_mysqli.dll.
It has many new features and advantages over mysql
(1) support local binding, preparation (prepare) and other syntax
(2) execute the error code of sql statement
(3) execute multiple sql simultaneously
(4) it also provides object-oriented methods to call the interface.
The following 11 use the php instance to connect to the mysqli database!
Use method 1: use a traditional process-oriented approach
The php code is as follows:

 
<?php 
$connect = mysqli_connect('localhost','root','','volunteer') or die('Unale to connect'); 
$sql = "select * from vol_msg"; 
$result = mysqli_query($connect,$sql); 
while($row = mysqli_fetch_row($result)){ 
echo $row[0]; 
} 
?> 

Using method 2: using an object-oriented method invocation interface (recommended)
See the php code as follows:
 
<?php 
// Create the object and open the connection, finally 1 Three parameters are the name of the selected database  
$mysqli = new mysqli('localhost','root','','volunteer'); 
// Check that the connection was successful  
if (mysqli_connect_errno()){ 
// Pay attention to mysqli_connect_error() New features  
die('Unable to connect!'). mysqli_connect_error(); 
} 
$sql = "select * from vol_msg"; 
// perform sql Statement, completely object-oriented  
$result = $mysqli->query($sql); 
while($row = $result->fetch_array()){ 
echo $row[0]; 
} 
?> 

The two php instances run exactly the same, and you can clearly see the advantages of using mysqli class objects to build database connections!
I don't need to talk about inserting and modifying records, just change 1 sql statement, and I'll talk about prepare interface features in the next article!


Related articles: