HTML Table /w mySQL.. help.. noob

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Exodus00
Forum Newbie
Posts: 13
Joined: Mon Aug 05, 2002 11:46 pm
Location: Oregon
Contact:

HTML Table /w mySQL.. help.. noob

Post by Exodus00 »

ok.. i read the other one posted.. but it wasn't with mySQL.. and I haven't even begun to look at ODBC... not a real microsoft fan, but.. maybee someday I might get around to it. Anyway... heres my code.. minus the database login info.. but I cant figure out whats wrong.. it keeps giving me an error near line 108, which i'll specify.. giving me a parse error.... I'll let you take a look at it.. its probably something really stupid and dumb.


this is basically the part giving me the errors...

<?php
Line 108* SELECT count(*) FROM account;

# Make the cells for each peice of data
print "<TR>\n";
foreach ("usernumber") { print "<TD WIDTH=\"25%\" ALIGN=\"CENTER\">$usernumber</TD>\n"; }
foreach ("callsign") { print "<TD WIDTH=\"25%\" ALIGN=\"CENTER\">$callsign</TD>\n"; }
foreach ("clan") { print "<TD WIDTH=\"25%\" ALIGN=\"CENTER\">$clan</TD>\n"; }
foreach ("age") { print "<TD WIDTH=\"25%\" ALIGN=\"CENTER\">$age</TD>\n"; }
print "</TR>\n";
}

?>

the columns i want posted have the names, usernumber, callsign, clan, age

if.. you have anymore questions I'll do the best to answer em.. any help whatsoever would be appreciated.. thanx

edit: this is all within a table already.. i just didn't post the HTML
learning_php_mysql
Forum Commoner
Posts: 27
Joined: Sun Aug 04, 2002 12:58 pm
Location: WA

Post by learning_php_mysql »

you need a nested loop like.....

Code: Select all

for($row=0; $row<$whatever; $row++) &#123;
    echo("<tr>");
    for($col=0; $col<$whenever; $col++) &#123;
        echo("\n\t<td>");
        echo("stuff in cell");
        echo("</td>\n");
        &#125;
    echo("</tr>");
&#125;
maybe that will help =/
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Hiya, need a little bit more information. Specifically, what is the exact error message and what exactly does line 108 and the lines above it and below it look like (not all 107+ just maybe the lines 100-115 or something). I think you may be having a problem with getting the information from the database. There's a few things I've noticed but I'd like to see more code.

Mac
Exodus00
Forum Newbie
Posts: 13
Joined: Mon Aug 05, 2002 11:46 pm
Location: Oregon
Contact:

Post by Exodus00 »

ok heres whats before the HTML. Thanx for replying.

Code: Select all

<?php
# Name
$db = "registered";

# Host
$host = "localhost";

# Username
$usr = "";

# Password
$pwd = "";

# Table
$table = "";

# connect 
$cid = mysql_connect($host,$usr,$pwd);
	?>
then inside the html table is what i had above and thats it...

then heres teh error, word for word

Parse error: parse error in /usr/local/psa/home/vhosts/oregonlan.ws/httpdocs/registered.php on line 108


i've been looking at the connection script.. I got that part from somewhere and put it in this.. but seem to have ommitted some peices now that i look at it..... the way they have it setup on the other application, i dont even think it will work with this....... but anyway... i'll post more if you need more.. trying to figure this out myself too with my PHP book, but its kinda hard to know where to look when i dont really know what i'm looking for :(
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Once you've done your connection to the MySQL database (the mysql_connect() part) you need a line in which you select a database using mysql_select_db():

Code: Select all

mysql_select_db('your_db_name_here') or die(mysql_error());
then you can set up your query (assuming usernumber, callsign, clan and age are the names of the fields whose information you want):

Code: Select all

$sql = "SELECT usernumber, callsign, clan, age FROM account";
and execute it using mysql_query():

Code: Select all

$result = mysql_query($sql) or die(mysql_error().'<p>'.$sql.'</p>');
Then to get the results you would most likely use mysql_fetch_assoc() in a while loop:

Code: Select all

while ($row = mysql_fetch_assoc($result)) &#123;
    $usernumber = $row&#1111;'usernumber'];
    $callsign = stripslashes($row&#1111;'callsign']);
    $clan = htmlspecialchars(stripslashes($row&#1111;'clan']));
    $age = $row&#1111;'age'];

    // Now do the HTML
    echo '<tr>';
    echo '<td width="25%" align="center">'.$usernumber.'</td>';
    echo '<td width="25%" align="center">'.$callsign.'</td>';
    echo '<td width="25%" align="center">'.$clan.'</td>';
    echo '<td width="25%" align="center">'.$age.'</td>';
    echo '</tr>';
&#125;
I've added in stripslashes() and htmlspecialchars() to the lines of code which retrieve information use these where necessary depending on the data coming out of the database.

If you use the links to the manual pages within this post you will find lots more examples. If you need help structuring an SQL query start with learning about SELECT at mysql.com.

Mac
Exodus00
Forum Newbie
Posts: 13
Joined: Mon Aug 05, 2002 11:46 pm
Location: Oregon
Contact:

Post by Exodus00 »

I have to go to work right now but I'll try that tonight. It looks promising. Thanks a ton, i'll keep you updated. :D
Exodus00
Forum Newbie
Posts: 13
Joined: Mon Aug 05, 2002 11:46 pm
Location: Oregon
Contact:

Post by Exodus00 »

Thanx a TON!!!, it worked perfectly the first time i tried. I also read the notes so I learned what I wasn't doing. Thanx again, that really helped me out.
Post Reply