Jump to content

Printing table data


Bounty

Recommended Posts

Something like this is probably what you're looking for.

 

$query = "SELECT `username` FROM `table` WHERE `username` IS NOT NULL;

$result = mysql_query($query);

echo "<table>";

count = 1;

while( $array = mysql_fetch_assoc($result) {

    echo "<tr><td>{$count}</td><td>{$array['username']}</td></tr>";

    $count++;

}

echo "</table>";

[code=php:0]

Link to comment
Share on other sites

I think you missed an '$' at count = 1;

Btw with this script i get  this error

Parse error: syntax error, unexpected '{' in C:\xampp\htdocs\register\memberspage.php on line 15

Line 15 is this:

while( $array = mysql_fetch_assoc($result) {

 

://

Thanks for the help btw :)

Link to comment
Share on other sites

D'oh! That's what I get for typing it directly in to the reply box . . . No syntax highlighting == I missed more than just that. Here it is edited, and without syntax errors.

 

$query = "SELECT `username` FROM `table` WHERE `username` IS NOT NULL";
$result = mysql_query($query);
echo "<table>";
$count = 1;
while( $array = mysql_fetch_assoc($result) ) {
     echo "<tr><td>{$count}</td><td>{$array['username']}</td></tr>";
     $count++;
}
echo "</table>";

Link to comment
Share on other sites

<html>
<head>
<title>Members</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
$tablee = "users";
$username = $_POST['username'];
$result = mysql_query ("SELECT username FROM $tablee WHERE username IS NOT NULL");

echo '<table>';
$count = 1;
while ($array = mysql_fetch_assoc($result)) {
     echo ("<tr><td>$count</td><td>$array[username]</td></tr>");
     $count++;
}
echo '</table>';
?>
</body>
</html>

Link to comment
Share on other sites

OK. Try this, but this time, copy and paste it. Don't go and change things until we see if it works or not.

 

<html>
<head>
<title>Members</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
$tablee = "users";
$username = $_POST['username'];
$result = mysql_query ("SELECT username FROM $tablee WHERE username IS NOT NULL");

echo '<table>';
$count = 1;
while ($array = mysql_fetch_assoc($result)) {
     echo ("<tr><td>$count</td><td>$array[username]</td></tr>");
     $count++;
}
echo '</table>';
?>
</body>
</html>

Link to comment
Share on other sites

Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\xampp\htdocs\register\memberspage.php on line 10

 

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\xampp\htdocs\register\memberspage.php on line 10

 

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\register\memberspage.php on line 14

Line 10:

$result = mysql_query ("SELECT username FROM $tablee WHERE username IS NOT NULL");

 

Line 14:

while ($array = mysql_fetch_assoc($result)) {

haven't changed a thing...the last time i was trying something thats why it was changed i forgot to undo it :P

Link to comment
Share on other sites

There's your problem. You aren't connecting to the database before running the query.

 

Look at:

the mysql_connect() and mysql_select_db() functions.

 

$server = // your server ip address, or localhost if that's the case
$user = // database user name
$pass = // database user's password, if applicable
$db_name = // database name

$dbc = mysql_connect($server, $user, $pass);

mysql_select_db( $db_name, $dbc)

 

Link to comment
Share on other sites

I'm not sure how, but I managed to paste your old code into the reply yesterday, instead of the code I edited. For that, I apologize.

 

Neither of those code blocks have parse errors, though, and neither of them has $username = $_POST['username'] on line 14. In both versions, that is line 9, and the value of $username is not even used in the script. Here is the version of it tried to post yesterday.

 

There are 5 lines of code before this script somewhere. Is this script include()d by another script? If it is, what is in that script?

 

<html>
<head>
<title>Members</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
$tablee = "users";
$username = $_POST['username']; // Does this line have a purpose in this script? The value is never used.
$query = "SELECT `username` FROM `$tablee` WHERE `username` IS NOT NULL";
$result = mysql_query($query) or die('Query cratered: ' . mysql_error() . '<br />Using query string: ' . $query);
echo '<table>';
$count = 1;
while ($array = mysql_fetch_assoc($result)) {
     echo ("<tr><td>$count</td><td>" . $array['username'] . "</td></tr>");
     $count++;
}
echo '</table>';
?>
</body>
</html>

Link to comment
Share on other sites

Have no idea how that line got in there...must be my mistake when pasting or something...

Anyhow..do i need database connection for this?

 

Wont work with or without the connection :///

 

This is the code i use now...

<html>
<head>
<title>Members</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
$server = "localhost";
$user = "***";
$pass = "***";
$db_name = "mysql";

$dbc = mysql_connect($server, $user, $pass);

mysql_select_db( $db_name, $dbc)
$query = "SELECT `username` FROM `users` WHERE `username` IS NOT NULL";
$result = mysql_query($query) or die('Query cratered: ' . mysql_error() . '<br />Using query string: ' . $query);
echo "<table>";
$count = 1;
while( $array = mysql_fetch_assoc($result) ) {
     echo "<tr><td>{$count}</td><td>{$array['username']}</td></tr>";
     $count++;
}
echo "</table>";
?>
</body>
</html>

 

It shows error in this line:

$query = "SELECT `username` FROM `users` WHERE `username` IS NOT NULL";

Should i escape quotes or anything..?

 

 

Link to comment
Share on other sites

Then there should only be 2 records in the `users` table in the database. How many are there?

 

Also, you aren't using any ORDER BY statement in the query, so it may not retrieve the records in the order you'd expect it to.

Link to comment
Share on other sites

So how can i order it?

 

Google "MySQL ORDER BY"

 

And is there a way to import data (usernames) in html directly?

 

What do you mean import username "directly"? I don't follow what you're asking.

 

Edit:

This is the table data structure its weird..

http://img149.imageshack.us/img149/5174/databh.jpg

I'm lost...

 

That isn't the table's structure, really. In phpMyAdmin, select the table, the click the "Structure" tab at the top to see the structure.

Link to comment
Share on other sites

What do you mean import username "directly"? I don't follow what you're asking.

Well could i make a variable that displays the list and when needed in html code just inserting the variable in the table..

 

That isn't the table's structure, really. In phpMyAdmin, select the table, the click the "Structure" tab at the top to see the structure.

Well what is it? I think its data inside the table...if it is than it is weird...

Link to comment
Share on other sites

You apparently hit the "Propose Table Structure" link. That doesn't give you the current table structure. The "Structure" tab at the top does. As for your first question, I still don't understand what you're wanting to do. The username information should already be in the database for all actual users, no? Why would you need to re-insert it?

Link to comment
Share on other sites

I ment something like this..could i make a string like "$userlist = command for listing" and use it in html anywhere in the code?

 

I know this is not a table structure but what is it? It looks messed up...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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