Jump to content

Archived

This topic is now archived and is closed to further replies.

desjardins2010

a simple SELECT....

Recommended Posts

Hey Again;

 

trying to have the code select the top 5 players bases on exper field in the DB

 

'SELECT * from players WHERE exper = '';

 

 

Share this post


Link to post
Share on other sites

look into ORDER BY and LIMIT. if exper is, say, a numeric value you can have it sort by the value and limit the returned records to 5

Share this post


Link to post
Share on other sites

what error or output are you getting? ("don't work" doesn't supply us enough info)

Share this post


Link to post
Share on other sites

What do you mean by it "don't work"? What is it doing or not doing that it should or should not do?

Share this post


Link to post
Share on other sites

Does it sit on the couch all day, eating nachos, drinking beer, and ignoring the classifieds? ;)

Share this post


Link to post
Share on other sites

sorry bout that, here is the code

 

 

 

<?php

 

 

if (!$link = mysql_connect('localhost', 'root', '')) {

    echo 'Could not connect to mysql';

    exit;

}

 

if (!mysql_select_db('tutorial')) {

    echo 'Could not select database';

    exit;

}

 

$sql    = "SELECT * FROM players ORDER BY exper DESC LIMIT 5;

 

 

 

$result = mysql_query($sql, $link);

 

if (!$result) {

    echo "DB Error, could not query the database\n";

    echo 'MySQL Error: ' . mysql_error();

    exit;

}

 

?>

<H2>TOP PLAYERS BY EXPERIENCE</H2><BR>

<?php

 

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

 

echo "<table border=\"1\" align=\"left\">";

echo "<tr>{$row['name']}</tr>";

 

 

 

}

 

mysql_free_result($result);

 

?>

 

Now it should be putting out the names of the players with the top 5 highest experience but it's returning an error..

 

Parse error: parse error in C:\wamp\www\test.php on line 23

 

Share this post


Link to post
Share on other sites

$sql    = "SELECT * FROM players ORDER BY exper DESC LIMIT 5;

Should be

 

$sql    = "SELECT * FROM players ORDER BY exper DESC LIMIT 5";

Share this post


Link to post
Share on other sites

Ahhh shoots ok yes that would be it..

 

that worked now however the output is all in a row I have tried this

 

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

 

echo "<table border=\"1\" align=\"left\">";

echo "<tr>{$row['name']}</tr>"."<BR>"; // notice the <BR> but that didn't do it

 

that gave me this

 

solidsouljasonharrylawrencetom

Share this post


Link to post
Share on other sites

echo "<table border=\"1\" align=\"left\">";

while ($row = mysql_fetch_assoc($result)) {
echo "<tr>{$row['name']}</tr>";
}

echo "</tr></table>";

Share this post


Link to post
Share on other sites
echo "<table border=\"1\" align=\"left\">";

while ($row = mysql_fetch_assoc($result)) {
echo "<tr><td>{$row['name']}</td></tr>";
}

echo "</table>";

Share this post


Link to post
Share on other sites

I dunno I don't get it makes sence to me but getting this error:

 

Notice: Use of undefined constant count - assumed 'count' in E:\WAMP\www\index.php on line 5

 

 

code is here

 

<?php

 

 

if (!$link = mysql_connect('localhost', 'root', '')) {

    echo 'Could not connect to mysql';

    exit;

}

 

if (!mysql_select_db('tutorial')) {

    echo 'Could not select database';

    exit;

}

 

$sql    = "SELECT * FROM players ORDER BY exper DESC LIMIT 5";

 

 

 

$result = mysql_query($sql, $link);

 

if (!$result) {

    echo "DB Error, could not query the database\n";

    echo 'MySQL Error: ' . mysql_error();

    exit;

}

 

?>

<H2>TOP PLAYERS BY EXPERIENCE</H2><BR>

<?php

 

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

 

echo "<table border=\"1\" align=\"left\">";

  echo "<tr><td>{$row['name']}</td></tr>";

}

 

echo "</table>";

 

 

 

mysql_free_result($result);

 

?>

Share this post


Link to post
Share on other sites

From now on please wrap you code by hitting the php icon when posting. Also, I dont see a constant count. This file isn't index.php is it? Line 5 of this is an echo..

Share this post


Link to post
Share on other sites

Not that it is affecting the problem you mention, shouldn't

echo "<table border=\"1\" align=\"left\">";

come before the 'while' statement?

Steve

Share this post


Link to post
Share on other sites

<H2>TOP PLAYERS BY EXPERIENCE</H2><BR>
<?php

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

echo "<table border=\"1\" align=\"left\">";
   echo "<tr><th>Player</th><th>Experience</th></tr><tr align=\"center\"><td>john doe</td><td>894</td></tr>";
}

echo "</table>";

 

so this is working however not given me the results I was hoping for.. what this is doing is drawing a new row for each what I'm looking for is simply a table with two bold headers one saying PLAYER the other saying EXPERIENCE and then each record in a col under it

 

any ideas?

Share this post


Link to post
Share on other sites

Anything within the while() loop will repeat for each record retrieved.

Share this post


Link to post
Share on other sites

"<tr><th>Player</th><th>Experience</th></tr><tr align=\"center\"><td>john doe</td><td>894</td></tr>"

 

this is drawing out exactly what I want.. problem is I want the next record to just add itself to the table in DESC order of experience

 

right now it's drawing that out for each record as you said it will do this during a while loop

Share this post


Link to post
Share on other sites
echo "<table border=\"1\" align=\"left\"><tr><th>Player</th><th>Experience</th></tr>";
while ($row = mysql_fetch_assoc($result))
{
   echo "<tr align=\"center\"><td>john doe</td><td>894</td></tr>";
}
echo "</table>";

Share this post


Link to post
Share on other sites

×

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.