dare87 Posted February 10, 2008 Share Posted February 10, 2008 I am trying to have a news post pull and use the persons userId. When they login in places the information into a session but I don't know how to get that information into the sql statement. Please help <?php // This will post the five most current news articles for a specific user. // Get User Information. $newsuser = $_SESSION['userId']; // Connect to the database. require_once('../mysql_connect.php'); $query = "SELECT title, article, author, DATE_FORMAT(date_written, '%M %d, %Y') AS dateTitle, DATE_FORMAT(date_written, '%c/%e/%y %l:%i%p') AS dateSig FROM news WHERE newsfor='$newsuser' ORDER BY date_written DESC LIMIT 5"; $results = mysql_query($query); Thanks for your help Quote Link to comment Share on other sites More sharing options...
p2grace Posted February 10, 2008 Share Posted February 10, 2008 The query looks right, what exactly is it doing wrong? Quote Link to comment Share on other sites More sharing options...
dare87 Posted February 10, 2008 Author Share Posted February 10, 2008 It doesn't pull anything. Here is the whole thing. <?php // This will post the five most current news articles for a specific user. // Get User Information. $newsuser = $_SESSION['userId']; // Connect to the database. require_once('../mysql_connect.php'); $query = "SELECT title, article, author, DATE_FORMAT(date_written, '%M %d, %Y') AS dateTitle, DATE_FORMAT(date_written, '%c/%e/%y %l:%i%p') AS dateSig FROM news WHERE newsfor='$newsuser' ORDER BY date_written DESC LIMIT 5"; $results = mysql_query($query); if ($results) { // Start the table. echo '<table align="left" cellspacing="0" cellpadding="0">'; // Insert the results. while ($row = mysql_fetch_array($results, MYSQL_ASSOC)) { echo ' <tr> <td class="newsDate" align="left">' . $row['dateTitle'] . '</td> </tr> <tr> <td class="newsTitle" align="left">' . $row['title'] . '</td> </tr> <tr> <td class="newsArticle" align="left">' . $row['article'] . '</td> </tr>'; echo' <tr> <td class="newsSig" align="left">Posted ' . $row['dateSig'] . ' by ' . $row['author'] . '</td> </tr> '; } // Close the table. echo '</table>'; // Free up the resources. mysql_free_result ($results); } else echo 'There are no news articles.'; ?> Quote Link to comment Share on other sites More sharing options...
p2grace Posted February 10, 2008 Share Posted February 10, 2008 Hmm with just glancing through your code, I'd try adding full syntax to the query, and if that doesn't work try copying your query into phpmyadmin or adding mysql_error() and seeing what kind of errors you get. (Obviously you'll have to replace the php variable with an actual id if you put it in phpmyadmin) The full syntax of the query would be: <?php $query = "SELECT `title`, `article`, `author`, DATE_FORMAT(`date_written`, '%M %d, %Y') AS `dateTitle`, DATE_FORMAT(`date_written`, '%c/%e/%y %l:%i%p') AS `dateSig` FROM `news` WHERE `newsfor`='$newsuser' ORDER BY `date_written` DESC LIMIT 0,5"; ?> Quote Link to comment Share on other sites More sharing options...
dare87 Posted February 10, 2008 Author Share Posted February 10, 2008 If I replace the $newsuser with a 1 it works. Quote Link to comment Share on other sites More sharing options...
p2grace Posted February 10, 2008 Share Posted February 10, 2008 Echo your newsuser and make sure there's something in the variable. $newsuser = $_SESSION['userId']; echo "newsuers = $newsuser <br />"; Quote Link to comment Share on other sites More sharing options...
dare87 Posted February 10, 2008 Author Share Posted February 10, 2008 It is posting the userId. I restarted the server and it works.... So thanks for your help, my system is just dumb. Thanks Quote Link to comment Share on other sites More sharing options...
p2grace Posted February 10, 2008 Share Posted February 10, 2008 Huh, Interesting. Can you mark the topic as solved. Thanks Quote Link to comment Share on other sites More sharing options...
dare87 Posted February 10, 2008 Author Share Posted February 10, 2008 I would love to.. but the link is gone. Quote Link to comment Share on other sites More sharing options...
p2grace Posted February 10, 2008 Share Posted February 10, 2008 Really? That's weird. Quote Link to comment 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.