Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 19th, 2005, 08:22 AM
Registered User
 
Join Date: Jan 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default simple database problem?

I face a problem with retrieving data from my database.

I would like to output the same piece of data twice in the document, but it wouldnt allow me to do so. The second spot where i wanna output the data just appears blank.

this is the code i have used

<?
include("connection/connection.php");
connect_init();

    $result1=mysql_query("SELECT * FROM tbllevel1 ");
    echo mysql_error();
    $row = mysql_fetch_assoc($result1);

?>
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<table width="100%" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td>
    <?php do { ?>
    <? echo $row['Level1'];?><br>
    <?php } while ($row = mysql_fetch_assoc($result1)); ?>

    </td>
    <td>
    <?php do { ?>
    <? echo $row['Level1'];?><br>
    <?php } while ($row = mysql_fetch_assoc($result1)); ?>

    </td>
  </tr>
</table>
</body>
</html>

does anybody know why that isnt possible or whether i just make a simple mistake

Cheers J

 
Old April 19th, 2005, 09:42 AM
Friend of Wrox
 
Join Date: Dec 2003
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hi J,
OK, what you're code is doing is:

- running the query on your database
 ( $result1=mysql_query("SELECT * FROM tbllevel1 "); )

- reading the first row of the data into an associative array:
 ( $row = mysql_fetch_assoc($result1); )

- running a loop, that will start with the second row from the data you got back, reading each row into an associative array and printing the ['Level1'] field:
 ( <?php do { ?>
    <? echo $row['Level1'];?><br>
    <?php } while ($row = mysql_fetch_assoc($result1)); ?> )

- trying to run the same loop again, but you've now reached the end of your data, so the initial while condition would never evaluate as true, so it's executed once with no data.

 BTW, You don't need the <?php for each statement, just use it to start and finish blocks of php.

Try something like:
<html>
 <head></head>
 <body>
 <?php
    include("connection/connection.php");
    connect_init();

    $result1=mysql_query("SELECT * FROM tbllevel1 ");
    echo mysql_error();
    // it's better to use a while statment, than a do-while,
    // just in case you don't get any data back
    while($row = mysql_fetch_assoc($result1)) {
         // this 'pushes' your field onto an array
         $my_array[]=$row['Level1'];
    }
    // at this point the array $my_array contains each value of
    //the field 'Level1' from each row in the table
 ?>
<table width="100%">
<?php
// this foreach loop is going to go through each element in our array,
// and print 1 table row, with 2 cells, each containing the datat from
// the field "Level1"
foreach ($my_array as $_) {
   echo "<tr> <td>";
   echo $_;
   echo "</td> <td>";
   echo $_;
   echo "</td> </tr>";
}
?>
</table>

Bear in mind that this is untested and I'm really a perl programmer, but it'll hopefully get u started :-)

Charlie


--
Don't Stand on your head - you'll get footprints in your hair
                                           http://charlieharvey.org.uk
                                              http://charlieharvey.com
 
Old April 19th, 2005, 10:03 PM
Registered User
 
Join Date: Jan 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks charlie

tried it out and i seems to work. really appreciate your help
and thanks also for the comments to my piece of code. really learnt something.

Cheers J

 
Old April 20th, 2005, 10:32 AM
Friend of Wrox
 
Join Date: Dec 2003
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Cool, no probs,

Charlie

--
Don't Stand on your head - you'll get footprints in your hair
                                           http://charlieharvey.org.uk
                                              http://charlieharvey.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Probably a very simple problem kerepuki Classic ASP Basics 3 January 14th, 2006 10:25 PM
Simple problem rsa3des Classic ASP Basics 4 August 8th, 2005 06:14 PM
Settings to a simple access to a SQL database tedhill Classic ASP Databases 0 July 21st, 2005 04:25 AM
Save simple record via web form t sql database kbarsi VB Databases Basics 1 January 30th, 2005 11:29 AM
Simple Database Question Nick.Net VB.NET 2002/2003 Basics 2 December 8th, 2003 05:43 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.