-2

Can I use payment_status = rand('pending','success','failed','refunded') for the below query or can anyone help me what’s my mistake with the below query.

for ($i=0;$i<100000;$i++)   
{ 
$payment_status = rand('pending','success','failed','refunded');    
$sql = 'INSERT INTO `aa_kkkk` (`id`, `user_id`, `biz_id`, `filing_year`, `filing_month`, `final_return`, `consent_disclosure`, `is_third_party_designee`, `amended_month`, `earliest_date`, `latest_date`, `tax_year_end_month`, `address_change`, `form_type`, `submission_id`, `payment_status`, `transaction_id`, `active`, `xml_submitted`, `date_user_submitted`, `date_xml_sent`, `date_last_ack_attempt`, `date_acknowledged`, `created_date`, `modified_date`, `next_submission_date`, `irs_approved`, `ack_received`, `sch1_received`, `sch1_path`, `user_completed`, `consent_to_submit`, `error_code`, `error_description`, `error_type`, `modifiedby_admin`, `data_masked`, `form_pdf_path`) VALUES
(Null, ':user_id', ':biz_id', ':filing_year', "c2aaf5544415889e720f042b04551c97'.$i.'", "c9c396be60e066ae92b978c08a3fca0a'.$i.'", "32b20aa703b1128d448321b83fe57d70'.$i.'", "0", "32b20aa703b1128d448321b83fe57d70'.$i.'", "32b20aa703b1128d448321b83fe57d70'.$i.'", "32b20aa703b1128d448321b83fe57d70'.$i.'", "32b20aa703b1128d448321b83fe57d70'.$i.'", "32b20aa703b1128d448321b83fe57d70'.$i.'",  "84843320140520033138'.$i.'", ':payment_status', "", ':transaction_id', ':active', "2014-02-21 10:31:35", "2014-02-21 03:31:42", "2014-02-21 03:32:00", "2014-02-21 03:32:00", "2014-02-21 03:11:15", "2014-04-18 11:12:39", NULL, ':irs_approved', ':ack_received', ':sch1_received', "84843320140520033138.pdf'.$i.'", 1, 1, "", "", "", 0, 0, "pf2290_1_84843320140520033138.pdf'.$i.'")';
Mohit Bumb
  • 2,466
  • 5
  • 33
  • 52
kanth
  • 7
  • 3
  • http://www.php.net/manual/en/function.rand.php – PeeHaa May 08 '14 at 12:22
  • `int rand ( int $min , int $max )` no, no you can't. I suggest editing the question to better reflect what you want to do, rather than ask for help with the (flawed) solution you've chosen. Also, there's absolutely no mysql in the question - how you use `$payment_status` isn't relevant to "the problem". – AD7six May 08 '14 at 12:22

4 Answers4

4
echo rand('pending','success','failed','refunded');

Warning: rand() expects exactly 2 parameters, 4 given

Documentation: rand()

So, NO, you can't. Not like that.

This WILL work:

$values = array('pending','success','failed','refunded');
$payment_status = $values[rand(0, sizeof($values)-1)];

Or use array_rand() (gotta love PHP... a function for everything...):

$values = array('pending','success','failed','refunded');
$payment_status = $values[array_rand($values)];

And for fun: you can also get creative and use shuffle:

$values = array('pending','success','failed','refunded');
shuffle($values);
$payment_status = $values[0];

Though that isn't anywhere near as efficient as the 2 options above it since it will shuffle the array first and then return the first element in it instead of just picking a random element from the list and returning that.

RobIII
  • 8,488
  • 2
  • 43
  • 93
4
$rand = $array[array_rand($array = ['pending','success','failed','refunded'])];

Or:

$array = ['pending','success','failed','refunded'];
$rand  = $array[rand(0, count($array)-1)];

Or ...

Mohit Bumb
  • 2,466
  • 5
  • 33
  • 52
1

You can also do this.

$input = array('pending','success','failed','refunded'); 
echo $input[array_rand($input)];

I prefer this method because by using this you don't have to specify the size of the array. Which might be unknown in some cases.

Jay Bhatt
  • 5,601
  • 5
  • 40
  • 62
  • `I prefer this method because by using this you don't have to specify the size of the array` -> Isn't that what [sizeof()](http://nl3.php.net/manual/en/function.sizeof.php) or [count()](http://nl3.php.net/manual/en/function.count.php) is for? :-) – RobIII May 08 '14 at 12:30
  • @RobIII I also prefer less lines of code. Like you said "PHP... a function for everything..." Then why not use one with everything rather then mixing multiple inbuilt functions? – Jay Bhatt May 08 '14 at 12:31
  • Coming from other platforms and only occasionally using PHP I tend to stay away from 'vague' functions like array_rand (and all those other "(sometimes) helpful but nowhere to be found in any other language"-functions). That's just my habit... – RobIII May 08 '14 at 12:36
  • @RobIII Agree. In such situations I normally develop my own array_rand() function using what you suggested sizeof(). Thanks for your opinion. – Jay Bhatt May 08 '14 at 12:40
0

you can use the following

$k = array_rand($array);
$v = $array[$k];

see documentation here

enjoy :)

Anri
  • 1,706
  • 2
  • 17
  • 36