Generate an integer bigger than 2,147,483,647 in PHP - php

I'm trying to make an API for my C# project in PHP to store users, but my problem is that my u_id (user id) doesn't go above 2,147,483,647. I get the error "duplicate found..." in SQL because I set it as unique, and all id's becomes 2,147,483,647 because it doesn't go above it. I've tried to force it into appending each number as a string onto a variable but that doesn't seem to work. (there are no errors with the code)
<?php
class f {
public static function randInt($length = 18) {
$output = array(rand(1, 9));
$return = "";
for($i = 0; $i < $length; $i++) {
$output[] = (string)mt_rand(0, 9);
}
for($j = 0; $j < count($output); $j++) {
$return .= (string)$output[$j];
}
return $return;
}
}
?>

Simply change column type INT to BIGINT.
ALTER TABLE tablename MODIFY columnname BIGINT;

Related

How can I make this programme format the string properly? PHP - Nested For Loop

<?php
$list = ['a', 'b', 'c'];
$count = [1, 3, 5];
function stringFormatter($c, $i) {
return "Char is $c & Int is $i";
}
for ($i = 0; i < $list; $i++) {
for ($j = 0; $j < $count; $j++) {
$string = stringFormatter($list[$i], $count[$j]);
print $string;
}
}
?>
This is sample code that I have rewritten to demonstrate an issue I'm having with a program where I want to assign a formatted string, with the right combination of char and int, to be able to execute an SQL statement, using said string. Ideal scenario is getting a string that has the combination of both char and int at that position in their respective lists. E.g. "Char is a & Int is 1". This doesn't currently compile and when I plug it into "Online PHP Sandbox" I get the error message: "Internal Server Error [500]." Any advice on how I could get this to work, or even alternative suggestions are welcome! Thanks in advance!
for ($i = 0; i < $list; $i++) {
for ($j = 0; $j < $count; $j++) {
There are a few syntax errors here - watch out for a missing $ (look at the i inside the outer loop). You probably mean something like $i<count($list) using the built in count function. Similarly in the inner loop you are referencing the $count array with $j<$count - do you mean $j<count($count) or are you using the outer array as in index for the inner element i.e. $j < $count[$i].
On a more general note there are a collection of functions built into php to better handle passing of values into mysql (etc) - you should probably be looking at using those - search on "sanitising" with php and mysqli.

Index of array not extracting correct element in array | PHP

I am having a weird problem in PHP. I have an array with some integer values in it. When I try to compare the values in the array to a target integer, the index does not work with the array and also cannot be incremented. Here is an example.
The problem lies in the if($j == $indexOfExposed[$k])
/* This is a simple function to show some letters of a patient name, while the rest of letters are replaced with asterisks.
#Param: Patient name
#Return: limited preview of patient name;
*/
function encodeName($name){
$len = strlen($name); // This is the lenghth of $name. used to itterate and set a range.
$numOfExposedLetters = rand(1, 4); // Random value assigned to see how many letters of the name will be exposed.
$indexOfExposed = array(); // This is an array of indexes for exposed letters.
$k = 0; // This counter is used to traverse the $indexOfExposed array.
$encodedName = ""; // This is the value we will return after it is generated.
$previous = 0; // Used to keep track of previous value in the $indexOfExposed array. This is incase we have repeated values in the array;
/* This loop is used to append random values to the arary of indexes,
With $numOfExposedLetters being the quantity of exposed letters. */
for($i = 0; $i < $numOfExposedLetters; $i++){
$indexOfExposed[$i] = rand(2, $len);
}
sort($indexOfExposed); // Sort the array, for easier access.
/* Ecoding name */
for($j = 1; $j <= $len; $j++){
if($indexOfExposed[$k] == $previous){
$encodedName .= "*";
$k++;
continue;
}
if($j == $indexOfExposed[$k]){
$encodedName .= $name[$j-1];
$previous = $indexOfExposed[$k];
$k++;
}
else
$encodedName .= "*";
$k++;
} // end of encode
return $encodedName;
}
This code takes in a person's name and replaces the letters in the name with asterisks. random indexes with expose the actual letters in the name.
Debugging your code is a tedious work that is not worth it. I suggest you an alternative implementation that, hopefully, uses the algorithm you have in mind, is easier to read and understand and probably runs faster.
function encodeName($name)
{
$len = strlen($name);
$numOfExposedLetters = rand(1, 4);
$encodedName = str_repeat('*', $len);
while ($numOfExposedLetters --) {
$index = rand(2, $len - 1);
$encodedName[$index] = $name[$index];
}
return $encodedName;
}

PHP infinity loop [While]

I am having some problems with PHP.
I used while to sum a number's digits always that it has more than two digits, some how, it gets into an infinity loop.
e.g: 56 = 5 + 6 = 11 = 1+1= 2.
Here is the code:
$somaP = 0;
$numPer = (string)$numPer; //$numPer = number calculated previously
while (strlen($numPer) > 1){
for ($j = 0; $j < strlen($numPer); $j++){
$somaP = $somaP + (int)($numPer[$j]);
}
$numPer = (string) $somaP;
}
Can anyone help me? Guess it is a simple mistake, but I couldn't fix it.
You need to reset the value of $somaP in your while loop.
Currently it continues to increase its value every time through the loop.
Try this:
$numPer = (string)$numPer; //$numPer = number calculated previously
while (strlen($numPer) > 1){
$somaP = 0;
for ($j = 0; $j < strlen($numPer); $j++){
$somaP = $somaP + (int)($numPer[$j]);
}
$numPer = (string) $somaP;
}
Take a look at this line:
$numPer = (string) $somaP;
It seems that the length of $somaP is never lesser (or equal) than 1. So the length of $numPer is never lesser (or equal) than 1.
What are you trying to do?
It's unclear to me.
This for example would add every number in a string together?
E.g "1234" = 1+2+3+4 = 10
$total = 0;
for($i = 0; i < strlen($string); $i++){
$total += $string[$i];
}
echo $total;
This looks cleaner I would say:
$numPer = 56;
while ($numPer > 9){
$numPer = array_sum(str_split($numPer));
}
echo $numPer;
PHP handles all string <> number conversions for you, so no need to do (string) on a number unless really needed.

Generating a new unique alpha numeric string,

I need to generate an alpha numeric string and compare with database values but if the value is present in database then we should generate a different numeric string and again it should be compared with database values....
<?php
$character_array = array_merge(range('a', 'z'), range(0, 9));
$Emp_Id = "";
for($i = 0; $i < 6; $i++) {
$Emp_Id .= $character_array[rand(0, (count($character_array) - 1))];} ?>
which is working fine by creating an alpha numeric code,But as i have mentioned above i need to compare it with database values.
$chkEmp_Id=mysql_fetch_array(mysql_query("SELECT Emp_Id FROM talent_users WHERE Emp_Id='{$Emp_Id}' "));
after comparing with database values if the value is present then it should generate a different "Emp_Id",every time generated employee id should be compared with database values...hope u guys got it na?please help me to fix this...
$pool = array_merge (range ('a', 'z'), range(0, 9));
do
{
$hash = "";
for ($i = 0; $i < $lengthOfHash; ++$i)
{
$hash .= $pool[array_rand ($pool)];
}
while ( mysql_result (mysql_query ('SELECT COUNT(1) FROM talent_users WHERE Emp_Id = ' . $hash), 0) > 0);
But this is a big bottleneck when the table size grows. Since the hash is alpha-numeric, why can't the hash just begin with the userId?
//hash = <a number> + <a letter> + <alpha-numeric chars>
$hash = $userId . generateHash();
Just make sure that the generateHash() function doesn't start with a number, as stated in the comment.
It would then be something like:
$hash = $userId;
$letters = range ('a', 'z');
$pool = array_merge ($letters, range (0, 9));
for ($i = 0; $i < $lengthOfHash; ++$i)
{
$chooseFrom = $pool;
if ( ! $i )
$chooseFrom = $letters;
$hash .= $chooseFrom[array_rand($chooseFrom)];
}

Not sure the loop I need to use every two variables

I have a problem where I have an array $user.
I have $_SESSION['players'] which has the total amount of $user.
I need a function where I can take the user1 and 2 and use them. Then move on to user3 and 4 and use them, and so on.. until I have used all the players. Obviously the total $user[$i] would be players-1.
Anyone have a solution for this?
Thanks
Would this suit your needs? This requires that there be an even number of players to work properly though, unless you stick in a check for odd numbers:
for ($i = 0; $i < $_SESSION['players']; $i += 2) {
$userA = $user[$i];
$userB = $user[$i + 1];
// Do things with $userA and $userB variables...
}
just because you're taught how to use for loops in one way does not mean that you're stuck continuing to use them the way you were taught:
$length = count($users);
$length = $_SESSION['players'];
for ($i = 0; $i < $length; $i += 2)
{
if (!isset($user[$i], $user[$i + 1])) break;
$userOne = $user[$i];
$userTwo = $user[$i+1];
//do stuff
}
I realized that isset wasn't necessary, the for call could be modified more:
$length = $_SESSION['players'];
for ($i = 0; ($i + 1) < $length; $i += 2)
{
$userOne = $user[$i];
$userTwo = $user[$i+1];
//do stuff
}
EDIT to change how the length was calculated:
EDIT to validate that user exists
EDIT to add consolidated version

Categories