Simple way to read single record from MySQL

后端 未结 20 1283
一整个雨季
一整个雨季 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:38

    The easiest way is to use mysql_result. I copied some of the code below from other answers to save time.

    $link = mysql_connect('localhost','root','yourPassword')
    mysql_select_db('database',$link);
    $sql = 'SELECT id FROM games'
    $result = mysql_query($sql,$link);
    
    $num_rows = mysql_num_rows($result);
    
    // i is the row number and will be 0 through $num_rows-1
    for ($i = 0; $i < $num_rows; $i++) {
        $value = mysql_result($result, i, 'id');
        echo 'Row ', i, ': ', $value, "\n";
    }
    

提交回复
热议问题