我应该以面向对象的方式使用PHP PDO吗?
我是PDO新手,听说这是做web应用程序的更好方法,我正在开发小型计费应用程序。 有一个dobut,我可以像下面这样编码吗我应该以面向对象的方式使用PHP PDO吗?,php,mysql,oop,pdo,Php,Mysql,Oop,Pdo,我是PDO新手,听说这是做web应用程序的更好方法,我正在开发小型计费应用程序。 有一个dobut,我可以像下面这样编码吗 <?php require_once '../../classes/PDO_connection.php'; $type = 'initial_stock'; $item_code = $_POST["item_code"]; $category = $_POST["category"]; $variety = $_POST
<?php
require_once '../../classes/PDO_connection.php';
$type = 'initial_stock';
$item_code = $_POST["item_code"];
$category = $_POST["category"];
$variety = $_POST["variety"];
$quantity = $_POST["quantity"];
$price = $_POST["price"];
$f_price = number_format($price, '2', '.', '');
$total = $quantity * $price;
$full_name = $item_code.':'.$category.':'.$variety.':'.$f_price;
$in_stock = $quantity;
$prev_stock = '';
//inserting data from initial stock page
$stmt = $pdo->prepare("INSERT INTO silk (type, item_code, category, variety, quantity, price, full_name, total, in_stock, sale_date, entered_date) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now())");
$stmt->bindParam(1, $type);
$stmt->bindParam(2, $item_code);
$stmt->bindParam(3, $category);
$stmt->bindParam(4, $variety);
$stmt->bindParam(5, $quantity);
$stmt->bindParam(6, $price);
$stmt->bindParam(7, $full_name);
$stmt->bindParam(8, $total);
$stmt->bindParam(9, $in_stock);
$stmt->execute();
//getting all initial stock for dispaling
$stmt = $pdo->prepare("SELECT * FROM silk WHERE type='initial_stock'");
$stmt->execute();
$rows = $stmt->fetchAll();
foreach($rows as $stock){
echo "<tr class='active'>
<td>".$stock['item_code']."</td>
<td>".$stock['category']."</td>
<td>".$stock['variety']."</td>
<td>".$stock['price']."</td>
<td>".$stock['quantity']."</td>
<td><a id='initial_stock_silk_delete' id_to_delete=".$stock['id'].">Delete</a></td>
</tr>";
}
这完全取决于你。如果你想使用一个你已经习惯了的函数——没有人禁止你创建一个
你必须考虑的唯一一件事——这样一个函数应该至少接受两个参数——一个带占位符的查询和一个带数据绑定的数组什么让你认为可能会有呢?我的常识是没有,但我的朋友们说使用oop更好,这很抱歉,但是你根本不知道你在说什么。OOP在某些方面确实更好,但你的问题毫无意义。首先,“更好”并不意味着“更快”。第二,你也可以用面向对象的方法来实现这样的功能——你只需要学习面向对象的方法就可以了。如果您愿意,Raw PDO不是使用PDO OOP的唯一方法-您可以创建一个用于数据库访问的类,并使您的函数成为这个类的方法。但这不是重点。无论哪种方式,您最终都将使用此函数-使用原始PDO的一行而不是三行。这就是重点。