What is the correct syntax for querying text values from a database in PHP using MySQL?
When querying text values from a database in PHP using MySQL, you need to use single quotes around the text values in your SQL query. This ensures that the text values are treated as strings by MySQL. Failure to do so may result in syntax errors or unexpected behavior in your query.
// Example of querying text values from a database in PHP using MySQL
$mysqli = new mysqli("localhost", "username", "password", "database");
// Query to select records where the text value is 'example'
$query = "SELECT * FROM table_name WHERE text_column = 'example'";
$result = $mysqli->query($query);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "ID: " . $row["id"]. " - Text: " . $row["text_column"]. "<br>";
}
} else {
echo "0 results";
}
$mysqli->close();
Related questions
-
PHP array filter
-
PHP preg_match_all arrays
-
PHP optimization efficiency