- You cannot use
or die in HTML output. You will receive invalid HTML. Often errors are detected at a low level, but can only be handled at a higher level. Correct error handling involves transmitting the error until it is properly processed. You can use return values ββto hold errors (if there can be different types of errors and errors without errors) or use exceptions . You can get the number of rows of results using mysqli_result->num_rows or PDOStatement->rowCount . Please note that the latter is not available for all databases supported by PDO, but for MySQL. However, both require buffered queries, which is less efficient since the query must complete before the program can continue (i.e., execute is synchronous with respect to the query) and the entire result set must be stored in memory. The Phil method shows working with other databases, although it suffers the same performance costs as buffered queries. Alternative, unbuffered queries ( execute asynchronous with respect to the query) is to work with strings as they appear, ignoring the total number of rows to the end. PDOStatement supports Traversable , which means you can PDOStatement over it with a foreach , so you don't need to know the total number of lines to iterate over.
... $query->execute(array(':after' => $date)); foreach ($query as $row) { ... }
This makes the processing results in other modules particularly enjoyable, since they do not need to know what they are repeating. You can even return results as instances of this class.
$query->execute(...); $query->setFetchMode(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE, 'Article');
The only drawback to using PDOStatement is that query results usually have a one-time use; if you do not use the cursor, you can scroll them only once.
This question has been asked (probably many times) before: When is the need for prepared pdo statements in PHP indicated?
You can think of prepared statements similar to functions. Using the function, you take a block of code, parameterize part of it, and package it. A function can be called several times, but it needs to be defined only once. The same applies to prepared statements. Since the values ββare stored separately from the code, injection is not possible in the parameters of the prepared statement (injection is the result of confusing data with the code ).
As with functions, you cannot arbitrarily replace parts of an instruction with a parameter. First, you need to respect the syntax. Another limitation may be the parameterization of only a certain type of thing. Both functions and parameters usually allow you to parameterize values, although in some languages ββthe value is considered quite wide. In SQL, what is considered a value is rather narrow. Identifiers (names for databases, tables, columns, stored procedures, and c) are not values. Lists of values ββ(such as the right argument of the IN operator) are not values ββthemselves.
Turning around at one of the points in 1. slightly, in a well-designed project, the code is divided into different modules, based on exactly what functions it implements. This is called " separation of concerns " and leads to things like MVC and layered architectures. Principle The " level of data access that is responsible for accessing the database." There are various patterns that you can apply to implement this, but the essential aspect is nothing, except that the DAL must access the database or be affected by a change in how the data is stored. DAL can handle certain errors (partially or completely), but this should not concern user interaction or data display.
source share