Jump to content

PHP/SQL list help


FrostedFlakes

Recommended Posts

I have a problem with my php code; I'm trying to list links for downloading. Problem is that there are 3 different types that need to be echo-ed in their own table column. So far this is what I have; This will list only about 1/2 of the sql entries.

 

<?php
$rs1 = @mysql_connect( "******", "******", "******" );
$rs1 = @mysql_select_db( "******" );

$rs2 = @mysql_connect( "******", "******", "******" );
$rs2 = @mysql_select_db( "******" );

$rs3 = @mysql_connect( "******", "******", "******" );
$rs3 = @mysql_select_db( "******" );

$sql1 = "SELECT * FROM `downloads` WHERE (typen='1') AND (id>'0') LIMIT 0 , 100";
$rs1 = @mysql_query( $sql1 );

$sql2 = "SELECT * FROM `downloads` WHERE (typen='2') AND (id>'0') LIMIT 0 , 100";
$rs2 = @mysql_query( $sql2 );

$sql3 = "SELECT * FROM `downloads` WHERE (typen='3') AND (id>'0') LIMIT 0 , 100";
$rs3 = @mysql_query( $sql3 );

while( $row1 = mysql_fetch_array( $rs1 ) and $row2 = mysql_fetch_array( $rs2 ) and $row3 = mysql_fetch_array( $rs3 ) )
{
?>
<tr valign="center" align="center">
  <td valign="center" align="center"><font face="Arial" size="2">
  <a href="<?php echo( $row1['url'] ); ?>"><?php echo( $row1['name'] ); ?></a>  <?php echo( $row1['comment'] ); ?><br>
  </font>
  </td>
  <td valign="center" align="center"><font face="Arial" size="2">
  <a href="<?php echo( $row2['url'] ); ?>"><?php echo( $row2['name'] ); ?></a>  <?php echo( $row2['comment'] ); ?><br>
  </font>
  </td>
  <td valign="center" align="center"><font face="Arial" size="2">
  <a href="<?php echo( $row3['url'] ); ?>"><?php echo( $row3['name'] ); ?></a>  <?php echo( $row3['comment'] ); ?><br>
  </font>
  </td>
</tr>
<?php } ?>

 

Please don't laugh at my barbaric ways. I've been doing PHP and MySQL by myself using trial and error.

Link to comment
Share on other sites

The problem is your while statement.

<?php
while( $row1 = mysql_fetch_array( $rs1 ) and $row2 = mysql_fetch_array( $rs2 ) and $row3 = mysql_fetch_array( $rs3 ) )
?>

Your while loop will stop as soon as any of of the mysql_fetch_array returns false. Say there are 10 rows in $rs1 and 100 in $rs3, the while loop will exit as soon as $row1 = mysql_fetch_array($rs1) returns false after the 10th rows.

 

Link to comment
Share on other sites

EDIT: DAMN SPACE BAR, try this:

 

<?php
$rs1 = @mysql_connect( "******", "******", "******" );
$rs1 = @mysql_select_db( "******" );

$rs2 = @mysql_connect( "******", "******", "******" );
$rs2 = @mysql_select_db( "******" );

$rs3 = @mysql_connect( "******", "******", "******" );
$rs3 = @mysql_select_db( "******" );

$sql1 = "SELECT * FROM `downloads` WHERE (typen='1') AND (id>'0') LIMIT 0 , 100";
$rs1 = @mysql_query( $sql1 );

$sql2 = "SELECT * FROM `downloads` WHERE (typen='2') AND (id>'0') LIMIT 0 , 100";
$rs2 = @mysql_query( $sql2 );

$sql3 = "SELECT * FROM `downloads` WHERE (typen='3') AND (id>'0') LIMIT 0 , 100";
$rs3 = @mysql_query( $sql3 );

while($row = mysql_fetch_array( $rs1 ))
{
        $rows1[] = $row;
}
while($row = mysql_fetch_array( $rs2 ))
{
        $rows2[] = $row;
}

while($row = mysql_fetch_array( $rs3 ))
{
        $rows3[] = $row;
}

for ($i=0;$i<count($rows1);$i++) {
     echo '<tr valign="center" align="center">';
     if (isset($rows1[$i])) {
  ?> <td valign="center" align="center"><font face="Arial" size="2">
  <a href="<?php echo( $rows1[$i]['url'] ); ?>"><?php echo( $rows1[$i]['name'] ); ?></a>  <?php echo( $rows1[$i]['comment'] ); ?><br>
  </font>
  </td>
<?php
     }

    if (isset($rows2[$i])) {
     ?>
  <td valign="center" align="center"><font face="Arial" size="2">
  <a href="<?php echo( $rows2[$i]['url'] ); ?>"><?php echo( $rows2[$i]['name'] ); ?></a>  <?php echo( $rows2[$i]['comment'] ); ?><br>
  </font>
  </td>
<?php
   }

   if (isset($rows3[$i])) {
?>
  <td valign="center" align="center"><font face="Arial" size="2">
  <a href="<?php echo( $rows3[$i]['url'] ); ?>"><?php echo( $rows3[$i]['name'] ); ?></a>  <?php echo( $rows3[$i]['comment'] ); ?><br>
  </font>
  </td>
<?php
    }
   echo ' </tr>';
}
?>

Link to comment
Share on other sites

First, why are you connecting the the database 3 time? You only need one connection. Change

<?php
$rs1 = @mysql_connect( "******", "******", "******" );
$rs1 = @mysql_select_db( "******" );

$rs2 = @mysql_connect( "******", "******", "******" );
$rs2 = @mysql_select_db( "******" );

$rs3 = @mysql_connect( "******", "******", "******" );
$rs3 = @mysql_select_db( "******" );
?>

to

<?php
$dbcon = mysql_connect( "******", "******", "******" ) or die("Couldn't connect, error: " . mysql_error());
$dbsel = mysql_select_db( "******" ) or die("Couldn't select database, error: " . mysql_error());
?>

 

Next, you can combine the three queries into one.

 

Change:

<?php
$sql1 = "SELECT * FROM `downloads` WHERE (typen='1') AND (id>'0') LIMIT 0 , 100";
$rs1 = @mysql_query( $sql1 );

$sql2 = "SELECT * FROM `downloads` WHERE (typen='2') AND (id>'0') LIMIT 0 , 100";
$rs2 = @mysql_query( $sql2 );

$sql3 = "SELECT * FROM `downloads` WHERE (typen='3') AND (id>'0') LIMIT 0 , 100";
$rs3 = @mysql_query( $sql3 );
?>

to

<?php
$query = "SELECT * FROM `downloads` WHERE id > 0 and typen in ('1','2','3')";
$rs = mysql_query($query) or die("Problem with the query <pre>$query</pre><br>" . mysql_error());
?>

 

Now you can write your loop as:

<?php
while ($rw = mysql_fetch_assoc($rs)) {
    $tmp = array();
    $tmp[rw['typen']] = '<a href="' . $rw['url']  . '"><' . $rw['name'] . '</a>  ' . $rw['comment'] . '<br>';
}
echo '<tr valign="center" align="center">';
for ($i=1;$i<4;$i++) {
    echo '<td valign="center" align="center"><font face="Arial" size="2">';
    echo $tmp[$i];
    echo '</font>';
    echo '</td>';
}
echo '</tr>';
?>

 

I think this will give you what you want. Untested, my have syntax errors and/or logic errors.

 

Ken

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.