...

View Full Version : How to Select all users?



RawliJr
07-28-2006, 11:06 PM
how do i get all the users that are listed in my database to show up in a table?

would it be something like ?

<?php SELECT username, id FROM 'users' WHERE rank by 'id' ?>

how would i do that? and how would i make it into a table?

Kid Charming
07-28-2006, 11:15 PM
You need to send your query to the server via mysql_query() (http://us3.php.net/manual/en/function.mysql-query.php). The doc page has an example.

RawliJr
07-29-2006, 12:19 AM
so i would use this code?



<?php
// This could be supplied by a user, for example
$firstname = 'fred';
$lastname = 'fox';

// Formulate Query
// This is the best way to perform a SQL query
// For more examples, see mysql_real_escape_string()
$query = sprintf("SELECT firstname, lastname, address, age FROM friends WHERE firstname='%s' AND lastname='%s'",
mysql_real_escape_string($firstname),
mysql_real_escape_string($lastname));

// Perform Query
$result = mysql_query($query);

// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}

// Use result
// Attempting to print $result won't allow access to information in the resource
// One of the mysql result functions must be used
// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc.
while ($row = mysql_fetch_assoc($result)) {
echo $row['firstname'];
echo $row['lastname'];
echo $row['address'];
echo $row['age'];
}

// Free the resources associated with the result set
// This is done automatically at the end of the script
mysql_free_result($result);
?>


ok so i edited that and it didnt work it just showed errors this is what i had


<?php
// This could be supplied by a user, for example
$username = 'Rawlijr';
$id = '1';

// Formulate Query
// This is the best way to perform a SQL query
// For more examples, see mysql_real_escape_string()
$query = sprintf("SELECT username, id FROM users WHERE username='%s' AND id='%s'",
mysql_real_escape_string($username),
mysql_real_escape_string($id));

// Perform Query
$result = mysql_query($query);

// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}

// Use result
// Attempting to print $result won't allow access to information in the resource
// One of the mysql result functions must be used
// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc.
while ($row = mysql_fetch_assoc($result)) {
echo $row['username'];
echo $row['id'];
}

// Free the resources associated with the result set
// This is done automatically at the end of the script
mysql_free_result($result);
?>

Kid Charming
07-29-2006, 12:24 AM
Pretty much. The important parts are mysql_query() to send your query to the server and retrieve the result resource that contains your data, and one of the mysql_fetch_* functions to extract usable data from the result resource.

Note you will also need to use mysql_connect() and mysql_select_db() to establish a connection and choose a database before you can send any queries.

Read up on those functions, and you should be on your way.

RawliJr
07-29-2006, 12:35 AM
<?php
mysql_connect( 'Localhost', 'xxx', 'xxx' );
mysql_select_db( 'xxx' );

$sql = "SELECT id, username, FROM users ORDER BY id DESC LIMIT 0,10";

if ($top10sql = mysql_query($sql))
{
echo '<table border="0" align="center" width="400" cellspacing="2" cellpadding="2">
<tr>
<td> Id</td>
<td> Username </td>
</tr>';
while ($disp_users = mysql_fetch_assoc($top10sql))
{
echo ' <tr>';
echo ' <td>' . $disp_users['id'] . '</td>';
echo ' <td>' . $disp_users['username'] . '</td>';
echo ' </tr>';
}
echo '</table>';
}
?>


ok i tried something else, this code above, bit it doesnt show anything how come? and i have th rights fields and stuff?

Kid Charming
07-29-2006, 12:51 AM
The comma after username is causing a syntax error.

For debugging this sort of thing, you can use the mysql_error() function -- it will return the db's error message upon failure.

RawliJr
07-29-2006, 12:54 AM
thanx i got it:D

guelphdad
07-30-2006, 01:57 AM
As an aside, do your query in mysql first. then once you know the query is correct and returning the proper rows, then write the code for it in PHP or ColdFusion or whatever. If you do it in that order then you know that when the query works in mysql that any errors after that would result from the PHP code.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum