TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 04-10-2009, 08:00 AM   #1 (permalink)
The Wanderer
 
amitdgr's Avatar
 
Join Date: Mar 2009
Posts: 5
Thanks: 0
amitdgr is on a distinguished road
Default 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 ?
amitdgr is offline  
Reply With Quote
Old 04-10-2009, 09:55 AM   #2 (permalink)
The Acquainted
 
sjaq's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 113
Thanks: 11
sjaq is on a distinguished road
Default

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>
sjaq is offline  
Reply With Quote
Old 04-10-2009, 10:05 AM   #3 (permalink)
The Wanderer
 
amitdgr's Avatar
 
Join Date: Mar 2009
Posts: 5
Thanks: 0
amitdgr is on a distinguished road
Default

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
amitdgr is offline  
Reply With Quote
Old 04-10-2009, 11:28 AM   #4 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by amitdgr View Post
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...
allworknoplay is offline  
Reply With Quote
Old 04-10-2009, 10:02 PM   #5 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

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.
tony is offline  
Reply With Quote
Old 04-10-2009, 11:19 PM   #6 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by tony View Post
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...
allworknoplay is offline  
Reply With Quote
Old 04-17-2009, 09:42 AM   #7 (permalink)
The Wanderer
 
amitdgr's Avatar
 
Join Date: Mar 2009
Posts: 5
Thanks: 0
amitdgr is on a distinguished road
Default

Hey guys,

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

Thanks for the inputs ... they helped me think in the right direction
amitdgr is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help on PHP MYSQL Forms Input deesudesu Absolute Beginners 1 03-20-2009 10:50 AM
Databse structure Village Idiot TalkPHP Developer Team 3 01-16-2009 10:31 PM
This project has begun! Village Idiot TalkPHP Developer Team 40 01-01-2009 04:29 AM
Securing your MySQL Queries with Sprintf Wildhoney General 26 03-18-2008 06:52 PM
Error in connecting to MySQL via PHP EyeDentify MySQL & Databases 0 01-03-2008 01:06 PM


All times are GMT. The time now is 02:49 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design