0

Im new to PDO, heard that this is the better method to do web applications, and im developing small billing application. Having one dobut, can i do coding like below?

<?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>"; } 

In mysql, i call the function that has query and return the value, but i thought PDO no need that? am i correct? expecting proffesionals advice.... thanks.

1 Answer 1

1

It's entirely up to you. If you want to use a function you already accustomed with - nobody forbids you from creating one.

The only thing you MUST take into account - such a function should accept at least TWO arguments - a query with placeholders and an array with data to bind

Sign up to request clarification or add additional context in comments.

4 Comments

What makes you think there could be any?
my common sense say there is no, but my friends says go for oop that is better, thats y im here
Sorry, but you just have no idea what you're talking about. OOP indeed better in some ways, but your question just makes no sense. First, "better" doesn't mean "faster". Second, you can have such a function in OOP way as well - you only need to learn OOP for this. Raw PDO is not the only way to use PDO OOP
If you want - you can create a class for database access, and make your function this class' method. But it is not the point. either way you will end up using this function - a one line instead of three lines with raw PDO. This is the point.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.