Forum Moderators: coopster
First... I'm new to this forum and new to Php/Mysql so probably my code is no good and sure it can be improved... at this moment I have a problem with the code below... is not working at all... maybe I'm doing a conceptual mistake... could you please take a look and let my know where is my error?
- The idea is select a group of records (first select) and
- for each selected record...
- select another group of records (second select)... that in this example happens to be the same table.. but could be different.
- for each record in the second select do some processing
- and continue in the first loop
here is the code:
?php
$periodo = '200912';
/*** mysql hostname ***/
$hostname = 'localhost';
/*** mysql username ***/
$username = 'myusername';
/*** mysql password ***/
$password = 'mypassword';
/*** mysql database name ***/
$dbname = 'thedatabase';
/*** create a new mysqli object with default database***/
$mysqli = @new mysqli($hostname, $username, $password, $dbname);
/* check connection */
if(!mysqli_connect_errno())
{
/*** if we are successful ***/
echo 'Connected Successfully<br />';
/*** SELECT RECORDS TO PROCESS ***/
$sqlone = "SELECT DISTINCT(CrId), CrFullName FROM tbl_one WHERE DATE_FORMAT(FlDate,'%Y%m') =".$periodo;
/*** prepare statement ***/
if($result = $mysqli->query($sqlone))
{
/*** loop over the result set ***/
while ($row = $result->fetch_assoc())
{
/*** echo our table rows ***/
echo '<tr><td>'.
$row['CrId'].'</td><td>'.
$row['CrFullName'].'</td></tr>';
/* FROM HERE*/
/*** Second query ..NOTE: I must pass the $row['CrId'] to this select as a WHERE parameter***/
$sqltwo = "SELECT * FROM tbl_one WHERE DATE_FORMAT(FlDate,'%Y%m') ='".$periodo."' ORDER BY DATE_FORMAT(FlDate,'%Y%m'),CrId,FlAbsDpt";
/*** prepare statement ***/
if($stmt2 = $mysqli->prepare($sqltwo))
{
/*** execute our SQL query ***/
$stmt2->execute();
/*** bind the results ***/
$stmt2->bind_result($fColumn1 , $fColumn2 , $fColumn3);
/*** loop over the result set ***/
while ($stmt2->fetch())
{
/** Make some actions with the records */
/*** echo our table rows ***/
echo '<tr><td>'.
$fColumn1.'</td><td>'.
$fColumn2.'</td><td>'.
$fColumn3.'</td><tr>';
}
}
/* TO HERE */
}
$result->close();
}
/*** close connection ***/
$mysqli->close();
}
else
{
/*** if we are unable to connect ***/
echo 'Unable to connect';
exit();
}
?>