FewFlyBy Posted August 2, 2015 Share Posted August 2, 2015 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? Quote Link to comment https://forums.phpfreaks.com/topic/297582-call-variable-outside-a-loop/ Share on other sites More sharing options...
ginerjm Posted August 2, 2015 Share Posted August 2, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/297582-call-variable-outside-a-loop/#findComment-1517902 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.