Simple way to read single record from MySQL

后端 未结 20 1357
一整个雨季
一整个雨季 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条回答
  •  旧时难觅i
    2020-12-01 00:22

    Better if SQL will be optimized with addion of LIMIT 1 in the end:

    $query = "select id from games LIMIT 1";


    SO ANSWER IS (works on php 5.6.3):

    If you want to get first item of first row(even if it is not ID column):

    queryExec($query) -> fetch_array()[0];
    

    If you want to get first row(single item from DB)

    queryExec($query) -> fetch_assoc();
    

    If you want to some exact column from first row

    queryExec($query) -> fetch_assoc()['columnName'];
    

    or need to fix query and use first written way :)

提交回复
热议问题