Answer the question
In order to leave comments, you need to log in
How to set a default value in a database?
Good night everyone!) How can I make the site so that by default some text is displayed from the database? For example, the user will see the text: no data.
<div class="stat">
<h2>Статистика</h2>
<?=$row['stat']?>
</div>
Answer the question
In order to leave comments, you need to log in
When creating a field in a table (in the database) - you can specify the default value.
And you can already do a check in php and if it's empty - display "no data"
PHP code:
try {
$dbh = new PDO("mysql:host=$host;dbname='название_базы'", 'имя_пользователя', 'пароль');
} catch(PDOException $e) {
echo "Нет соединения с базой данных";
}
$user_id = 'id_нужной_записи';
$user = $dbh->prepare("SELECT name FROM users WHERE id = :user_id");
$user->execute(array('user_id' => $user_id))->fetch(PDO::FETCH_OB);
if($user) echo $user->name;
else echo 'Пользователь не найден.'
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question