从mysql查询中检索uuid

时间:2015-08-18 13:36:28

标签: php mysql sql

我将uuid()分配给id字段。它完美地运作。但我不知道如何echo生成的uuid回到用户身上。我怎么能这样做?

我的代码:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // prepare sql and bind parameters
    $stmt = $conn->prepare("INSERT INTO MyGuests (id,firstname, lastname, email)
    VALUES (UUID(),:firstname, :lastname, :email)");
    $stmt->bindParam(':firstname', $firstname);
    $stmt->bindParam(':lastname', $lastname);
    $stmt->bindParam(':email', $email);

    // insert a row
    $firstname = "John";
    $lastname = "Doe";
    $email = "john@example.com";
    $stmt->execute();

    echo "New records created successfully";
    }
catch(PDOException $e)
    {
    echo "Error: " . $e->getMessage();
    }
$conn = null;
?> 

1 个答案:

答案 0 :(得分:0)

你不能。

您唯一的选择是将其分为两个查询。

  • SELECT UUID()
  • 将创建的UUID检索到php变量($uuid
  • 将该变量插入准备好的陈述中。

    $stmt = $conn->prepare("INSERT INTO MyGuests (id,firstname, lastname, email)
    VALUES (:uuid,:firstname, :lastname, :email)");
    $stmt->bindParam(':uuid', $uuid);
    $stmt->bindParam(':firstname', $firstname);
    $stmt->bindParam(':lastname', $lastname);
    $stmt->bindParam(':email', $email);