 |
Linux Format forums Help, discussion, magazine feedback and more
|
| View previous topic :: View next topic |
| Author |
Message |
huwgreen
Joined: Mon Aug 01, 2005 10:04 am Posts: 12
|
Posted: Mon Aug 01, 2005 10:22 am Post subject: php/mysql array problem |
|
|
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 |
|
 |
bigbee

Joined: Thu Jun 09, 2005 3:46 pm Posts: 55 Location: Westerlo, Belgium
|
Posted: Mon Aug 01, 2005 9:19 pm Post subject: |
|
|
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 |
|
 |
huwgreen
Joined: Mon Aug 01, 2005 10:04 am Posts: 12
|
Posted: Wed Aug 03, 2005 8:41 am Post subject: |
|
|
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 previous topic :: View next topic |
|
|
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
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|