I got stuck on selecting fields from a table using SQL and PHP.
I have a table named invitations with those fields:
- senderid
- receiverid1
- receiverid2
- …
- receiverid10
I have different rows with the same senderid and want to select the receiverid corresponding to it.
Some receiversid may be null so I filter only non null value.
Here is my code:
$d1 = mysql_connect($dbhost,$dbuser ,$dbpass);
mysql_select_db($dbname, $d1);
/* fetching receivers */
$q1 = mysql_query("select receiverid1,receiverid2,receiverid3,receiverid4,
receiverid5,receiverid6,receiverid7,receiverid8,receiverid9,receiverid10
from invitations
where senderid = '528495538' ", $d1);
while($row = mysql_fetch_array($q1))
{
$totalreceivers = count(array_filter($row));
$receivers[] = array_filter($row);
}
echo '<br>';
echo "the receivers";
echo '<br>';
print_r($receivers);
echo '<br>';
echo $totalreceivers;
With the above sender id, on the table I have 2 rows but when I execute the code I get only one:
the receivers Array ( [0] => 1743650643 [receiverid1] => 1743650643 ) 2
Any kind of help will be appreciated, thank you.
Make
$recieversinto an array, and$totalreceiversinto an int starting at 0, and declare them before the loop. You are reassigning them on each loop instead of adding to them:Note that it is more efficient to call
array_filter()once and assign it to a variable than it is to call it twice for each operation. Also, especially for the operation above, usemysql_fetch_assoc()instead ofmysql_fetch_array().EDIT See if this is more like what you want (slightly fixed)…