generate get next unique number from mysql ID - php

I need to generate a unique extension for each user (this is separate from the ID but works much the same) the number should be 100 or greater and can be overwritten/custom set. Right now I am taking the next id and if it's less than 100 add 100.
So if the next id is 5 the number would be 105 but it the next id is 105 the number would just be 105. The problem is that because i'm letting the user pick their own extension if a user choose 105 before I want to make it automatically jump to the next number in this case 106. Now if there is 105, 106, and 108 I would want to jump to 107 then jump to 109. Here is the code I'm using to generate the numbers. I think the problem is with my while loop. I'm not sure how to make it keep on checking for unique numbers.
Here is the code, I'm sure I'm overcomplicated things A LOT.
$result = mysql_query("SELECT MAX(id)
FROM USERS");
$row = mysql_fetch_row($result);
$sql_extention = intval($row[0]);
//make sure it's at least 100
$extension = ($sql_extension < 100) ? $sql_extension+100 : $sql_extension;
//check to see if the extention is in use
$qry = "SELECT `extention`
FROM users
WHERE extention = '$extention'";
$result2 = mysql_query($qry);
//if the extention is in use then find the next available one (this isn't currently working)
if($result2) {
//get all results greater or equal to our extention
$qry3 = "SELECT `id`,`extention`
FROM admin_users
WHERE extention >= '$extention'";
$result3 = mysql_query($qry3);
//this loop needs to be rewritten somehow to get the next number by checking if the next number exist if not return that as the extention
$new_extention = $extention+1;
while($extention_data = mysql_fetch_array($result3)) {
if($new_extention != $extention_data['extention']+1) {
$extention = $new_extention;
}
$new_extention++;
}

I came up with this, haven't tested it thoroughly but i think it should return the next available value correctly
SELECT (a.extention + 1) as avail
FROM admin_users a LEFT JOIN admin_users b on ( (a.extention + 1) = b.extention )
WHERE a.extention >= 105 and b.extention is null
ORDER BY avail ASC
LIMIT 1
So if this works as expected you wont need the last few lines of code at all.
Edit:
Revised the query because i realized i was approaching it from the wrong side.

Shoddy attempt at PHP/pseudocode example as per my comment:
//nicer way to get the id of the user you just inserted!
$id = mysql_insert_id();
$sql = "SELECT `extension` FROM users ORDER BY `extension` ASC";
$res = mysql_query($sql);
$i=0;
while($n = mysql_fetch_array($res)){
if($i==0){
$i=$n['extension'];
}
if($i==$n['extension']){
$i++;
} else {
break;
}
}
//No existing users, start at 100
if($i==0){
$i=100;
}
Then use $i as your extension.

Ok, so you need the next available extention higher then a given number, that is not already in the database.
So, you ideally want an array from the database that has all available extentions higher then a given key sorted ascending. Then you loop from the given number increasing by one, until it does not match. You don't mention a max number of extention. I would do it like this:
<?php
$rs = mysql_unbuffered_query("SELECT extention, MAX(extention) FROM admin_users WHERE extention > '$extention' ORDER BY extention ASC");
$unavailable = array();
$max = 0;
while( ($row = mysql_fetch_row($rs)) )
{
$unavailable[] = $row[0];
if( !$max ) $max = $row[1];
}
mysql_free_result($rs);
// Optimization for the quite common case where no more extentions are available
if( count($unavailable) > 0 )
{
while($extention <= $max+1)
{
if( !in_array($extention, $unavailable) )
break;
$extention++;
}
}
else
$extention = $max+1;
// Worst case: $extention now is max + 1 and we looped through almost everything.
?>

Related

pHp Issue: "Waiting for localhost": Maybe infinite loop issue?

I am currently working on an elo based matchmaking system. Basically, I have a database where there are a bunch of users, each having their respective ratings.
Brief explanation of code, it chooses one user at random, and chooses another user if the second user is within +-100 difference-elo of the first user (so grandmasters don't play with bronze players). If there is no one within 100 elo, the difference-elo increments by 50 and checks again.
Problem: I created a test user that is 100 elo more than the other users to test the matchmaking system. The program works except for when the first selected player is my test user. The page never loads and it is stuck on "Waiting for localhost". I assumed this meant one of my while loops was going on infinitely, but after spending sometime editing them and trying to figure out why they might be going on infinitely, the error still persists.
<?php
include('mysql.php');
include('functions.php');
$result2 = executeQuery("SELECT * FROM images ORDER BY score DESC");
function executeQuery($query2){
$connect = mysqli_connect("localhost", "root", "password","database");
$result2 = mysqli_query($connect, $query2);
return $result2;
}
// Get random
$query="SELECT * FROM images ORDER BY RAND() LIMIT 0,1";
$result = #mysqli_query($connection, $query);
//Put random in images array
while($row = mysqli_fetch_object($result)) {
$images[] = (object) $row;
}
//Get elo of random
$elo1 = $images[0]->score;
--------------------------
//Point of Interest Below
--------------------------
//Sort database by DESC. Then go through entire database. If current row's elo is with +- difference (which right now is 100) of elo1's rating, ($elo1-100 < x < $elo1+100), then put into new array with all matchups.
//Get length of array, if length is 0 (meaning no match ups were found), increase difference by 50 and try again.
$potentialMatchup = [];
$diff = 100;
$arrayLength = count($potentialMatchup);
while ($arrayLength == 0){
$diff += 50;
while($row2 = mysqli_fetch_object($result2)){
if(($row2->score > $elo1-$diff) && ($row2->score < $elo1+$diff)){
$potentialMatchup[] = (object) $row2;
}
}
$arrayLength = count($potentialMatchup);
}
-------------------------------
//Get random index between 0 and length of array.
$randomNumber = rand(0, $arrayLength-1);
//Put match up into images array
$images[1] = (object) $potentialMatchup[$randomNumber];
//Get names of images
$nameLeft = $images[0]->filename;
$nameRight = $images[1]->filename;
//If same person, pick another random
while($nameLeft == $nameRight){
$randomNumber = rand(0, $arrayLength-1);
$images[1] = (object) $potentialMatchup[$randomNumber];
$nameRight = $images[1]->filename;
}
// Close the connection
mysqli_close($connection);
$leftName = str_replace("_", " ", $images[0]->filename);
$rightName = str_replace("_", " ", $images[1]->filename);
?>

How do you make sure two rows selected at random are different from one another?

I asked this question yesterday and received an easy solution, but it was about ORDER BY rand(), which I was told was inefficient for large tables. I searched the web and found this a much more efficient method:
Get the total number of rows in the table: $rows
Use mt_rand to set $row1_id as a random number between 1 and the total number of rows: $row1_id = mt_rand(1,$rows)
Use mt_rand again to set $row2_id as a random number between 1 and the total number of rows: $row2_id = mt_rand(1,$rows)
Run queries to select random rows, i.e.
mysqli_query($conn,"SELECT * FROM photos WHERE photo_id=$row1_id")
mysqli_query($conn,"SELECT * FROM photos WHERE photo_id=$row2_id")
However, I need to make sure that $row1_id != $row2_id (the randomly generated numbers must be different from each other). I tried using an if statement but it only lessened the chances of the numbers being the same, but it was still possible.
Any easy solution to this one?
Just generate your second random number inside a loop to make sure it isn't equal to the first one. In all likelihood this loop will only ever execute once.
$num1 = mt_rand(...);
$num2 = 0;
do {
$num2 = mt_rand(...);
} while($num2 == $num1);
// $num1 and $num2 are guaranteed to be different
This method will work for you, so long as your row id's are contiguous, with no gaps
If there are gaps, you'll need to generate new numbers up until both result in database hits. Something like this.
$photo1 = null;
$photo2 = null;
do {
$num = mt_rand(...);
$photo1 = mysql_query(...);
} while(mysqli_num_rows($photo1) == 0);
$photo1 = mysqli_fetch_assoc($photo1);
do {
$num = 0;
do {
$num = mt_rand(...);
} while($num == $photo1['id']);
$photo2 = mysql_query(...);
} while(mysqli_num_rows($photo2) == 0);
$photo2 = mysqli_fetch_assoc($photo2);]
Up to you to compare these methods against the order by rand() options and see which is more performant.
Just a little modification to yesterday's query. Try with this query -
SELECT * FROM photos GROUP BY photo_id ORDER BY rand() LIMIT 2
You have to try where clause and IN function.
SELECT * FROM photos WHERE photo_id IN ($row1_id, $row2_id);

End Foreach after X number of records PHP

I have the following query:
$trabajos = $con->query("SELECT * FROM portafolio ORDER BY RAND() LIMIT 4");
while ($po = $trabajos->fetch_array()) {
$trab[] = $po;
}
This query is working fine, but, it shows only 4 records, but then again in another part of the same page I need the same table information but I don't want it to be limited, say I want 10 instead of only 4. so.
Section Header
Section Slider <-- here only shows 4 items as intended
Using a foreach as follow:
foreach ($trab as $it) {
// My html Code
}
Section Mid Body some info
Section Main Body More info
Section Mid End Footer <-- here I need the same information as in the slider section but with a different layout instead of only 4 items I need 10 items.
For that last item I have to open another query similar as before.
$trabajos = $con->query("SELECT * FROM portafolio ORDER BY RAND() LIMIT 10");
while ($po = $trabajos->fetch_array()) {
$trab[] = $po;
}
So I was thinking is there a way to use:
$trabajos = $con->query("SELECT * FROM portafolio ORDER BY RAND()");
while ($po = $trabajos->fetch_array()) {
$trab[] = $po;
}
That query without the LIMIT and instead Limit the foreach to only fetch the first 4 rows or is there a way to use that query with the LIMIT but instead of 4 or 10, say I use 20, and for the first foreach only display the first 4 and for the second foreach show the rest after the first 4.
The main idea is not have more than 1 query for the same information.
this will exit a loop, but why not query 10 records, build your results, all 10 and then just output 4 of them, there and all 10 over yonder.
$x = 0;
foreach ($trab as $it) {
if($x == 5){
break; //will exit loop as will return.
}
// My html Code
++$x;
}
This will work with any kind of loop, you can also use continue to skip to the next pass of the loop
foreach ($trab as $it) {
if($x < 5){
continue; //code below here wont run until $x >= 5.
}
// My html Code
++$x;
}
i'm not sure to understand exactly what you want, but if you want to divide your array in 2 part. You can use a counter
$trabajos = $con->query("SELECT * FROM portafolio ORDER BY RAND() LIMIT 20");
$i = 0;
$trab2 = new Array();
while ($po = $trabajos->fetch_array()) {
if($i < 4) {
$trab[] = $po;
} else {
$trab2[] = $po;
}
$i++;
}
First, RAND() is horrible since it essentially disables MySQL’s built in caching with each run of the query. But there is no best way for me to advise how to get around that based on your script’s requirements. So just keep that in mind. But here is what I would recommend.
That query without the LIMIT and instead Limit the foreach to only
fetch the first 4 rows or is there a way to use that query with the
LIMIT but instead of 4 or 10, say I use 20, and for the first
foreach only display the first 4 and for the second foreach show
the rest after the first 4.
Yes, just use array_slice like this:
$trabajos = $con->query("SELECT * FROM portafolio ORDER BY RAND() LIMIT 20");
while ($po = $trabajos->fetch_array()) {
$trab[] = $po;
}
$trab_slice_1_size = 4;
$trab_slice_2_size = 10;
$trab_slice_1 = array_slice($trab, 0, $trab_slice_1_size);
$trab_slice_2 = array_slice($trab, $trab_slice_1_size, $trab_slice_2_size);
The nice thing about array_slice is you can cleanly select a start & end point. In my example I set the sizes in variables. Which makes it easier to adjust in your code if you somehow want to dynamically change values.
The easiest way is to use a counter variable:
function fetch_items($limit=10) {
public $trabajos,$trab;
$count=0;
$trabajos = $con->query("SELECT * FROM portafolio ORDER BY RAND()");
while (($po = $trabajos->fetch_array()) && ($count<$limit)) {
$count++;
$trab[] = $po;
}
}
If you call fetch_items() without a parameter, it defaults to 10 or less. If you specify a parameter it loads the array with up to the amount you specify. You could also have the function return the value of $count so you know how many were returned.
try;
$trabajos = $con->query("SELECT * FROM portafolio ORDER BY RAND() LIMIT 10");
$i = 0;
while ($i < 5 && $po = $trabajos->fetch_array()) {
.....
.....
$i++;
}
then, if you want to re-use the data,
mysql_data_seek($trabajos, 0);
$i = 0;
while ($i < 11 && $po = $trabajos->fetch_array()) {
.....
.....
$i++;
}

Reading a field from the code, working with the data, saving the values to new fileds in the row

So here is what I am doing.
Read a row each in for loop. (Because all at once is going to take some resources since I am in a shared hosting.)
2.Get the right field data to a variable.
3.Manipulate the req datas dependant on the extracted field.
4.update the new fields where filed=extracted data.
Bit of addition, I am adding the current position to a file, so that the script can continue from there next time it is run.
Problem : It doesnt seem to work. The counter.txt gets values like 3-4, but it simply resides there. my db has like 1000k rows.
my code :
require ("dbconnect.php");
header("refresh:29;url=process.php"); // so it doesnt ever end. I cant use max_execution_time here for some reason.
$count = mysql_query("SELECT COUNT(*) FROM collection ");
$data = mysql_fetch_array($count);
$count = $data[0];
echo $count;
$countfile = fopen("counter.txt", "r");
$counter = fgets($countfile);
echo fgets($countfile);
while (fgets($countfile) <= $count)
{
$i = fgets($countfile);
$takeword = mysql_query("SELECT word FROM collection WHERE id='$i'") or die();
$wd = mysql_fetch_array($takeword);
$data = $wd[0];
$d1 = hash($algorith='md2',$data);
$d2 = hash($algorith='md4',$data);
$write = mysql_query("UPDATE collection SET md2='$d1', md4='$d2' WHERE id='$i'") or die(mysql_error());
//opens, empties and write the new pointer to the file. closes, and open the file in readmode for the next read at the loop.
$counts = fopen("counter.txt", "w+");
fwrite($counts, $counter + 1);
fclose($counts);
$countfile = fopen("counter.txt", "r");
}
Any help would be appreciated :) Looking for code optimization and killing the error. Suggestions would do.:)
Alright I'd do something like this (sorry about the delayed response, I kept forgetting)
<?php
//main execution
$sql = mysql_connect(...);
if (!$sql)
die ("No database connection");
if (!mysql_select_db(..., $sql))
die ("Database does not exist in this schema");
//Run the query for this iteration.
processQuery();
//---
function getQueryOffset($file)
{
$offset = 0; //default offset
if (file_exists($file)) //check if the counter file exists
{
$contents = file_get_contents($file); //get the contents of the counter
if ($contents !== FALSE && is_numeric($contents)) //check if an appropriate counter value
$offset = intval($contents);
}
return $offset;
}
function processQuery()
{
$table = "collection"; //table to update
$counter = "counter.txt"; //where to look for the last execution's offset.
$maxrows = 10000; //update 10,000 rows each time this file is loaded.
$sql = $GLOBALS['sql'];
//calculate the number of rows in the table
$qCount = mysql_query("SELECT COUNT(*) max FROM $table", $sql);
$aCount = mysql_fetch_assoc($qCount);
mysql_free_result($qCount);
$max = $aCount["max"];
$offset = getQueryOffset($counter); //calculate the offset (or a default 0)
if ($offset < $max) //if offet >= max, we're done.
{
$qUpdate = mysql_query("SELECT word, id FROM $table LIMIT $offset, $maxrows", $sql); //get the next "maxrows" rows from the table.
if ($qUpdate)
{
$assoc = NULL;
while (($assoc = mysql_fetch_assoc($qUpdate)) != NULL)
{
$md4 = hash("md4", $assoc["word"]); //calculate the hashes
$md2 = hash("md2", $assoc["word"]);
$id = $assoc["id"]; //id the row
mysql_query("UPDATE $table SET md2='$md2', md4='$md4' WHERE id=$id", $sql); //update the table columns
}
//update the offset in the counter file.
file_put_contents($counter, ($offset + mysql_num_rows($qUpdate)));
mysql_free_result($qUpdate);
}
}
}
mysql_close($sql);
?>
1 issue that I am seeing here:
Check your update query - that seems to be wrong. According to me, it should be "SET md2='$d1' AND md4='$d2'"
Another issue that I am not sure about:
I am unsure if md2 and md4 are valid names of hashing algorithms
A better way of doing this:
1. Dont write to file!
2. Create an additional column in your SQL by the name 'status', default value to 0. On update, change that value to 1.
3. Search for rows to edit based on query "SELECT word FROM collection WHERE status=0 limit 0,1"
4. OR if the columns md2 and md4 are empty in the original table, query could also be "SELECT word FROM collection WHERE md2='' and md4='' limit 0,1"
Hope this helps.

