TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   PHP MySQL Table not loading sometimes (http://www.talkphp.com/general/4122-php-mysql-table-not-loading-sometimes.html)

amitdgr 04-10-2009 08:00 AM

PHP MySQL Table not loading sometimes
 
Hi all,

I have been facing this problem from quite sometime. I have to fetch data from a mysql db and display it as a table. This is my code


PHP Code:

    $con=mysql_connect($hostName,$user,$password);

   if(!$con)
    {
        echo "connection failed";
    }
    mysql_select_db($dbName,$con);
    $sql="select * from market_details";

<table cellpadding="0" cellspacing="0" border="0">
<thead>
<tr>
<th>Market Code</th>
<th>Market Name</th>
</tr>
</thead>
<tbody>
<?php
    
while($row=mysql_fetch_array($result,$con))
    {
?>
     <tr class="gradeA">
     <td class="center" ><?php echo $row['market_code']; ?></td>
     <td class="center" ><?php echo $row['market_name']; ?></td>
     </tr>
<?php
     
}        
?>
</tbody>
</table>

The problem is, sometimes the table contents appear blank. It shows data when i refresh the page.

I suspect the problem is in mysql_fetch_array($result,$con), maybe it is not fetching the data sometimes , thus it is not showing it.

Where have I gone wrong ?

sjaq 04-10-2009 09:55 AM

PHP Code:

<?php 

    $con
=mysql_connect($hostName,$user,$password); 
    
    if(!
$con) { 
        die 
"connection failed"
    } 
    
    
mysql_select_db($dbName,$con); 
    
$sql="select * from market_details";
    
$result mysql_query($sql);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
    </head>
    <body>
        <p>
        </p>
        <table cellpadding="0" cellspacing="0" border="0">
            <thead>
                <tr>
                    <th>
                        Market Code
                    </th>
                    <th>
                        Market Name
                    </th>
                </tr>
            </thead>
            <tbody>
                <?php 
                    
while($row=mysql_fetch_array($result,$con)) 
                    { 
                
?>
                <tr class="gradeA">
                    <td class="center">
                        <?php echo $row['market_code']; ?>
                    </td>
                    <td class="center">
                        <?php echo $row['market_name']; ?>
                    </td>
                </tr><?php 
                     
}         
                
?>
            </tbody>
        </table>
    </body>
</html>


amitdgr 04-10-2009 10:05 AM

Hi sjaq,

I have DOCTYPE declaration in my original code. It doesn't work even with it.

I just posted the code snippet where I was having the problem.

Thanks

allworknoplay 04-10-2009 11:28 AM

Quote:

Originally Posted by amitdgr (Post 22985)
Hi sjaq,

I have DOCTYPE declaration in my original code. It doesn't work even with it.

I just posted the code snippet where I was having the problem.

Thanks

But our code shows no mysql_query function. Or was that just a type that you forgot to show?

You're looking for the $result resource var, which is fine, but you don't have a query creating it...

tony 04-10-2009 10:02 PM

like allworknoplay said, there is no query being made, so mysql_fetch_array() can't get the results from the array.

another thing, but I am not sure in this, but if you are accessing the the results with the names of the filds, I think you need to use the mysql_fetch_assoc() instead. But i am not sure about that one.

allworknoplay 04-10-2009 11:19 PM

Quote:

Originally Posted by tony (Post 23003)
like allworknoplay said, there is no query being made, so mysql_fetch_array() can't get the results from the array.

another thing, but I am not sure in this, but if you are accessing the the results with the names of the filds, I think you need to use the mysql_fetch_assoc() instead. But i am not sure about that one.

You can use either. But "assoc" is better since you're just returning the named fields.

If he's looking for the key/value pair, then yeah he would use array...

amitdgr 04-17-2009 09:42 AM

Hey guys,

I used mysql_fetch_object() ... works fine now :)

Thanks for the inputs ... they helped me think in the right direction


All times are GMT. The time now is 04:53 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0