shcKr- Posted April 14, 2009 Share Posted April 14, 2009 Hello, Im pretty new to php, but what im trying to do is create a basic permissions systems, where i can change what access the user has, this is the code I have, but I can't work out how to make it show more than one folder ??? With this code, it shows me just one folder, when I have 2 in the database.. <?php include "_config.php"; include "_functions.php"; session_start(); $user_name = $_SESSION["username"]; $row=mysql_fetch_array(mysql_query("SELECT * FROM `users` WHERE username = '$user_name' LIMIT 1")); $user_id=stripslashes($row['id']); $row2=mysql_fetch_array(mysql_query("SELECT * FROM `permissions` WHERE userID = '$user_id'")); $folderID=stripslashes($row2['folderID']); $allowed=stripslashes($row2['allowed']); $row3=mysql_fetch_array(mysql_query("SELECT * FROM `folders` WHERE id = '$folderID'")); $name=stripslashes($row3['name']); echo $name; ?> Quote Link to comment https://forums.phpfreaks.com/topic/154024-no-idea-what-my-problem-is/ Share on other sites More sharing options...
Mchl Posted April 14, 2009 Share Posted April 14, 2009 See example #2 at mysql_query Quote Link to comment https://forums.phpfreaks.com/topic/154024-no-idea-what-my-problem-is/#findComment-809626 Share on other sites More sharing options...
shcKr- Posted April 14, 2009 Author Share Posted April 14, 2009 I changed my code to the following, but it still only displays 1 result <?php include "_config.php"; include "_functions.php"; session_start(); $user_name = $_SESSION["username"]; $row=mysql_fetch_array(mysql_query("SELECT * FROM `users` WHERE username = '$user_name' LIMIT 1")); $user_id=stripslashes($row['id']); $row2=mysql_fetch_array(mysql_query("SELECT * FROM `permissions` WHERE userID = '$user_id'")); $folderID=stripslashes($row2['folderID']); $allowed=stripslashes($row2['allowed']); // Formulate Query // This is the best way to perform a SQL query // For more examples, see mysql_real_escape_string() $query = sprintf("SELECT name FROM folders WHERE id='$folderID'", mysql_real_escape_string($name)); // Perform Query $result = mysql_query($query); // Check result // This shows the actual query sent to MySQL, and the error. Useful for debugging. if (!$result) { $message = 'Invalid query: ' . mysql_error() . "\n"; $message .= 'Whole query: ' . $query; die($message); } // Use result // Attempting to print $result won't allow access to information in the resource // One of the mysql result functions must be used // See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc. while ($row = mysql_fetch_assoc($result)) { echo $row['name']; } // Free the resources associated with the result set // This is done automatically at the end of the script mysql_free_result($result); ?> Quote Link to comment https://forums.phpfreaks.com/topic/154024-no-idea-what-my-problem-is/#findComment-809638 Share on other sites More sharing options...
Mchl Posted April 14, 2009 Share Posted April 14, 2009 This $query = sprintf("SELECT name FROM folders WHERE id='$folderID'", mysql_real_escape_string($name)); should be $query = sprintf("SELECT name FROM folders WHERE id='%s'", mysql_real_escape_string($folderID)); Apart from that it looks fine. Are you sure this query returns more than one row? You can check using mysql_num_rows Quote Link to comment https://forums.phpfreaks.com/topic/154024-no-idea-what-my-problem-is/#findComment-809649 Share on other sites More sharing options...
shcKr- Posted April 14, 2009 Author Share Posted April 14, 2009 Ok, I replaced what you put, and it doesn't give me any results now. I added the num_rows.. this is my code: this is what it gives: www.citiphones.co.uk/test/test.php <?php include "_config.php"; include "_functions.php"; session_start(); $user_name = "James"; $row=mysql_fetch_array(mysql_query("SELECT * FROM `users` WHERE username = '$user_name' LIMIT 1")); $user_id=stripslashes($row['id']); $row2=mysql_fetch_array(mysql_query("SELECT * FROM `permissions` WHERE userID = '$user_id'")); $folderID=stripslashes($row2['folderID']); $allowed=stripslashes($row2['allowed']); // Formulate Query // This is the best way to perform a SQL query // For more examples, see mysql_real_escape_string() $query = sprintf("SELECT name FROM folders WHERE id='%s'", mysql_real_escape_string($folderID)); // Perform Query $result = mysql_query($query); // Check result // This shows the actual query sent to MySQL, and the error. Useful for debugging. if (!$result) { $message = 'Invalid query: ' . mysql_error() . "\n"; $message .= 'Whole query: ' . $query; die($message); } // Use result // Attempting to print $result won't allow access to information in the resource // One of the mysql result functions must be used // See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc. while ($row = mysql_fetch_assoc($result)) { echo $row['name']; } // Free the resources associated with the result set // This is done automatically at the end of the script mysql_free_result($result); echo "<br><br>"; $result = mysql_query("SELECT * FROM folders"); $num_rows = mysql_num_rows($result); echo "$num_rows Rows\n"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/154024-no-idea-what-my-problem-is/#findComment-809782 Share on other sites More sharing options...
shcKr- Posted April 14, 2009 Author Share Posted April 14, 2009 This is what my tables look like: PERMISSIONS: id userID folderID allowed 1 1 1 1 2 1 2 1 USERS: id username password activated 1 James # 1 FOLDERS id name 1 Test 2 Test2 Quote Link to comment https://forums.phpfreaks.com/topic/154024-no-idea-what-my-problem-is/#findComment-809792 Share on other sites More sharing options...
JonnoTheDev Posted April 14, 2009 Share Posted April 14, 2009 Why are you mixing the way you are writing queries? This is bad code embedding functions that can return incorrect values $row2 =mysql_fetch_array(mysql_query("SELECT * FROM `permissions` WHERE userID = '$user_id'")); Better $query = mysql_query("SELECT * FROM permissions WHERE userID = '".mysql_real_escape_string($user_id)."'"); $row2 = mysql_fetch_array($query); Here also you have decided to use sprintf, why? $query = sprintf("SELECT name FROM folders WHERE id='%s'", mysql_real_escape_string($folderID)); Again stick to a standard: $query = mysql_query("SELECT name FROM folders WHERE id='".mysql_real_escape_string($row2['folderID'])."'"); while ($row = mysql_fetch_assoc($query)) { } I'm guessing you are copying and pasting code from a variety of sources Quote Link to comment https://forums.phpfreaks.com/topic/154024-no-idea-what-my-problem-is/#findComment-809798 Share on other sites More sharing options...
shcKr- Posted April 15, 2009 Author Share Posted April 15, 2009 I kinda figured out what my problem is, its only selecting my one result, so when the userid=1, it selects the two results from the permissions table, but it is only displaying the first result. Is there a way I can display all of the results ? Quote Link to comment https://forums.phpfreaks.com/topic/154024-no-idea-what-my-problem-is/#findComment-810443 Share on other sites More sharing options...
JonnoTheDev Posted April 15, 2009 Share Posted April 15, 2009 With a loop while ($row = mysql_fetch_assoc($query)) { } Quote Link to comment https://forums.phpfreaks.com/topic/154024-no-idea-what-my-problem-is/#findComment-810458 Share on other sites More sharing options...
shcKr- Posted April 15, 2009 Author Share Posted April 15, 2009 This is what my code stands at: <?php include "_config.php"; include "_functions.php"; session_start(); $user_name = "James"; $row=mysql_fetch_array(mysql_query("SELECT * FROM `users` WHERE username = '$user_name' LIMIT 1")); $user_id=stripslashes($row['id']); $row2=mysql_fetch_array(mysql_query("SELECT * FROM `permissions` WHERE userID = '$user_id'")); $folderID=stripslashes($row2['folderID']); $allowed=stripslashes($row2['allowed']); $result = mysql_query("SELECT * FROM `folders` WHERE id = '$folderID'"); while($row = mysql_fetch_assoc($result)) { echo $row['name'] . " " . $row['LastName']; echo "<br />"; } ?> and This is what my tables look like: PERMISSIONS: id userID folderID allowed 1 1 1 1 2 1 2 1 USERS: id username password activated 1 James # 1 FOLDERS id name 1 Test 2 Test2 Now this is what its showing me: www.citiphones.co.uk/test/test.php but surely it should be displaying: Test Test2 Quote Link to comment https://forums.phpfreaks.com/topic/154024-no-idea-what-my-problem-is/#findComment-810482 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.