PHP/MySQL Count() Issue

I am trying to create a class registration system for a client that utilizes PHP and MySQL. I have the database and table all set up and that part works just fine, however, the client has requested that upon registration, if there are 3 or fewer students enrolled to warn that the class may not run.
I'm trying to use the count() function as well as passing a dynamic variable from a cookie, set from the registration PHP script. However, I've hit a roadblock. I can't seem to get the count() function to actually count the rows. My select statement is below. Any help would be greatly appreciated.
$class = $_COOKIE["class"];
$min_check = "SELECT class_list, COUNT(class_list) as count
FROM T_Student WHERE class_list = '$class'
GROUP BY class_list
HAVING count < 20";
$result = mysql_query($min_check);
$count = mysql_num_rows($result);
if ($count < 4)
{
echo "IF THERE ARE 3 OR FEWER PEOPLE SIGNED UP FOR THIS CLASS, IT MAY NOT RUN.\n";
echo "THERE ARE CURRENTLY " . $count . " PEOPLE SIGNED UP.\n";
}
else if ($count > 4)
{
echo "There are currently " . $count . " people signed up for this class.";
}
?>
Your SQL query is returning a list of the class_list values, along with a count of each specific instance, where there are less than 20 people registered.
$count = mysql_num_rows($result);
...is getting the number of records returned in the resultset, not the alias count value, which is why you aren't seeing the output you expect. You need to read into your resultset to get the value:
while ($row = mysql_fetch_assoc($result)) {
$count = $row['count'];
if($count < 4) { ... }
}
The count that you want is returned in the row of the query. the mysql_num_rows will count the rows returned, which is not what you want. Use this instead.
$result = mysql_query($min_check);
$count = mysql_fetch_row($result);
$count = $count[0];
On a first glance, the HAVING count < 20 is unnecessary.
You use the MySQL-count-function, but never retrieve it's value!? Use:
$firstRow = mysql_fetch_row($result);
$count = $firstRow[1]; // 1 indicates the second column (0 being the first)
I don't recommend using known MySQL identifiers like count. It's confusing.
$class = mysql_real_escape_string($_COOKIE["class"]);
$min_check = "SELECT class_list, COUNT(class_list) as mycount
FROM T_Student WHERE class_list = '$class'
GROUP BY class_list
HAVING mycount < 20";
Don't forget to escape the contents of that cookie!
The error is that count is a reserved word. You need to either surround it in backticks `count` or even better, use a different moniker. It's not an error per se, but it's just too confusing.
Next up, you are not actually retrieving the mycount result from the database. I suggest using code something like this:
$result = mysql_query($min_check);
while( $row = mysql_fetch_assoc($result) ) {
$people_count = $row['mycount'];
if ($people_count < 4) { echo "this" }
else { echo "that" }
}

Categories