开发者

PHP Problem with MySQL query. The function is only running for the first mysql result

I tried to search for something similar in the web but no results. What I am trying to do is simply taking the results from the DATABASE and then run some functions for EACH result. We have two kinds of functions. The first function is when the row "Type" is = F , the second one when the row "Type" is = T. The problem that I am having with this code is that it runs the functions ONLY for the first mySQL result. But I have more results in the same time, and the functions should run for EACH mySQL result and not only for the first one.

I do not know if I need a foreach开发者_如何学Go or whatever. I do not know anything about arrays and php loops.

Thank you.

include_once("../dbconnection.php");
date_default_timezone_set('UTC');
$TimeZone ="UTC";
$todaydate = date('Y-m-d') ."\n";
$time_utc=mktime(date('G'),date('i'),date('s'));
$NowisTime=date('G:i:s',$time_utc);
$MembID =(int)$_COOKIE['Loggedin'];
$DB = new DBConfig();
$DB -> config();
$DB -> conn();
$queryMAIN="SELECT * FROM TableTuit WHERE TimeZone ='".$TimeZone."' AND Date ='".$todaydate."' ORDER BY ID ASC";
$result=mysql_query($queryMAIN) or die("Errore select TableT: ".mysql_error());
$tot=mysql_num_rows($result);

while($ris=mysql_fetch_array($result)){
    $text=$ris['Tuitting'];
    $account=$ris['IDAccount'];
    $memberID=$ris['memberID'];
    $type=$ris['Type'];
    $id=$ris['ID'];
    $time=$ris['Time'];

    if($time <= $NowisTime){
    if($type=="F") //if the row type is = F, then do the things below
    {
    $queryF ="SELECT * FROM `TableF` WHERE `memberID`='$MembID' AND `ID`='$account'";
    $result=mysql_query($queryF) or die("Errore select f: ".mysql_error());
    $count = mysql_num_rows($result); 
    if ($count > 0) {
    $row = mysql_fetch_assoc($result);
        DO FUNCTION // Should call the function that requires the above selected values from $queryF. Should Run this function for every mysql result given by $queryMAIN where row "type" is = F
        } 
} 
}

    if($type=="T") //if the row type is = T, then do the things below
    {
    $queryT = $queryF ="SELECT * FROM `TableT` WHERE `memberID`='$MembID' AND `ID`='$account'";
    $result=mysql_query($queryT) or die("Errore select $queryT: ".mysql_error());
    $count = mysql_num_rows($result); 
    if ($count == 0)
    $Isvalid = false;
    else
    {   
    $Isvalid = true;
    $row = mysql_fetch_assoc($result);

    }

    if($Isvalid){

    DO THIS FUNCTION // Should call the function that requires the above selected values from $queryT. Should Run this function for every mysql result given by $queryMAIN where row "type" is = T

    }
    }
}
}//END OF MYSQL WHILE OF $queryMAIN


You are using $result for the outer Query ($result=mysql_query($queryMAIN); and the Query inside the while loop $result=mysql_query($queryF); - I believe you do not want to mix these?

Right now you process the first row from TableTuit, then overwrite the $result with a row from TableF or TableT. In the next loop, the following columns will not be found in the array (unless they are also in these two tables, of course):

$text=$ris['Tuitting'];
$account=$ris['IDAccount'];
$memberID=$ris['memberID'];
$type=$ris['Type'];
$id=$ris['ID'];
$time=$ris['Time'];


You are loading the results into an array, try using this in your while loop instead:

while($ris=mysql_fetch_assoc($result)){
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