/    Sign up×
Community /Pin to ProfileBookmark

Quick Question

hey, ive got a database of games and i want the list of the names to be displayed and im pretty sure i can do that from the database. But would someone be able to point me at some tutorial to like add <a href=”game?id=$name”>$name</a> to do something like that for each name. maybe something to do with loops like display all data from the database with such and such. makes sense?

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJan 13.2006 — If using MySQL, this page shows a basic example of the process to query the database and display the values returned from each row returned by the query.

http://www.php.net/manual/en/function.mysql-fetch-assoc.php
Copy linkTweet thisAlerts:
@welshauthorJan 13.2006 — so its saying use this
[code=php]
<?php

$conn = mysql_connect("localhost", "mysql_user", "mysql_password");

if (!$conn) {
echo "Unable to connect to DB: " . mysql_error();
exit;
}

if (!mysql_select_db("mydbname")) {
echo "Unable to select mydbname: " . mysql_error();
exit;
}

$sql = "SELECT id as userid, fullname, userstatus
FROM sometable
WHERE userstatus = 1";

$result = mysql_query($sql);

if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}

if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}

// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
// Note: If you put extract($row); inside the following loop, you'll
// then create $userid, $fullname, and $userstatus
while ($row = mysql_fetch_assoc($result)) {
echo $row["userid"];
echo $row["fullname"];
echo $row["userstatus"];
}

mysql_free_result($result);

?>
[/code]

the main part i get is:
[code=php]while ($row = mysql_fetch_assoc($result)) {
echo $row["userid"];
echo $row["fullname"];
echo $row["userstatus"];
}

mysql_free_result($result);[/code]
so how would i add the loop for just variable. i get i can add my href stuff after the echo.
Copy linkTweet thisAlerts:
@NogDogJan 13.2006 — [code=php]
$query = "SELECT game_name FROM games ORDER BY game_name";
$result = mysql_query($query) or die(mysql_error());
if(mysql_num_rows($result) > 0)
{
echo "<ul>n";
while($row = mysql_fetch_assoc($result))
{
$url = urlencode($row['game_name']);
$name = $row['game_name'];
echo "<li><a href='game?id=$url'>$name</a></li>n";
}
echo "</ul>n";
}
[/code]
Copy linkTweet thisAlerts:
@welshauthorJan 13.2006 — thanks man, that works amazingly!
×

Success!

Help @welsh 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 5.18,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...