A
A
Alexander Konovalov2014-06-28 00:30:40
PHP
Alexander Konovalov, 2014-06-28 00:30:40

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

2 answer(s)
B
bahek2462774, 2014-06-28
@Huf

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"

I
Ivan, 2014-06-28
@0neS

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 question

Ask a Question

731 491 924 answers to any question