/    Sign up×
Community /Pin to ProfileBookmark

Manual coding to display records from SQL DB?

Hi guys, I’m hoping that this is the correct forum to post this in.

I’m making a website for my local rugby club, and as a newbie to PHP I was hoping someone could help me with the following.

I need to display a list of players and their details on a single page called “squad list”. This page needs to have the following list on it, in the format shown:

Shirt # | Surname, Forename | Date of Birth | Nationality | Appearances | Tries

Now, the “surname, forename” will display like that, but it needs to come from two separate (cells? I can’t remember the correct name for it) places but in the same row. (I must apologise for not using the correct terminology, I’m struggling to remember right now what they are correctly called!! ? ? )
So the forename will come from the “forename” field and the surname from the “surname” field, but will be displayed as Surname, Forename (e.g. Bush, George or Blair, Tony – get the idea?)
Each player will have a unique identifier (playerID) but this will not be shown on the page (if possible).

Now I know how to do this if Dreamweaver connects to the database on my hosts server, but for some reason it’s refusing, so I’m wondering if someone could help me to manually code this so that it works…

I need to know how to connect to the database, as well as how to show these records.
Any help would be greatly appreciated, if you need any more information please let me know.

Many thanks.

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYJul 03.2006 — so, i understand you already have the dB set up?
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYJul 03.2006 — [code=php]
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">

</style>
</head>
<body>
<?php
# get database credentials
include "db_cred_file.php"; // where $db_host, $db_user, $db_pass, $db_name are defined

# connect to database
$connection = mysql_connect($db_host, $db_user, $db_pass) or die("<h2>Error:</h2><p>Couldn't connect to dB</p>");
mysql_select_db($db_name);

# query
$query = "SELECT * FROM <your_table_name>"; // <- if you want to add extra conditions
$result = mysql_query($query);

if(!$result) die("<h2>Error:</h2><p>Query error" . mysql_error() . "</p>");

# fetch result as array
while($q_array = mysql_fetch_array($result, MYSQL_ASSOC)){
# print out the infos for each player
# $q_array['key'] will contain the value of the 'key' field in the database for that row
# e.g.: $q_array['surename'] will contain the surename of the player in the current row
}
?>
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@AnotherDesignerauthorJul 03.2006 — The DB is set up and waiting - I know how to do that lol

Problem I'm having is just with the PHP. I'll try that out and get back to you later...

Incidentally, db_cred_file.php i presume I have to create - how do i specify the login/connection details to the database? What code would I need to put into it?

Thanks
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYJul 03.2006 — yes, create the file, best is to place it in a non-public directory

in the file:
[code=php]
<?php
$db_host = "localhost";
$db_user = "username"; // you get that from your host
$db_pass = "password"; // you'll usually get it from the host as well, or can set it somewhere
$db_name = "database_name"; // you set that
?>
[/code]
Copy linkTweet thisAlerts:
@Stephen_PhilbinJul 03.2006 — Fuzzy's expample has the login details for the database in the db_cred_file.php file (user name, password, database name and host). It would just be a file setting the variables used in the mysql_connect() and mysql_select_db() functions. Probably the most common way of doing it though, is to just put them in to the functions directly as plain strings. You can do it any way you like, just so long as the functions get the correct data they require to log in to the database.
×

Success!

Help @AnotherDesigner 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,
)...