Reputation: 25
I am trying to generate a unique String with a function. feed the output into a different function that checks a database with PDO to see if that String of characters already exists and then parse it to be entered into a database using PDO. IF for some reason the query comes up positive the function that generates the String gets called again and the process repeats itself. ( on a side note this is why I need this to be done via functions; so I can recall the function over and over if I need to, if you have any better ideas please do share)
my code
$pdo = new PDO("mysql:host=localhost;dbname=notifiyr;charset=utf8mb4", 'root', '', [
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_EMULATE_PREPARES => false
]);
$length = 10;
function generatecode($length,$pdo){
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
$abc = $randomString;
checkcode($abc);
}
function checkcode($abc){
$sql2 = "SELECT id FROM users WHERE code = :code";
if($stmt = $pdo->prepare($sql2,$abc,$pdo)){
$stmt->bindParam(":code", $param_code, PDO::PARAM_STR);
$param_code = $abc;
if($stmt->execute()){
if($stmt->rowCount() == 1){
generatecode();
} else{
$code = $abc;
}
} else{
$code_err = "something doest want to work here, come back later";
echo "Oops! Something went wrong. Please try again later.";
}
// Close statement
unset($stmt);
}
}
the problem I am having is that later on in the script I am supposed to enter this along with other variables into the database using PDO all of them enter but the randomly generated string.
edit
I changed my code to this
function generatecode($length,$pdo){
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
$abc = $randomString;
return $abc;
}
function checkcode($abc,$pdo){
$sql2 = "SELECT code FROM users WHERE code = :code";
if($stmt = $pdo->prepare($sql2,$abc,$pdo)){
$stmt->bindParam(":code", $param_code, PDO::PARAM_STR);
$param_code = $abc;
if($stmt->execute()){
if($stmt->rowCount() == 1){
generatecode();
} else{
$code = $abc;
return $code;
}
} else{
$code_err = "something doest want to work here, come back later";
echo "Oops! Something went wrong. Please try again later.";
}
// Close statement
unset($stmt);
}
}
generatecode($length,$pdo);
checkcode($abc,$pdo);
I've had trouble returning functions before in PHP, I hope this code is better
Upvotes: 1
Views: 54
Reputation: 90
The problem is that you are not using functions correctly, not only are you not parsing items incorrectly you are calling them incorrectly. read the documentation more carefully.
I think you would be want something like this
function generatecode($length,$pdo){
$characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
$abc = $randomString;
return checkcode($abc,$pdo);
}
function checkcode($abc,$pdo){
$sql2 = "SELECT code FROM users WHERE code = :code";
if($stmt = $pdo->prepare($sql2)){
$stmt->bindParam(":code", $param_code, PDO::PARAM_STR);
$param_code = $abc;
if($stmt->execute()){
if($stmt->rowCount() == 1){
generatecode();
} else{
$code = $abc;
return $code;
}
} else{
$code_err = "something doest want to work here, come back later";
echo "Oops! Something went wrong. Please try again later.";
}
// Close statement
unset($stmt);
}
}
$code = generatecode($length,$pdo);// this correctly calls the function
you're welcome
Upvotes: 1