Get data for mysql records via array?
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 开发者_StackOverflow社区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 :)
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'].'';
}
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.
精彩评论