put ids into array from mysql query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #16
    @pradeep: that’s not very efficient (though it works). less overhead is used when working with prepared statements:
    - get all IDs with first query (1st query result array)
    - make a prepared statement for the second query
    - execute/fetch it for each of the IDs from the first query (result array)

    Comment

    • pradeepkr13
      New Member
      • Aug 2010
      • 43

      #17
      Agree.

      Even more efficient is what I gave before this.
      Get all Ids with first SQL into array.
      Create second query and use all ids from first in this query. Store into multidimensiona l array2.

      Hence only Two DB querires.

      Now, use loop to get things from array.

      Comment

      • wizardry
        New Member
        • Jan 2009
        • 201

        #18
        thanks for your responses. I will post both my method and your method latter.

        Comment

        • wizardry
          New Member
          • Jan 2009
          • 201

          #19
          ok i was able to get it working, however its not setting the $str_ids in this loop.

          i've echo $str_ids after the - comma string and the comma is not being removed.

          here is the output:
          Code:
          10,11,12,5,6,7,8,1,2
          any insight is helpful, thanks in advance for your help!


          here is my code:

          Code:
          <?php 
          $all_ids = array();
          $str_ids = "";
          
          // first parent query
          $maxRows_sourceType = 10;
          $pageNum_sourceType = 0;
          if (isset($_GET['pageNum_sourceType'])) {
            $pageNum_sourceType = $_GET['pageNum_sourceType'];
          }
          $startRow_sourceType = $pageNum_sourceType * $maxRows_sourceType;
          
          mysql_select_db($database_Comments, $Comments);
          $query_sourceType = "SELECT c.Name, h.Path, h.Default, h.Id as hId, j.Id as Id, j.type, j.Dates, k.comment FROM aswebinfo as c right join asmanyalbums as e on ( c.UIdFk=e.UserId ) right join asalbums as f on ( e.AlbumId=f.Id ) right join astitle as g on ( f.Id=g.AlbumId ) right join asdata as h on ( g.Id=TitleId ) right join asstatusupdate as j on ( c.UIdFk=j.UIdFk) right join asstatusdata as k on (k.SFk=j.Id) WHERE f.Default='Y' and h.Default='Y' and c.UIdFk = any (select FriendId from asfriends where UIdFk0=1) order by Dates desc";
          $query_limit_sourceType = sprintf("%s LIMIT %d, %d", $query_sourceType, $startRow_sourceType, $maxRows_sourceType);
          $sourceType = mysql_query($query_limit_sourceType, $Comments) or die(mysql_error());
          $row_sourceType = mysql_fetch_assoc($sourceType);
          
          if (isset($_GET['totalRows_sourceType'])) {
            $totalRows_sourceType = $_GET['totalRows_sourceType'];
          } else {
            $all_sourceType = mysql_query($query_sourceType);
            $totalRows_sourceType = mysql_num_rows($all_sourceType);
          }
          $totalPages_sourceType = ceil($totalRows_sourceType/$maxRows_sourceType)-1;
          
          // add the array
          while ($row_source = mysql_fetch_assoc($sourceType)) {
          	
          	$all_ids[] = $row_source['Id'];
          	
          	$str_ids .= $row_source['Id'].',';
          	
           }
          	
          		// remove the array
          	 $str_ids = (substr($str_ids,-1) == ',') ? substr($str_ids, 0, -1) : $str_ids;
          	
          //second child query
          	$maxRows_sourceComments = 10;
          $pageNum_sourceComments = 0;
          if (isset($_GET['pageNum_sourceComments'])) {
            $pageNum_sourceComments = $_GET['pageNum_sourceComments'];
          }
          $startRow_sourceComments = $pageNum_sourceComments * $maxRows_sourceComments;
          
          mysql_select_db($database_Comments, $Comments);
          $query_sourceComments = "select a.memo, a.date as Date, b.SFk, c.Name, g.Path  from ascomments as a  right join asmanystatusupdate as b  on a.Id=b.CFk, aswebinfo as c right join asmanyalbums as d on ( c.UIdFk=d.UserId ) right join asalbums as e on ( d.AlbumId=e.Id ) right join astitle as f on ( e.Id=f.AlbumId ) right join asdata as g on ( f.Id=g.TitleId ) where c.UIdFk=any(select friendid from asfriends where UIdFk0='1') and e.Default='Y' and g.Default='Y' and b.SFk in ($str_ids) order by date desc";
          $query_limit_sourceComments = sprintf("%s LIMIT %d, %d", $query_sourceComments, $startRow_sourceComments, $maxRows_sourceComments);
          $sourceComments = mysql_query($query_limit_sourceComments, $Comments) or die(mysql_error());
          $row_sourceComments = mysql_fetch_assoc($sourceComments);
          
          if (isset($_GET['totalRows_sourceComments'])) {
            $totalRows_sourceComments = $_GET['totalRows_sourceComments'];
          } else {
            $all_sourceComments = mysql_query($query_sourceComments);
            $totalRows_sourceComments = mysql_num_rows($all_sourceComments);
          }
          $totalPages_sourceComments = ceil($totalRows_sourceComments/$maxRows_sourceComments)-1;
          
          
          
          $resultComments = @mysql_query($query_sourceComments, $Comments);
          $numComments = @mysql_num_rows($resultComments);
          
          
          $result = @mysql_query($query_sourceType, $Comments);
          
          $num = @mysql_num_rows($result);
          
          // column count for parent
          	$thumbcols = 1;
          	
          		// column count for parent query
          	$thumbrows = 1+ round($num / $thumbcols);
          			
          			// column count for child query	$thumbrowsComments = 1+ round($numComments/$thumbcols);
          			
          			// header
          			print '<br />';
          			print '<table align="center" width="500" border="2" cellpadding="0" cellspacing="0">';
          			
          			if (!empty($num)) {
          				print '<tr><td colspan="3" align="center"><strong>Returned Num of Record Sets: ' .$num. ' and comments count' .$numComments. '</strong></td></tr>';	
          				
          			}
          			
          	// table layout for parent query		function display_table() {
          				
          // global variables
          global $num, $result, $thumbrows, $thumbcols, $resultComments, $numComments, $thumbrowsComments ;
          		
          // row count for parent
           for ($r=1; $r<=$thumbrows; $r++) {
          	 
          	 // print table row
          	 	print '<tr>';
          		
          //format the columns
          for ($c=1; $c<=$thumbcols; $c++) {
          						
          print '<td align="center" valign="top">';
          						
          	$row = @mysql_fetch_array($result);
          						
          	$Id = $row['Id'];
          	$Path = $row['Path'];
          	$Name = $row['Name'];
          	$Type = $row['Type'];
                  $Dates = $row['Dates'];
          	$Comment = $row['Comment'];
          	
          // test if not empty show record sets			
          	if (!empty($Id)) {
          							
          							// print output from parent query
          							
          									echo "$Name";
          									echo "<br />";
          									echo "&nbsp;";
          							echo '<img src="' ."$Path". '" height="120" width="120" />'; 
          									echo "&nbsp;";
          									echo "<br />";
          									
          									// grab type dates comment and format for there column
          									
          									echo '<td valign="top" align="center">'; 
          									echo "$Type"; echo '&nbsp;&nbsp;'; echo "$Dates"; echo '<br />';echo '<br />';echo '<br />';
          									echo "$Comment"; echo '</td>';
          									echo '<td>'; echo "$Id"; echo '</td>';
          								    echo '<td>'; echo "$str_ids"; echo '</td>';
          			} // closing the $id loop
          						
          							// row count for second query
          for($i=1;$i<=$thumbrowsComments;$i++){   						// print new row
          				print '<tr>';
          				
          // column count for second query
          		for ($g=1; $g<=$thumbcols; $g++) {
          							print '<td align="center" valign="top">';
          							
          							$com = @mysql_fetch_array($resultComments);
          							
          							$Memo = $com['memo'];
          							$Dates0 = $com['Date'];
          							$SFk = $com['SFk'];
          							$Name0 = $com['Name'];
          							$Path0 = $com['Path'];
          							
          											
          	if (!empty($Memo)) {
          echo "$numComments";
          											
          							echo "$Name0";
          							echo "<br />";
          							echo "&nbsp;";
          							echo '<img src="' ."$Path0". '" height="120" width="120" />';
          							echo "&nbsp;";
          							echo "<br />";
          							
          							// grab type comments and format the column
          									
          									echo '<td valign="top" align="center">';
          									echo "$Dates0";
          									echo "<br />";
          									echo "$Memo";
          									echo '</td>';
          									echo '<td>'; echo "$SFk"; echo '</td>';
          							} // closing the $sfk loop
          
          												else {
          														print '&nbsp;&nbsp;';
          													}
          							print '</td>'; //closing the table data
          						} //closing the rows
          							print '</tr>';
          			 } // closing the outter loop
          			//} //closing fk id loop
          							print '</td>'; //closing the table data
          				} //closing the rows
          							print '</tr>';	
          			} // closing the outter loop
          			
          	} // closing the main loop
          			
          				// call the main table
          			display_table() ;
          				print '</table>';
          											
          						
          ?>

          Comment

          Working...