Jump to content

Select info belonging to logged in user


kannuk

Recommended Posts

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

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

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);

:confused:

 

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!

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);

:confused:

 

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.

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);

:confused:

 

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());

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.