displaying info from database using foreach- SOLVED
Posted: Thu Jul 26, 2012 5:39 am
I'm working on a subscription system that works similar to youtube
so far I am able to manualy subsribe to users by inserting data into the database.
Now I want to display the array of users that I am subscribed to on my profile page.
However, I am a bit confused on the foreach function and I'm running into parameter errors.
I've used foreach before on my page which is found here yourtechview.com/source/user_list.php
which shows all of the registered users on my site and each one is linked to their profile page.
I want to do the same thing but with the people I am subscribed to.
To make the user list I made a function
[syntax=php]function fetch_users(){
$result = mysql_query("SELECT `id_user` AS `id`, `username` AS `username` FROM `fgusers3` ");
$users = array();
while (($row = mysql_fetch_assoc($result)) !== false){
$users[] = $row;
}echo mysql_error();
return $users;
}[/syntax]
Then I called the function on the user_list.php as follows
[syntax=php]<?php
foreach (fetch_users() as $user){
?>
<p>
<a href="profile.php?uid=<?php echo $user['id'];?>"><?php echo $user['username']; ?> </a>
</p>
<?php
}
?>[/syntax]
This works beautifully.
Now I want to connect to the same table fgusers3
but I only want to fetch the usernames and ids of the usernames that are = to $subscribedto
When I echo $subscribedto I only get 1 username without links of course which is what I want.
This is how $subscribed to is born
[syntax=php]
require_once 'dbconnect.php';
$sqlCommand = "SELECT `username` FROM fgusers3 WHERE id_user = '$subscribers'";
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error());
$rows = mysqli_fetch_assoc($query);
$subscribedto = $rows['username'];[/syntax]
I setup a function
[syntax=php]function fetch_subscribedto()
{ $result = mysql_query("SELECT `id_user` AS `id`, `username` AS `username` FROM `fgusers3` WHERE `username` = {$subscribedto} ");
$users = array();
while (($row = mysql_fetch_assoc($result)) !== false){
$users[] = $row;
}echo mysql_error();
return $users;
}[/syntax]
then I added the foreach within my profile page
[syntax=php]<?php
foreach (fetch_subscribedto() as $user){
?>
<p>
<a href="profile.php?uid=<?php echo $user['id'];?>"><?php echo $user['username']; ?> </a>
</p>
<?php
}
?>[/syntax]
I get this over 1 million times
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/content/66/9481266/html/source/subscribers.php on line 38
line 38 is [syntax=php]while (($row = mysql_fetch_assoc($result)) !== false){[/syntax]
common sense tells me it is what I am making `username equal` to
[syntax=php]$result = mysql_query("SELECT `id_user` AS `id`, `username` AS `username` FROM `fgusers3` WHERE `username` = {$subscribedto} ");[/syntax]
Any clue?
so far I am able to manualy subsribe to users by inserting data into the database.
Now I want to display the array of users that I am subscribed to on my profile page.
However, I am a bit confused on the foreach function and I'm running into parameter errors.
I've used foreach before on my page which is found here yourtechview.com/source/user_list.php
which shows all of the registered users on my site and each one is linked to their profile page.
I want to do the same thing but with the people I am subscribed to.
To make the user list I made a function
[syntax=php]function fetch_users(){
$result = mysql_query("SELECT `id_user` AS `id`, `username` AS `username` FROM `fgusers3` ");
$users = array();
while (($row = mysql_fetch_assoc($result)) !== false){
$users[] = $row;
}echo mysql_error();
return $users;
}[/syntax]
Then I called the function on the user_list.php as follows
[syntax=php]<?php
foreach (fetch_users() as $user){
?>
<p>
<a href="profile.php?uid=<?php echo $user['id'];?>"><?php echo $user['username']; ?> </a>
</p>
<?php
}
?>[/syntax]
This works beautifully.
Now I want to connect to the same table fgusers3
but I only want to fetch the usernames and ids of the usernames that are = to $subscribedto
When I echo $subscribedto I only get 1 username without links of course which is what I want.
This is how $subscribed to is born
[syntax=php]
require_once 'dbconnect.php';
$sqlCommand = "SELECT `username` FROM fgusers3 WHERE id_user = '$subscribers'";
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error());
$rows = mysqli_fetch_assoc($query);
$subscribedto = $rows['username'];[/syntax]
I setup a function
[syntax=php]function fetch_subscribedto()
{ $result = mysql_query("SELECT `id_user` AS `id`, `username` AS `username` FROM `fgusers3` WHERE `username` = {$subscribedto} ");
$users = array();
while (($row = mysql_fetch_assoc($result)) !== false){
$users[] = $row;
}echo mysql_error();
return $users;
}[/syntax]
then I added the foreach within my profile page
[syntax=php]<?php
foreach (fetch_subscribedto() as $user){
?>
<p>
<a href="profile.php?uid=<?php echo $user['id'];?>"><?php echo $user['username']; ?> </a>
</p>
<?php
}
?>[/syntax]
I get this over 1 million times
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/content/66/9481266/html/source/subscribers.php on line 38
line 38 is [syntax=php]while (($row = mysql_fetch_assoc($result)) !== false){[/syntax]
common sense tells me it is what I am making `username equal` to
[syntax=php]$result = mysql_query("SELECT `id_user` AS `id`, `username` AS `username` FROM `fgusers3` WHERE `username` = {$subscribedto} ");[/syntax]
Any clue?