Forum Moderators: coopster

Message Too Old, No Replies

How use php array in a mysql query?

         

Markinho

5:22 pm on Jun 16, 2011 (gmt 0)

10+ Year Member



Hi! How I can use in a mysql query values of an array, I've tried to Do it but I can't

// the values of the $array are:
2011-05-25 08:00:00,2011-05-26 08:00:00,2011-05-27 08:00:00,2011-05-28 08:00:00

now I want to use this values in a query to know the difference of minutes


<?
foreach($array as $value){
$query2 = "SELECT No_Empl,Entr,
TIMESTAMPDIFF(MINUTE,'$value',Entr) as DifEntr
FROM Table
WHERE weekY = 21 AND No_Empl = 15";

$result2 = mysql_query($query2) or die (mysql_error());
$num_results = mysql_num_rows($result2);

while($row2 = mysql_fetch_assoc($result2)){

echo "\t\t<tr class=\"row2$r\">
<td>".$row2['No_Empleado']."</td>
<td>".$row2['Entr']."</td>
<td>".$row2['DifEntr']."</td>
</tr>\n";

if($r%2==0)++$r;else--$r;
}

}

echo "\t</table>\n";

?>


the code above doesn't work as I want because print many tables, and I wish print only 1 table using the correct values of the array
Thanks
Best Regards

Markinho

5:29 pm on Jun 16, 2011 (gmt 0)

10+ Year Member



FYI
Sorry I Don't translated all the names fields of my Table
the names that I use on the $row2 are:
No_Empl,Entr and DifEntr

Demaestro

5:33 pm on Jun 16, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Try taking out the close table tag out of the loop

<?
foreach($array as $value){
$query2 = "SELECT No_Empl,Entr,
TIMESTAMPDIFF(MINUTE,'$value',Entr) as DifEntr
FROM Table
WHERE weekY = 21 AND No_Empl = 15";

$result2 = mysql_query($query2) or die (mysql_error());
$num_results = mysql_num_rows($result2);

while($row2 = mysql_fetch_assoc($result2)){

echo "\t\t<tr class=\"row2$r\">
<td>".$row2['No_Empleado']."</td>
<td>".$row2['Entr']."</td>
<td>".$row2['DifEntr']."</td>
</tr>\n";

if($r%2==0)++$r;else--$r;
}

}



?>

</table>

Markinho

5:53 pm on Jun 16, 2011 (gmt 0)

10+ Year Member



Thanks Demaestro
but still continues to print many tables, if I quit the "while" loop out of the foreach loop, It's print 1 table, but the values &#8203;&#8203;of the matrix there are not correct, only uses the last value of the matrix

Demaestro

7:11 pm on Jun 17, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Where is the open table tag?

Markinho

7:53 pm on Jun 17, 2011 (gmt 0)

10+ Year Member



thanks but it was not in the table

the opening tag of the table I have it up, ok I'll paste some more code, sorry

the solution was add this in the query:"where day(Entr) = day($value)"
<?
echo "<table>";
echo "<tr>
<td>Num:</td>
<td>Entr:</td>
<td>DifEntr:</td>
</tr>\n";

foreach($array as $value){
$query2 = " SELECT No_Empl,Entr,
TIMESTAMPDIFF(MINUTE,'$value',Entr) as DifEntr
FROM Table
WHERE weekY = 21 AND No_Empl = 15 and where day(Entr) = day($value)";

$result2 = mysql_query($query2) or die (mysql_error());

while($row2 = mysql_fetch_assoc($result2)){

echo "\t\t<tr class=\"row2$r\">
<td>".$row2['No_Empl']."</td>
<td>".$row2['Entr']."</td>
<td>".$row2['DifEntr']."</td>
</tr>\n";

if($r%2==0)++$r;else--$r;
}


}
echo "\t</table>\n";
?>

and it worked!
thanks

Demaestro

8:28 pm on Jun 17, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Glad you got it