/    Sign up×
Community /Pin to ProfileBookmark

make a table of my MySQL Data

I am trying to make a table of my MySQL Data. My table, test, has 5 columns: idt, datetime, col1, col2 and col3 with many rows of data.

[code=php]</php
$conn = mysql_connect(‘server.com’, ‘username’, ‘password’);
mysql_select_db(‘database’);
$sqli = “SELECT * FROM `test`;”
$resulti = mysql_query($sqli, $conn);
$index = mysql_fetch_array($resulti, MYSQL_ASSOC);
foreach ($g in count($index[‘idt’])){
$_SESSION[‘idt’] = $indexi[‘idt’];
$_SESSION[‘datetime’]=$index[‘datetime’];
$_SESSION[‘col1’] =$indexi[‘col1’];
$_SESSION[‘col2’] =$indexi[‘col2’];
$_SESSION[‘col3’] =$indexi[‘col3’];
}
mysql_close($conn);
?>

[/code][code=html]<body>
<center><table><tr><td><br /><br /><br /></td></tr>
<tr><td><?php $_SESSION[‘idt’] ?></td></tr>
<tr><td><?php $_SESSION[‘datetime’] ?></td></tr>
<tr><td><?php $_SESSION[‘col1’] ?></td></tr>
<tr><td><?php $_SESSION[‘col2’] ?></td></tr>
<tr><td><?php $_SESSION[‘col3’] ?>
</td></tr></table></center>
</body>[/code]

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmMay 22.2014 — And your problem is?

btw - what is that foreach on $g doing for you? I can't make sense of it.
Copy linkTweet thisAlerts:
@deathshadowMay 22.2014 — I think you meant for ALL those TD to be in a single TR. They are ROWS of data in the database, so each of them is only one ROW.

Usually when building a table, it also helps to use the FULL markup for a table, with TH, THEAD, TBODY, TFOOT (if applicable) and CAPTION, with the TH properly using the SCOPE attribute. Also, it's 2014 not 1997, so you have NO business having a CENTER tag in your markup.

Might also help if you weren't using the mysql_ functions like it's still 2004. We've been told for close to eight years (since PHP 5.1 dropped) to stop using them, which is why a couple years ago they added the [url=http://us3.php.net/function.mysql-connect]GIANT RED WARNING BOXES[/url] to tell you to... well... STOP USING THEM!

Would also be a good idea to open PHP properly, and leverage it's echo function properly, and stop making variables for NOTHING.

It's also a REALLY bad idea to make a massive array copy of your entire result set, as that increases your memory footprint for no good reason... which is why your logic is confusing gibberish since you aren't even using your $g index variable, with that "for" that seems to be doing "while's" job.

Generally speaking, I also consider it bad practice to open and close PHP more than once per file. [i]I had my way and the "php tags" of <?php and ?> would be removed from the specification... as would a few other things.[/i]

&lt;?php

<i> </i>$db = new PDO(
<i> </i> 'mysql:host=server.com;dbname=database',
<i> </i> 'username',
<i> </i> 'password'
<i> </i>);

<i> </i>$statement = $db-&gt;query('
<i> </i> SELECT * FROM test
<i> </i>');

<i> </i>echo '
<i> </i>&lt;table id="test"&gt;
<i> </i> &lt;caption&gt;Describe this Table&lt;/caption&gt;
<i> </i> &lt;thead&gt;
<i> </i> &lt;tr&gt;
<i> </i> &lt;th scope="col"&gt;id&lt;/th&gt;
<i> </i> &lt;th scope="col"&gt;date&lt;/th&gt;
<i> </i> &lt;th scope="col"&gt;col 1&lt;/th&gt;
<i> </i> &lt;th scope="col"&gt;col 2&lt;/th&gt;
<i> </i> &lt;th scope="col"&gt;col 3&lt;/th&gt;
<i> </i> &lt;/tr&gt;
<i> </i> &lt;/thead&gt;&lt;tbody&gt;
<i> </i> ';

<i> </i>while ($row = $statement-&gt;fetch()) echo '&lt;tr&gt;
<i> </i> &lt;th scope="row"&gt;', $row['idt'], '&lt;/th&gt;
<i> </i> &lt;td&gt;', $row['datetime'], '&lt;/td&gt;
<i> </i> &lt;td&gt;', $row['col1'], '&lt;/td&gt;
<i> </i> &lt;td&gt;', $row['col2'], '&lt;/td&gt;
<i> </i> &lt;td&gt;', $row['col3'], '&lt;/td&gt;
<i> </i> &lt;/tr&gt;';

<i> </i>echo '
<i> </i> &lt;/tbody&gt;
<i> </i>&lt;/table&gt;';

?&gt;


No extra variables needed. The SCOPE on the THEAD>TR>TH says they are headings for their columns, the SCOPE=ROW on TBODY>TR>TH says it's the heading for that row.

Generally speaking on a modern page, you shouldn't even be needing extra classes on that unless you REALLY need to support IE6; more than enough hooks to style with CSS already present.
×

Success!

Help @Philosophaie spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...