Jump to content
shades

Prepare statement query not executing

Recommended Posts

Hi, 

 

Can someone point out the mistake ? I tested the query in MySQL workbench and it works fine but in the prepare statement it is not executing and also I get no error.

if(isset( $_POST['name'], $_POST['samplecount'], $_POST['decks'])) 
{  

  $name = $_POST['name'];
  $sample = $_POST['samplecount'];
  $decks = $_POST['decks'];
  $rows = ($sample/$decks);

  for ($i=0; $i<$decks; $i++){

    for ($j=1; $j<=$rows; $j++) {

         $stmt = $dbconnect -> prepare(" INSERT INTO decks (id, dnumber, vtext) 

        ( SELECT :id, :dnumber, 

        (SELECT r.vtext FROM rsample r WHERE r.groupname = :groupname  AND r.vtext 

          NOT IN (SELECT vtext FROM decks) 

        )
         ORDER BY rand() LIMIT 1
        )
    "); 

  $stmt -> bindValue(':id', $i);
  $stmt -> bindValue(':dnumber', $j);
  $stmt -> bindValue(':groupname',$name)
  $stmt -> execute ();

    }
  }
}

In my code I use name = "test" value of samplecount = 8 and decks = 2. So $rows will be 4.

My Requirement: Out of the 8 texts. I want to get the data like below and I want the texts to be in random order.

DeckId     DeckNumber   Text
D1             1         CC
D1             2         AA
D1             3         ZZ
D1             4         JJ
D2             1         FF
D2             2         BB
D2             3         CC
D2             4         WW
 

Any help will be greatly appreciated.

Thanks

Share this post


Link to post
Share on other sites

Prepare the statement once, before the loop.

 

Inside the loop you bind the new values and execute.

Share this post


Link to post
Share on other sites

Running loops and doing a select statement each time [especially with rand()] is a bad idea. here is what I think is a better approach.

 

1. Run ONE SELECT query to get all of the vtext values from the rsample table for the groupname

2. Populate those results into an array

3. User array_shuffle() to randomize the results of the array

4. Run the loops for $i & $j like you have now, but create a simple INSERT statement  like this

INSERT INTO decks
    (id, dnumber, vtext) 
VALUES (:id, :dnumber, :vtext)

5. Define the vtext value on each loop using array_shift() which will remove and return the first value in the array

$stmt -> bindValue(':vtext', array_shit($vtextValuesAry));
Edited by Psycho

Share this post


Link to post
Share on other sites

 

Running loops and doing a select statement each time [especially with rand()] is a bad idea. here is what I think is a better approach.

 

1. Run ONE SELECT query to get all of the vtext values from the rsample table for the groupname

2. Populate those results into an array

3. User array_shuffle() to randomize the results of the array

4. Run the loops for $i & $j like you have now, but create a simple INSERT statement  like this

INSERT INTO decks
    (id, dnumber, vtext) 
VALUES (:id, :dnumber, :vtext)

5. Define the vtext value on each loop using array_shift() which will remove and return the first value in the array

$stmt -> bindValue(':vtext', array_shit($vtextValuesAry));

 

Thank you. The solution is perfect.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

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.