I'm trying to generate a random number and insert it among other information if it doesn't exist in the same column. All with AJAX, so it is in another php file.
I'm using this:
$dbhost = "127.0.0.1";
$dbuser = "root";
$dbpass = "";
$dbname = "feely";
$db = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
function func(){
$success = false;
while (!$success) {
$rndInt = rand(0, pow(36, 6) - 1);
$rndStr = base_convert ($rndInt, 10, 36);
$rndStr = str_pad($rndStr , 6, "0", STR_PAD_LEFT);
global $db;
$query = "SELECT * FROM pedidos WHERE Codigo = {$rndStr} LIMIT 1";
$db->query($query);
if (!$db->fetchColumn()) { // value does not exist yet
// insert new random value
//$db->query($query);
$success = true; // will terminate the loop
return $rndStr;
} else { // value already exists
// do nothing - try again in the next loop
}
}
}
$code = func();
$device= $_POST['devicename'];
$issue= $_POST['issue'];
$sql = "insert into pedidos (Code, Device, Issue) values (";
$sql .= "'$codigo', '$device', '$issue')";
if(mysqli_query($db, $sql)){
echo 'success';
}
But I'm getting:-
"Call to undefined method PDO::fetchColumn()".
It is important to say that I'm a front-end developer, so... :D
Help ;-;