Simple way to read single record from MySQL

后端 未结 20 1345
一整个雨季
一整个雨季 2020-11-30 23:46

What\'s the best way with PHP to read a single record from a MySQL database? E.g.:

SELECT id FROM games

I was trying to find an answer in t

20条回答
  •  佛祖请我去吃肉
    2020-12-01 00:33

    $link = mysql_connect('localhost','root','yourPassword')
    mysql_select_db('database_name', $link);
    $sql = 'SELECT id FROM games LIMIT 1';
    $result = mysql_query($sql, $link) or die(mysql_error());
    $row = mysql_fetch_assoc($result);
    print_r($row);
    

    There were few things missing in ChrisAD answer. After connecting to mysql it's crucial to select database and also die() statement allows you to see errors if they occur.

    Be carefull it works only if you have 1 record in the database, because otherwise you need to add WHERE id=xx or something similar to get only one row and not more. Also you can access your id like $row['id']

提交回复
热议问题