PDA

View Full Version : Simple Output Query


harlequin2k5
05-21-2006, 09:56 PM
all that I want to do is output my list of board members from a mysql table

this is the last piece of code I've tried to work with - all taken from the php manual (from php.net) as well as another php/mysql book that I have

<?php require_once("functionlist.php");?>
<?php
$query = 'SELECT * FROM `board`';
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result))
{
echo $row['boardmemberid'];
echo $row['boardmembertitle'];
echo $row['boardmembername'];
echo $row['boardmemberterm'];
echo $row['boardmemberelection'];
}
mysql_free_result($result);
?>



this is my functionlist

// Connect to DB
function doconnect()
{
$dbhost = '127.0.0.1:3306';
$dbuser = '*';
$dbpass = '*';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to MySQL');

$dbname = 'suncoastusbcba_org_-_association';
mysql_select_db($dbname, $conn) or die ('Error finding database');

return $conn;
}



all of this is supposed to display in a list (I'll worry about formatting it later) on this page (http://test.suncoastusbcba.org/board.php)

I know how easy this is but this is the first time I'm actually making my own query with php...

EDIT: I may need a bump to MySQL - sorry for the mispost

GJay
05-21-2006, 10:06 PM
the sql is fine, so it can stay here.

mysql_fetch_assoc returns an associative array, that is an array of key=>value pairs.
As you've used 'select *', I don't know what your columns are, but at a guess, change the echo to:

echo $row['username'];

anarchy3200
05-21-2006, 10:08 PM
You need to tell it which row you want it to display, so for example if your database had the columns:
ID | Username | Password
As you have selected all the columns you would need to echo $row[1]
as id would be $row[0] and password would be $row[2].
In your situation you will probably have more columns than demonstrated here but all you need to do is to count across as to which column it is starting from0... either this or you can substitute your * in your query for the username column name i.e.
$sql = 'SELECT username FROM `board`';
(username will need substituting with whatever the name of the username column is in your situation)
then: echo $row['0'];

Hope you understand what i have said.

Edit: Too slow... Gjays solution will also work, temporary lapse that you can use the column name as well as its number!

harlequin2k5
05-21-2006, 10:12 PM
I reposted the query code above - I didn't realize when I was originally posting that I had grabbed the wrong code

I want to return all 24 records - there are 5 columns...boardmemberid, boardmembertitle, boardmembername, boardmemberterm, boardmemberelection

GJay
05-21-2006, 10:26 PM
what does it do at the moment then?


(and as an aside, indexed references ($row[0] etc.) won't work with mysql_fetch_assoc)

TheShaner
05-21-2006, 10:58 PM
She's not calling the function doconnect() in functionlist.php, so it's never establishing a connection. All her code is fine. It just needs the line: $conn = doconnect(); before her query.

-Shane

harlequin2k5
05-21-2006, 11:06 PM
and the rest of the answer was I was using the wrong case - boardmemberid needed to be BoardMemberID...