Jump to content

Fetch array problem


smc

Recommended Posts

Heya,

Alright basically I'm trying to create a listing of my usernames in the database. This is the code I am working with:

[code=php:0]
//Lets go ahead and connect to the DB using my fancy patent pending function (saves time doesn't it)
dbconnect();

//The following will execute a query to list all the users

//Query for execution
//$fetchfido = mysql_query("SELECT * FROM users ORDER BY username ASC");
$fetchfido = mysql_query('SELECT * FROM `users` ORDER BY `users` . `username` ASC');
//While we are grabbing the array we will go ahead and prep the HTML for echoing
while($users = mysql_fetch_array($fetchfido)){
$listusers = "<a href=\"admin.php?user=$users[username]\">$users[username]</a><br />";
$cms_template_path = '../templates/admin/';
require($cms_template_path . 'admin_users.php');
die;
}
[/code]

My problem is with the code above it only lists the first username and then stops. I tried taking out the die; function and it listed all the usernames, but also included a new template for every single specific username.

How can I get around this problem?
Link to comment
https://forums.phpfreaks.com/topic/35565-fetch-array-problem/
Share on other sites

your sql statement may not be right

[code]$fetchfido = "SELECT * FROM `users` ORDER BY username ASC";[/code]

Also set the template path before the loop. not need to set it for every user in the table.
What is in the admin_users.php file???
The die function wil cause the loop to end the first time it runs through. Take it out and put the die after the loop.

Ray
Link to comment
https://forums.phpfreaks.com/topic/35565-fetch-array-problem/#findComment-168397
Share on other sites

[quote author=craygo link=topic=123881.msg512617#msg512617 date=1169670807]
your sql statement may not be right

[code]$fetchfido = "SELECT * FROM `users` ORDER BY username ASC";[/code]

Also set the template path before the loop. not need to set it for every user in the table.
What is in the admin_users.php file???
The die function wil cause the loop to end the first time it runs through. Take it out and put the die after the loop.

Ray
[/quote]

The admin_users.php file is the template file for admin_users.php script (both in different folders). admin_users.php template only has <? echo $listusers; ?>

And my problem is if I take die out then it will keep running and running, creating a template for every single user. If I put the require and die after the while script then it only shows the last user to be called in the array
Link to comment
https://forums.phpfreaks.com/topic/35565-fetch-array-problem/#findComment-168409
Share on other sites

Not to go around your problem here, but why would you call another file to echo our your results??

[code]<?php
$sql = "SELECT * FROM `users` ORDER BY username ASC";
$fetchfido = mysql_query($sql) or die(mysql_error());
while($users = mysql_fetch_array($fetchfido)){
echo "<a href=\"admin.php?user=".$users[username]."\">".$users[username]."</a><br />";
}
?>[/code]

Ray
Link to comment
https://forums.phpfreaks.com/topic/35565-fetch-array-problem/#findComment-168416
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.