php/mysql array problem

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

php/mysql array problem

Postby huwgreen » Mon Aug 01, 2005 9:22 am

I've just started working with php and mysql, and seem to have fallen at the first hurdle. I have a table called authority holding userids and level of access to the database. If I run the following code directly on the mysql server it displays all the userids and the total row count
Code: Select all
select userid from authority;

However, if I run it through php, and try to use mysql_fetch_array/assoc/result, and then try to display the result all that is returned is the first element of the array. This happens whatever I use to display the result, foreach, list, echo $varname[0]...[n].
mysql_num_rows returns the correct row count.
I've been trying to see where I've made a mistake for a couple of days now and I'm not getting anywhere.
The relevant php is:
Code: Select all
$userID_sql = "select userID from authority";
$userID = mysql_query ($userID_sql,$conn); //$conn is the connection index
$allowed = mysql_fetch_assoc($userID);
foreach ($allowed as $membid)
{
echo ("The ids are:<br>$membid<br>");
}

I'm using php/mysql/apache that is installed with SLES9
I hope someone can help.
Many thanks in advance
John
huwgreen
 
Posts: 12
Joined: Mon Aug 01, 2005 9:04 am

Postby bigbee » Mon Aug 01, 2005 8:19 pm

First of all mysql_fetch_assoc array returns a single row of your result your construction should be a little bit like this:
Code: Select all
int $foo=0;
while ($result = mysql_fetch_assoc($userid)) {
   $row[$foo]=$result[0]; //or result["userid"], there the associative part of fetch_assoc comes into play
   $foo++;
}
//this construct a row containing the userids
foreach ($row as $membid)
{
echo ("The ids are:<br>$membid<br>");//if $membid is an unique number this will print only one id, if several accounts have the same memid, this will be printed the amount of users with the same memid
}
//to print all the elements:
for($bar=0;$bar<count($row);$bar++)
{
echo "memberid = ".$row[$bar]."<br>";
}


The mysql_fetch_assoc is handy if you extract your data and want access to them by the row-name. eg. Say $result is the returned result of your query on a table containing "name" and "age"
$data=mysql_fetch_row($result); will be an array on which your data can be accessed as $data[0] and $data[1]
$data=mysql_fetch_assoc($result); will be an array on which your data can be accessed as $data["name"] and $data["age"]

I hope this will help you solve the problem... Let us/me know!

grtz

bb
User avatar
bigbee
 
Posts: 55
Joined: Thu Jun 09, 2005 2:46 pm
Location: Westerlo, Belgium

Postby huwgreen » Wed Aug 03, 2005 7:41 am

Many thanks bigbee, I've finally got it sorted. Sorry I haven't replied sooner, but I'm currently doing more things at once than I should!
Once again, many thanks
huwgreen
 
Posts: 12
Joined: Mon Aug 01, 2005 9:04 am


Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests

cron