Simple way to read single record from MySQL

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

    Assuming you are using an auto-incrementing primary key, which is the normal way to do things, then you can access the key value of the last row you put into the database with:

    $userID = mysqli_insert_id($link);
    

    otherwise, you'll have to know more specifics about the row you are trying to find, such as email address. Without knowing your table structure, we can't be more specific.

    Either way, to limit your SELECT query, use a WHERE statement like this: (Generic Example)

    $getID = mysqli_fetch_assoc(mysqli_query($link, "SELECT userID FROM users WHERE something = 'unique'"));
    $userID = $getID['userID'];
    

    (Specific example) Or a more specific example:

    $getID = mysqli_fetch_assoc(mysqli_query($link, "SELECT userID FROM users WHERE userID = 1"));
    $userID = $getID['userID'];
    
    0 讨论(0)
  • 2020-12-01 00:46

    Say that you want to count the rows in an existing table:

    Use mysql_result with only one parameter set to "0"

    Like this:

    $string = mysql_result(
              mysql_query("SELECT COUNT(*) FROM database_name.table_name ")
    ,0);
    

    Put ( ` ) around database_name and table_name

    var_dump($string);
    
    // string(1) "5" //if it only has 5 records in the table
    

    Note: if you query for something else like

    SELECT * FROM database.table LIMIT 1
    

    and the table has more than one column then you WILL NOT get an array() in your var_dump you will get the first column only

    0 讨论(0)
提交回复
热议问题