Discuss the different ways for looping over a query result in PHP.
What are placeholders? How are they used in PHP database programming?
OBJECT AND OBJECT-RELATIONAL DATABASE
How do regular inheritance, multiple inheritance, and selective inheritance differ?
What are the main differences between designing a relational database and an object database?
XML EXTENSIBLE MARKUP LANGUAGE
What is the difference between data-centric and document-centric XML documents?
WEB DATABASE PROGRAMMING
Full Answer Section
$result = mysqli_query($conn, "SELECT * FROM users");
while ($row = mysqli_fetch_assoc($result)) {
echo $row['name'] . "<br>";
}
fetch_object()
: This method returns an object with properties corresponding to the column names. You can access the properties using the object syntax:
PHP
$result = mysqli_query($conn, "SELECT * FROM users");
while ($row = mysqli_fetch_object($result)) {
echo $row->name . "<br>";
}
Placeholders
Placeholders are used to dynamically insert values into an SQL query. They help prevent SQL injection attacks by separating the query structure from the data. In PHP, placeholders are typically represented by question marks (?
).
Here's an example of using placeholders with prepared statements:
PHP
$stmt = mysqli_prepare($conn, "SELECT * FROM users WHERE username = ?");
mysqli_stmt_bind_param($stmt, "s", $username);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
while ($row = mysqli_fetch_assoc($result))
{
// ...
}
Object and Object-Relational Databases
Inheritance
- Regular Inheritance: A child class inherits all the properties and methods of its parent class.
- Multiple Inheritance: A child class can inherit from multiple parent classes.
- Selective Inheritance: A child class can inherit only specific properties and methods from its parent class.
Relational vs. Object Databases
Feature | Relational Database | Object Database |
---|---|---|
Data Structure | Tables and rows | Objects and classes |
Data Model | Relational model | Object-oriented model |
Query Language | SQL | OQL |
Data Complexity | Simpler data structures | Complex data structures |
Performance | Generally faster for simple queries | Can be slower for complex queries, but offers better performance for complex object-oriented operations |
XML: Data-Centric vs. Document-Centric
- Data-Centric XML: Focuses on the structure and content of data, often used for data exchange and storage. It adheres to strict schema definitions.
- Document-Centric XML: Focuses on the structure and content of documents, often used for human-readable documents like books or articles. It is more flexible and less structured than data-centric XML.
Sample Answer
Looping Over Query Results in PHP
PHP provides several methods to iterate over the results of a database query:
fetch_array()
: This method returns an array with each row of the result set. You can iterate over this array using aforeach
loop:
PHP
$result = mysqli_query($conn, "SELECT * FROM users");
while ($row = mysqli_fetch_array($result)) {
echo $row['name'] . "<br>";
}
fetch_assoc()
: This method returns an associative array with column names as keys. You can also use aforeach
loop to iterate over this:
PHP