0

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!

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • Try using `UUID() as uuid`, that may help. – gen_Eric Mar 06 '17 at 17:46
  • What have you tried? How are you running this query? How are you getting the results? Have you tried to `var_dump()` the result set to see what fields you have? – gen_Eric Mar 06 '17 at 17:47
  • @RocketHazmat What do you mean? I've used it to INSERT fields, but need it on the PHP side as a variable for future operations. – Dmitry Petrov Mar 06 '17 at 17:50
  • @RocketHazmat Yeah I've tried vardump and got "object(mysqli_result)#2 (5) { ["current_field"]=> int(0) ["field_count"]=> int(1) ["lengths"]=> NULL ["num_rows"]=> int(1) ["type"]=> int(0) }" Don't know how to access the actual string. – Dmitry Petrov Mar 06 '17 at 17:50
  • Well, you didn't describe your issue or what you are doing in your question, so I just guessed at a solution. You need to give *details* about your issue, so we understand what you are doing. Anyway, if you want to get a value from the database, you're going to need to `SELECT` it. All `INSERT` does is *insert* a row, it does not give you back any of the values. – gen_Eric Mar 06 '17 at 17:55
  • @RocketHazmat Right, sorry - I appreciate your help! Here's what I'm currently trying $query = "SELECT UUID()"; if ($result = $mysqli->query($query)){ $appuserid = var_dump($result); } – Dmitry Petrov Mar 06 '17 at 17:56
  • First off, `var_dump()` doesn't return anything, so your `$appuserid` will not contain anything. Second, you need to *fetch* the results after running the query. See: http://php.net/manual/en/mysqli-result.fetch-array.php – gen_Eric Mar 06 '17 at 17:59
  • 1
    @dpetrov Please **edit** the question to add information. Don't scatter it in comments, where code is unreadable. – Álvaro González Mar 06 '17 at 17:59

2 Answers2

1

you are mostly there

$uuid = $mysqli->query("SELECT uuid()")->fetch_row()[0];
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
MikeT
  • 5,398
  • 3
  • 27
  • 43
  • Not sure why you decided to write ten times more code than needed, but I left only what's really needed – Your Common Sense Mar 06 '23 at 15:14
  • because i generally access mysqli via a helper class that handles all the nicities of preparing a statements, binding the params error handling , etc and was a little to rushed to completely rewrite so i just trimmed down the helper class to mimimum to get it working – MikeT Mar 06 '23 at 16:14
  • Just for your info, the code you've got was unusable due to some errors. foreach with unconditional return inside is rather questionable move. By the way, "error handling" is probably redundant, given mysqli nowadays reports its errors by itself. – Your Common Sense Mar 06 '23 at 16:32
0

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.

Alister Bulman
  • 34,482
  • 9
  • 71
  • 110