Jump to content

Output Data from database


eleven0

Recommended Posts

I'm currently changing my site from HTML to PHP. I'm making a simple CMS.

 

include 'config.php';
include 'opendb.php';
// Make a MySQL Connection
$query = "SELECT * FROM cms"; 

$result = mysql_query($query) or die(mysql_error());


while($row = mysql_fetch_array($result)){
$title= $row['first'];
$cont= $row['last'];
echo "<br />";
}

include 'closedb.php';
echo "<td valign='top'><div class='tborder22'><b> $title </b> </div>";
echo $cont;
}

 

I got a title and an article.

Above outputs the article under its title. But it only outputs the last entry. How can I show all entries in that form?

 

Link to comment
https://forums.phpfreaks.com/topic/91500-output-data-from-database/
Share on other sites

the reason your script only echoes the last entry from what i can tell is your logic with the while loop.

 

Try Echoing during the while loop, otherwise you will need to store $title and $cont as arrays, so that each time the while() loops it wont overwrite whatever is stored already (which is why only the last entry survives).

 

Try This:

include 'config.php';
include 'opendb.php';
// Make a MySQL Connection
$query = "SELECT * FROM cms"; 

$result = mysql_query($query) or die(mysql_error());


$row = mysql_fetch_array($result); // Store entire array
$x = count($row); // Count Rows
For($i=0;$i<$x;$i++){
    $title[$i] = $row[$i]['first']; // $row[row_number][Field_Name_or_Number]
    $cont[$i] = $row[$i]['last'];
}

include 'closedb.php';

// Do not need in this example, but for larger scripts you may want to recalculate $x to be safe
For($i=0;$i<$x;$i++){
    echo "<td valign='top'><div class='tborder22'><b> $title[$i] </b> </div>";
    echo $cont[$i];
    echo "<br />";
}

 

I replaced the While loop with a For loop for ease of assigning arrays. you can do it with either really but this is typically how i would code this personally.

 

I noticed in the while loop you were only echoing a <br> ? the only reason i can see is to count the <br>'s for debugging purposes.

 

Also i noticed an extra "}" end bracket, from the code visible in your script you dont need it.

 

Any question please ask, hope this helps

hmm no output would suggest an empty result (your query matched 0 rows, or the table is empty)

 

try this and tell me what it says:

<?php
include 'config.php';
include 'opendb.php';
// Make a MySQL Connection
$query = "SELECT * FROM cms";

$result = mysql_query($query) or die("Mysql ERROR: ".mysql_error());


$row = mysql_fetch_array($result); // Store entire array
$x = count($row); // Count Rows
echo("<br>Row Count: $x<br>");  // debug echo
For($i=0;$i<$x;$i++){
    $title[$i] = $row[$i]['first']; // $row[row_number][Field_Name_or_Number]
    $cont[$i] = $row[$i]['last'];
}
print_r($title);print_r($cont);  // Debug Echo

include 'closedb.php';

// Do not need in this example, but for larger scripts you may want to recalculate $x to be safe
For($i=0;$i<$x;$i++){
    echo "<div class='tborder22'><b> $title[$i] </b> </div>";
    echo $cont[$i];
    echo "<br />";
}
?>

 

 

i have added debug lines to try and solve this mystery

Row Count: 6

Array ( [0] => 2 [1] => s [2] => a [3] => [4] => [5] => ) Array ( [0] => 2 [1] => s [2] => a [3] => [4] => [5] => )

2 (id number of my first entry, I removed the first one so it's 2.)

2

s (First letter of my first entry's title)

s

a (First letter of my first entry's context)

a

 

Then I get 3 blank title lines.

 

Here is the page source of above;

 

<br>Row Count: 6<br>Array
(
    [0] => 2
    [1] => s
    [2] => a
    [3] => 
    [4] => 
    [5] => 
)
Array
(
    [0] => 2
    [1] => s
    [2] => a
    [3] => 
    [4] => 
    [5] => 
)
<div class='tborder22'><b> 2 </b> </div>2<br /><div class='tborder22'><b> s </b> </div>s<br /><div class='tborder22'><b> a </b> </div>a<br /><div class='tborder22'><b>  </b> </div><br /><div class='tborder22'><b>  </b> </div><br /><div class='tborder22'><b>  </b> </div><br /></td>

</tr>

May have to mess with your tables, but you should get the idea.

 


include 'config.php';
include 'opendb.php';
// Make a MySQL Connection
$query = "SELECT * FROM cms"; 

$result = mysql_query($query) or die(mysql_error());

echo "<td valign='top'><div class='tborder22'><b> $title </b> </div>";

while($row = mysql_fetch_array($result)){
echo "<tr><td>{$row['first']}</td>";
echo "<td>{$row['last']}</td>";
echo "</tr>>";
}

include 'closedb.php';
}

sry for not replying, went to bed :P.

 

did revraz solution fix your problem? (i believe i accounted for a different result set in my code).

-----

To answer your last question all you have to do is change your query:

 

$query = "SELECT * FROM cms WHERE `fieldname`='value'";

ie:

$query = "SELECT * FROM cms WHERE `id`='2'";

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.