Dmitry Petrov
Dmitry Petrov

Reputation: 11

How do I get the result of UUID() in a PHP var?

I can't for the live of me figure out how to get the result of a simple UUID() query into a variable/echo it/whatever.

$query = "SELECT UUID()";
if ($result = $mysqli->query($query)){
    $appuserid = var_dump($result);
} 

vardump of result:

object(mysqli_result)#2 (5) {
    ["current_field"]=> int(0)
    ["field_count"]=> int(1)
    ["lengths"]=> NULL
    ["num_rows"]=> int(1)
    ["type"]=> int(0)
}

Help would be hugely appreciated!

Upvotes: 0

Views: 1236

Answers (2)

MikeT
MikeT

Reputation: 5500

you are mostly there

$uuid = $mysqli->query("SELECT uuid()")->fetch_row()[0];

Upvotes: 1

Alister Bulman
Alister Bulman

Reputation: 35149

There is a SELECT LAST_INSERT_ID(); that you can send immediately after an INSERT, but that won't work for bulk-inserts, and it's for an auto_increment primary key.

It may be more useful to generate the UUID within your own code, with a library such as ramsey/uuid, and send that in with the rest of the data to be inserted. You can then simply use the variable you already have.

Upvotes: 0

Related Questions