0

Hello i m new to MySQL and PHP under training please tell me where i m wrong

My problem occurs in ad01 and ad03 they are echo Advertise Here but ad02 echo properly

<?php require_once('Connections/localhost.php'); ?>
<?php
mysql_select_db($database_localhost, $localhost);
$query_advtDisplay = "SELECT * FROM advt";
$advtDisplay = mysql_query($query_advtDisplay, $localhost) or die(mysql_error());
$row_advtDisplay = mysql_fetch_assoc($advtDisplay);
$totalRows_advtDisplay = mysql_num_rows($advtDisplay);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Download Links</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="wrapper">
    <div class="ad01">
        <?php if ($row_advtDisplay['advt-no']=='ad01')
            {
            echo $row_advtDisplay['advt-content'];
            }
            else{ echo "Advertise Here";}
            ?>
    </div> 
    <div class="middlebox">  
        <div class="ad02">
            <?php if ($row_advtDisplay['advt-no']=='ad02')
            {
            echo $row_advtDisplay['advt-content'];
            }
            else{ echo "Advertise Here";}
            ?>
        </div>
        <div class="linkbox">
            <p>Download Links</p>
            <ul>
                <li><a href="#">Link 01</a></li>
                <li><a href="#">Link 02</a></li>
                <li><a href="#">Link 03</a></li>
                <li><a href="#">Link 04</a></li>
                <li><a href="#">Link 05</a></li>
            </ul>
            <ul>
                <li><a href="#">Link 06</a></li>
                <li><a href="#">Link 07</a></li>
                <li><a href="#">Link 08</a></li>
                <li><a href="#">Link 09</a></li>
                <li><a href="#">Link 10</a></li>
            </ul>
            <ul>
                <li><a href="#" target="_blank">Link 11</a></li>
                <li><a href="#" target="_blank">Link 12</a></li>
                <li><a href="#" target="_blank">Link 13</a></li>
                <li><a href="#" target="_blank">Link 14</a></li>
                <li><a href="#" target="_blank">Link 15</a></li>
            </ul>
        </div>
        <div class="passwordbox">
            <p>RAR Password</p>
        </div> 
    </div>
    <div class="ad03"><?php if ($row_advtDisplay['advt-no']=='ad03')
            {
            echo $row_advtDisplay['advt-content'];
            }
            else{ echo "Advertise Here";}
            ?></div>  
    <div class="clear"></div> 
</div>
</body>
</html>
<?php
mysql_free_result($advtDisplay);
?>

only ad02 shows properly not ad01 and ad03 No Problem with Table only Coding Problem sorry for bad english

3
  • I would have to assume, then... that $row_advtDisplay['advt-no']='ad02' so ad01 and ad03 hit the else statement. Commented Oct 10, 2014 at 23:15
  • 1
    You are fetching only the first row from the result set. Please, read about looping throw a result set... here for example: php.net/manual/en/mysqli-result.fetch-assoc.php Commented Oct 10, 2014 at 23:15
  • wait... what is the PHP error? Commented Oct 10, 2014 at 23:32

1 Answer 1

2

mysql_fetch_assoc will only return one row ever... so you need to loop through to see all your data.

$just_one_row = mysql_fetch_assoc($advtDisplay); 

// how to loop through all the rows 
while ($row = mysql_fetch_assoc($advtDisplay)) {
    echo $row["advt-no"];
    echo $row["advt-content"]; 
}

// maybe do something like... 
$mydata = array(); 
while ($row = mysql_fetch_assoc($advtDisplay)) {
    $mydata[ $row['advt-no'] ] = $row['advt-content'];
}
<!-- and then -->
<div class="ad01">
    <?php 
     if ( isset($mydata['ad01']) && !empty($mydata['ad01']) ) {
        echo $mydata['ad01'];    
     } else { echo "Advertise Here"; }  ?>
</div> 

and I have to say it... you should really be using PDO or mysqli.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.