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
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
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
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
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; }Then I called the function on the user_list.php as follows
<?php foreach (fetch_users() as $user){ ?> <p> <a href="profile.php?uid=<?php echo $user['id'];?>"><?php echo $user['username']; ?> </a> </p> <?php } ?>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
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'];I setup a function
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; }then I added the foreach within my profile page
<?php foreach (fetch_subscribedto() as $user){ ?> <p> <a href="profile.php?uid=<?php echo $user['id'];?>"><?php echo $user['username']; ?> </a> </p> <?php } ?>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
while (($row = mysql_fetch_assoc($result)) !== false){common sense tells me it is what I am making `username equal` to
$result = mysql_query("SELECT `id_user` AS `id`, `username` AS `username` FROM `fgusers3` WHERE `username` = {$subscribedto} ");Any clue?