10.07.2013, 00:27
I've got a PHP script that I want to convert so that I can handle it all in-game. The script generates a random plate and checks if the plate exists in the database. If it does, the script continues the while loop and generates another. If it doesn't, the plate is echo'd out so that the vehicle can be created. However, with the introduction of data cacheing in the latest SQL plugin I'm unsure of the best way to handle this in the script.
Here is my PHP script if anyone's interested.
Here is my PHP script if anyone's interested.
PHP код:
<?php
function randomUppercaseLetter()
{
/*
* Generates a random uppercase letter from the alphabet
*/
$int = rand(0,25);
$a_z = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$rand_letter = $a_z[$int];
return $rand_letter;
}
$con = mysqli_connect("host", "user", "pass", "dbname"); // Connects to the SQL database
if (mysqli_connect_errno($con))
{
// Give the error if one occurs
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$i = false; // The license plate is invalid
while ($i == false)
{
// Execute while loop until valid plate is found
if(rand(0,3) == 3) $plate = (randomUppercaseLetter() . rand(0, 9) . rand(1,9) . " " . randomUppercaseLetter() . rand(0, 9) . randomUppercaseLetter()); // Generate random 6-character plate
else $plate = (randomUppercaseLetter() . randomUppercaseLetter() . rand(0, 9) . rand(1,9) . " " . randomUppercaseLetter() . rand(0, 9) . randomUppercaseLetter()); // Generate random 7-character plate
$queryString = "SELECT * FROM vehicles WHERE `plate`='" . $plate . "'"; // Use SELECT query to find if plate exists
$result = mysqli_query($con, $queryString); // Execute query and store result
if(mysqli_num_rows($result) == 0)
{
// If there are no rows, set $i to true in order to stop the while loop, close the connection and echo the generated plate.
echo $plate;
$i = true;
mysqli_close();
}
}
?>