我一直在使用与MySQL *的处理方式,直到最近。 现在我想转移到mysqli的和面向对象的方法。 许多网上资源和书籍声明OOP比过程更甚至在PHP的情况下。 通过一些在线教程去后,我写了一个小程序,连接到数据库,并选择一个值。 我想知道为什么至上的方针,目的是更好? 加上这是编写一个PHP的面向对象网页的正确方法?
在面向对象的方法
$host = "localhost"; $username = "root"; $password = ""; $dbname = "compdb"; @ $db = new mysqli($host, $username, $password, $dbname); if(mysqli_connect_errno()) { die("Connection could not be established"); } $query = "SELECT company_id FROM company_basic_details WHERE company_name = 'ABC'"; $result = $db->query($query); $total_num_rows = $result->num_rows; echo "The Results Are : <br>"; while($row = $result->fetch_array()) { echo $row['company_id']; } ?>
该程序的方法
<?php
$host = "localhost";
$username = "root";
$password = "";
$dbname = "compdb";
@ $db = mysqli_connect($host, $username, $password, $dbname);
if(mysqli_connect_errno())
{
die("Connection could not be established");
}
$query = "SELECT company_id FROM company_basic_details WHERE company_name = 'ABC'";
$result = mysqli_query($db, $query);
$total_num_rows = mysqli_num_rows($result);
echo "The Results Are : <br>";
while($row = mysqli_fetch_array($result))
{
echo $row['company_id'];
}
?>