Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

php/mysql array problem

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
huwgreen



Joined: Mon Aug 01, 2005 10:04 am
Posts: 12

PostPosted: Mon Aug 01, 2005 10:22 am    Post subject: php/mysql array problem Reply with quote

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 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:

$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
Back to top
View user's profile Send private message
bigbee



Joined: Thu Jun 09, 2005 3:46 pm
Posts: 55
Location: Westerlo, Belgium

PostPosted: Mon Aug 01, 2005 9:19 pm    Post subject: Reply with quote

First of all mysql_fetch_assoc array returns a single row of your result your construction should be a little bit like this:
Code:

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
Back to top
View user's profile Send private message Visit poster's website
huwgreen



Joined: Mon Aug 01, 2005 10:04 am
Posts: 12

PostPosted: Wed Aug 03, 2005 8:41 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast