Prtingin Mysql rows

They have: 7 posts

Joined: Jun 2004

I have mySQL database with many tables. I'd like to print all titles of table and rows in them to screen with php.

I know I can print rows under some titles like this:

SELECT * from mydb WHERE title = 'something';

But now I have to manually do this for every title. How should I print titles AND rows under it? I know I should count titles somehow and then create loop for them but I don't know actually how.

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

Use a while loop.

<?php
$sql
= \"SELECT title FROM mydb\";
$qry = mysql_query($sql);
$numrows = mysql_count_rows();

echo \"There are
$numrows titles in this database<br />They are as follows:<br /><br />\";

while (
$row = mysql_fetch_array($qry)) {
    echo \"
$row['title']<br />\";
}
?>

See mysql.com for details on how to pull more selective information and sort it using the SELECT statement, and php.net on how to use while loops and others to get that information displayed.

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

Can you provide a small sample of how you would like the output displayed and the table setup?

-Greg

They have: 7 posts

Joined: Jun 2004

Thanks for you're answers,
But what if I have something like this in my table:

<?php

titles
| titles 2 | titles 3 ....
----------------------------------------
row       row        row
row       row        row
?>

Now I'd like to print those all titles to web page and print row under each. Count of titles is different in every of my tables... so I'd could be nice if I could print any of them with same script. Now I have to manually define how many titles there are and then query rows for each

They have: 7 posts

Joined: Jun 2004

I mean I'd like to view some table in database in "excel-style".
It could help a lot if somebody told me how to count those titles (I mean the fields in database).

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

oh, you want to print off your sql tables entirely?

column names and fields?

I already showed you how to count the rows, only you don't seem to mean "row" when you say row... You want to count every single cell? I think it would be advisable for you to go to mysql.com and learn the terminology so you can express your question in way that we can all understand so we don't confuse you further!

Anyway, if you know the column names, you can do what I told you earlier, only you'll identify them as such:

<?php
$sql
= \"SELECT * FROM mydb\";
$qry = mysql_query($sql);
$numrows = mysql_count_rows();

echo \"There are
$numrows records in this database<br />They are as follows:<br /><br />\";

echo<<<OPENTABLE
<table width=\"100%\" id=\"listing\">
    <tr>
        <th>Column Name</th>
        <th>Column Name</th>
        <th>Column Name</th>
        <th>Column Name</th>
    </tr>
OPENTABLE;
while (
$row = mysql_fetch_array($qry)) {
    echo<<<ROW
<tr>
    <td>
$row['title1']</td>
    <td>
$row['title2']</td>
    <td>
$row['title3']</td>
    <td>
$row['title4']</td>
</tr>
ROW;

echo \"</table>\";
?>

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.