Cycling through mysql database

This is driving me nuts. Code used to work but then I did something (probably stupid) and now can’t figure out why it wont work.

Trying to add a unique code (accesskey) to each row of a column. All I get is the first row and then it dies. Any help would be appreciated.

	require_once ('./admin/connect.php');
	$db = mysqli_connect($db_hostname,$db_username,$db_password,"xxxxx_members");
	$result = mysqli_query($db,"SELECT id FROM memberlist WHERE (accesskey ='')"); 
	while ($row = mysqli_fetch_array($result)) {
	extract($row);
	$length = 10;
	$randomString = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
	echo $row['lname']. " $id - $randomString<br>";
	$query = "UPDATE memberlist SET accesskey = '$randomString' WHERE (id = '$id')";	
	$result = mysqli_query($db,$query);  
	unset($randomString);
	}

Is seems like you post one thread a year (skipped 2018) with the same fundamental problem in it.

You are reusing the $result variable inside of the loop, so after the 1st pass through the loop it is no longer the result from the select query. It’s it result from the UPDATE query, which is either a boolen true or false

Your “unique code” is far from it and will most assuredly have collisions. You need to use CSPRNG.

https://www.php.net/manual/en/book.csprng.php

Thank you for the response.
I only post about once a year because I only play with php about once a year - like the username says Noobie. My question probably has the same fundamental problem in it because I never seem to solve the entire problem.

Removing the $result in the loop is a help but it brought me back to my original problem before I messed it up further:

the echo diplays what I want (id and unique sequence for each id) but the database has the same value for all rows, i.e., the first randonString for everyone. That is why I tried the unset which didnt work.

I know my “unique” sequence is not great but it a small number generated and a one time use.

echo output:
1 - 9zKu3raXgL
2 - 9NdV20I4QE
3 - J7iCt6LZzk
4 - qGu2ejlYRa
5 - DWkrQqx49f
etc

Database:
1 - 9zKu3raXgL
2 - 9zKu3raXgL
3 - 9zKu3raXgL
4 - 9zKu3raXgL
etc

Figured it out - thanx

Sponsor our Newsletter | Privacy Policy | Terms of Service