# Cycling through mysql database

**URL:** <https://www.phphelp.com/t/cycling-through-mysql-database/30252>\
**Category:** General PHP Help\
**Created:** [October 22, 2019, 4:36pm UTC](https://www.phphelp.com/t/cycling-through-mysql-database/30252 "2019-10-22T16:36:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![N00bie](https://www.phphelp.com/letter_avatar_proxy/v4/letter/n/bc79bd/32.png) [@N00bie](https://www.phphelp.com/u/N00bie)\
**Post date:** [October 22, 2019, 4:36pm UTC](https://www.phphelp.com/t/cycling-through-mysql-database/30252/1 "2019-10-22T16:36:33Z")

</div>

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);
	}
```

---

<div class="post-metadata">

**Author:** ![phdr](https://www.phphelp.com/letter_avatar_proxy/v4/letter/p/91b2a8/32.png) [@phdr](https://www.phphelp.com/u/phdr)\
**Post date:** [October 22, 2019, 5:09pm UTC](https://www.phphelp.com/t/cycling-through-mysql-database/30252/2 "2019-10-22T17:09:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![benanamen](https://www.phphelp.com/user_avatar/www.phphelp.com/benanamen/32/287_2.png) [@benanamen](https://www.phphelp.com/u/benanamen)\
**Post date:** [October 22, 2019, 5:16pm UTC](https://www.phphelp.com/t/cycling-through-mysql-database/30252/3 "2019-10-22T17:16:52Z")

</div>

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](https://www.php.net/manual/en/book.csprng.php)

---

<div class="post-metadata">

**Author:** ![N00bie](https://www.phphelp.com/letter_avatar_proxy/v4/letter/n/bc79bd/32.png) [@N00bie](https://www.phphelp.com/u/N00bie)\
**Post date:** [October 22, 2019, 5:50pm UTC](https://www.phphelp.com/t/cycling-through-mysql-database/30252/4 "2019-10-22T17:50:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![N00bie](https://www.phphelp.com/letter_avatar_proxy/v4/letter/n/bc79bd/32.png) [@N00bie](https://www.phphelp.com/u/N00bie)\
**Post date:** [October 22, 2019, 5:54pm UTC](https://www.phphelp.com/t/cycling-through-mysql-database/30252/5 "2019-10-22T17:54:13Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![N00bie](https://www.phphelp.com/letter_avatar_proxy/v4/letter/n/bc79bd/32.png) [@N00bie](https://www.phphelp.com/u/N00bie)\
**Post date:** [October 22, 2019, 6:54pm UTC](https://www.phphelp.com/t/cycling-through-mysql-database/30252/6 "2019-10-22T18:54:34Z")

</div>

Figured it out - thanx
