1

I am working on my web site and I am trying to implement a feature that works like this:

Admin checks a checkbox on a record to show that payment is received. The values are stored in an array called $paymentr which is imploded and updates the MySQL database.

Now here's where the tricky part (for me, anyway) comes in:

After it does this, the code checks how many rows have been affected and then sends out ONE email to another person that should list all the information for the records based on the IDs stored in the array.

For some reason I've been having a bear of a time trying to figure out how to do this. I had a foreach loop running right before the email code which ran something like this (I have simplified the query as it is much longer and complex, but I HAVE tested it in phpmyadmin) --

foreach ($paymentr as $v) {

$query = "SELECT transactions.id, transactions.refid, transactions.affid FROM transactions WHERE transactions.id = '$v'";

$result = mysql_query($query) or die("Query Failed: ".mysql_errno()." - ".mysql_error()."<BR>\n$query<BR>\n");
$trans = mysql_fetch_array($result, MYSQL_ASSOC);

$transactions .= '<br>User ID:'.$trans['id'].' -- '.$trans['refid'].' -- '.$trans['affid'].'';

}

Then that $transactions variable would be sent out in the email code.

But it didn't work, sadly. Does anyone have any ideas? I feel like I am just missing one crucial chunk or idea... anything would be helpful. Maybe array_map? I've never used that though. Thank you ever so much :)

3
  • Did you already check what var_dump($trans) prints? Commented Jan 14, 2011 at 1:18
  • Hi again, thanks for your thoughtful answers and comments! I did try print_r ($paymentr); and it is outputting the right variables. I also tried var_dump($trans); as well as print_r on that... nothing is in there! :/ I tried the mysql query in phpmyadmin and it worked, pulling the right record. Commented Jan 16, 2011 at 3:23
  • I'm also getting "Invalid argument supplied for foreach() in .... on line 50" problems in my error file... could this be because I imploded $paymentr, it's no longer an array? Commented Jan 16, 2011 at 3:26

2 Answers 2

1

I tried your code and didn't notice anything wrong. To help debug, you could try adding some "print_r" and "echo" code for a better sense of what's happending

Also, you mentioned that your array is imploded, which to my understanding returns a string with the values of the array in the form [value][separator][value]..., is it still an array when the 'foreach' starts? Are you finding that $transactions is empty?

print_r ($paymentr);  // <-----  ADDED FOR DEBUG

foreach ($paymentr as $v) {

$query = "SELECT id, refid, affid FROM transactions WHERE id = '$v'";

echo $query . "<br/>";  // <-----  ADDED FOR DEBUG,  (XHTML <br/> closed tag)

$result = mysql_query($query) or die("Query Failed: ".mysql_errno()." - ".mysql_error()."<BR>\n$query<BR>\n");
$trans = mysql_fetch_array($result, MYSQL_ASSOC);

$transactions .= '<br>User ID:'.$trans['id'].' -- '.$trans['refid'].' -- '.$trans['affid'].'';    
}

Or, you could do this, which hits the db less:

foreach ($paymentr as $v) { $in_clause .= "'".$v."',"; }
$in_clause = trim($in_clause, ",");
$query = "SELECT id, refid, affid FROM transactions WHERE id IN ($in_clause)";

$result = mysql_query($query) or die("Query Failed: ".mysql_errno()." - ".mysql_error()."<BR>\n$query<BR>\n");
while ( $trans = mysql_fetch_array ( $result, MYSQL_ASSOC ))    
{
  $transactions .= '<br>User ID:'.$trans['id'].' -- '.$trans['refid'].' -- '.$trans['affid'].'';
}
Sign up to request clarification or add additional context in comments.

3 Comments

Hi again, thanks for your thoughtful answers and comments! I did try print_r ($paymentr); and it is outputting the right variables. I also tried var_dump($trans); as well as print_r on that... nothing is in there! :/ I tried the mysql query in phpmyadmin and it worked, pulling the right record...
OH - I just had a revelation. I'm overwriting the variable name with the implode. Hmm. Let me give this a shot by renaming the implode to something else..
Yes, that was the trick. Implode was writing over my array and giving foreach() a problem. Thank you for bringing that to my attention!
1

I don't have the php-mysql manual in front of me, however shouldn't you be using a call like mysql_fetch_assoc rather than mysql_fetch_array .

When you use mysql_fetch_array you specify the fields as $trans[0], $trans['1] rather than as strings eg. $trans['id']

-- sorry you are right, I always use the mysql_fetch_assoc call rather than the flag. How did you go with adding debug statements, if you print the sql statement copy and paste it into a query window - to check it runs correctly. Do other DB calls work as expected, possibly you are not getting any results back.

2 Comments

Hi John, from what I understand you can use mysql_fetch_array the same way as mysql_fetch_assoc by specifying "MYSQL_ASSOC" after the $result variable... if you wrote MYSQL_NUM it would then do the array numerically. correct me if I'm wrong though? :)
mysql_fetch_array($con) defaults to using MYSQL_BOTH if the type isn't specified, so the array will contain both the regular numerically indexed 0,1,2,3,etc... entries AND the associative 'id','name', etc... version.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.