Jump to content

Call variable outside a loop


Recommended Posts

Hello so I have a simple code here that will check if a random number already exists in the database and will generate a new one:
$rand = mt_rand(100000, 999999);
$sid = array(
   ":sponsorID" => $rand
$accounts = $db->select("accounts", "sponsorID = :sponsorID", $sid)
while(count($accounts) > 0) {
$newNum = mt_rand(100000, 999999);


What I wanted to do is to echo out $newNum. When I echo it out I get undefined index of that variable. How can I solve this?


Link to comment
Share on other sites

This code is worthless. It is the most incomplete attempt at anything I've ever seen.


1 - where is this 'echo' you speak of?

2 - where is the db connection

3 - what is 'select' - a php function or your own function which you failed to show us?

4 - you have what looks like the beginnings (the VERY beginnings) of a query statement with a substitute parm in it, but you have no prepare() statement to follow (unless of course your 'select' is your own function)

5 - your while loop is laughable. while the number of elements in your array is > 0? It WILL always be > 0

6 - in your loop all you do is pick another random number. Why? You're not even looking at the contents of your query(?) results!

7 - you say when you echo out newnum you get an error. How about SHOWING us the line that gives you that error.


If you wrote this code yourself, you have to do some learning/teaching/reading to get to a place where you understand how to program and how to use the PHP language. If you copied this code from somewhere - do not copy anything from that place ever again in your life!


Good luck if you choose to continue in this line.


Tough on you? You bet because you need to be told how far from your goal you are.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.