kannuk Posted December 29, 2011 Share Posted December 29, 2011 Hi - how's it going? I've been working on something with no luck and I'm wondering if you have any ideas. I have two tables; Members and Events. When a member logs in they are redirected to a page where I want them to be able to see a list of events that they have created based on a user ID that is common in both tables. So if the member has a memberID of 123 I want them to see any events that have the userID of 123. I used two different column names just for clarity. I've tried a number of variations on the following code: <?php session_start(); header("Cache-control: private"); // Connect to Database include ('includes/db.php'); //Has login info // If not logged in if (!$_SESSION['pkMemberID']) { echo ("<div class='box'><h2>Sorry, you are not logged in!</h2>"); exit(); } // Convert Session variable 'pkMemberID' to simple variable. $memberID = $_SESSION['memberID']; $pkMemberID = $_SESSION['pkMemberID']; $name = $_SESSION['name']; //Query Events $sql = "SELECT * FROM events WHERE userID = $memberID"; $result = mysql_query($sql); $num_rows = mysql_num_rows($result); echo ("<div>Events:<br />"); // Event exists if (!($num_rows == 0)) { $myrow = mysql_fetch_array($result); do { printf ('<span>– <a href="event_edit.php?pkEventID=%s">%s</a></span><br />', $myrow['pkEventID'], $myrow['eventName']); } while ($myrow = mysql_fetch_array($result)); } echo ("</div>"); //End Events //end page ?> Sometimes I get errors, sometimes just nothing. Any advice would be appreciated. Link to comment https://forums.phpfreaks.com/topic/254043-select-info-belonging-to-logged-in-user/ Share on other sites More sharing options...
AyKay47 Posted December 29, 2011 Share Posted December 29, 2011 you are using mysql_fetch_array which will return an array corresponding to the fetched row(s), however you are calling your column by name, instead of array key. you will either need to change mysql_fetch_array to mysql_fetch_assoc or change your column output to an array key.. eg. printf ('<span>– <a href="event_edit.php?pkEventID=%s">%s</a></span><br />', $myrow['pkEventID'], $myrow[0]);[code=php:0] or you can even keep mysql_fetch_array and set the second parameter to MYSQL_BOTH Link to comment https://forums.phpfreaks.com/topic/254043-select-info-belonging-to-logged-in-user/#findComment-1302372 Share on other sites More sharing options...
kannuk Posted December 30, 2011 Author Share Posted December 30, 2011 Thanks for the feedback, AyKay47. I tried what you suggested but I don't even get that far because I get an error at line: $num_rows = mysql_num_rows($result); Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/... Maybe I need to sleep on this for a while! Link to comment https://forums.phpfreaks.com/topic/254043-select-info-belonging-to-logged-in-user/#findComment-1302399 Share on other sites More sharing options...
MasterACE14 Posted December 30, 2011 Share Posted December 30, 2011 Thanks for the feedback, AyKay47. I tried what you suggested but I don't even get that far because I get an error at line: $num_rows = mysql_num_rows($result); Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/... Maybe I need to sleep on this for a while! something wrong with your query. Link to comment https://forums.phpfreaks.com/topic/254043-select-info-belonging-to-logged-in-user/#findComment-1302400 Share on other sites More sharing options...
AyKay47 Posted December 30, 2011 Share Posted December 30, 2011 Thanks for the feedback, AyKay47. I tried what you suggested but I don't even get that far because I get an error at line: $num_rows = mysql_num_rows($result); Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/... Maybe I need to sleep on this for a while! yes that error mean that something is wrong with your query, debug your query using die and mysql_error $result = mysql_query($sql) or die(mysql_error()); Link to comment https://forums.phpfreaks.com/topic/254043-select-info-belonging-to-logged-in-user/#findComment-1302405 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.