Quote:
ob_start();
echo random_strings(10);
$Output = ob_get_contents();
ob_end_clean();
As far as I can see, that's a very convoluted way of saying:
$Output = random_strings(10);
You'll need a SQL query to check whether the ID already exists. You'll need to generate an ID and test whether it exists inside a loop, until you find one that doesn't exist. For example:
function generate_id($conn) {
$stmt = mysqli_prepare($conn, "SELECT Count(1) FROM ID WHERE Id = ?");
mysqli_stmt_bind_param($stmt, "s", $Id);
mysqli_stmt_bind_result($stmt, $Count);
do
{
$Id = random_strings(10);
mysqli_stmt_execute($stmt);
} while ($Count > 0)
return $Id;
}
PHP: do-while - Manual[
^]
PHP: mysqli_stmt::bind_result - Manual[
^]
Once you've opened your connection, call this function to generate a new random ID:
$conn = mysqli_connect("localhost", "probleem_uniqid", "Password", "DBname");
if($conn === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$Output = generate_id($conn);
...
NB: There is still a fairly small possibility that two requests executing at the same time might generate the same ID which isn't already in the database, and then try to insert a record with that ID. Assuming your database field is correctly constrained to be unique, one request will fail.