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 Link to comment https://forums.phpfreaks.com/topic/90311-solved-use-session-in-sql-statement/ 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? Link to comment https://forums.phpfreaks.com/topic/90311-solved-use-session-in-sql-statement/#findComment-463121 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.'; ?> Link to comment https://forums.phpfreaks.com/topic/90311-solved-use-session-in-sql-statement/#findComment-463123 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"; ?> Link to comment https://forums.phpfreaks.com/topic/90311-solved-use-session-in-sql-statement/#findComment-463124 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. Link to comment https://forums.phpfreaks.com/topic/90311-solved-use-session-in-sql-statement/#findComment-463127 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 />"; Link to comment https://forums.phpfreaks.com/topic/90311-solved-use-session-in-sql-statement/#findComment-463130 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 Link to comment https://forums.phpfreaks.com/topic/90311-solved-use-session-in-sql-statement/#findComment-463132 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 Link to comment https://forums.phpfreaks.com/topic/90311-solved-use-session-in-sql-statement/#findComment-463133 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. Link to comment https://forums.phpfreaks.com/topic/90311-solved-use-session-in-sql-statement/#findComment-463134 Share on other sites More sharing options...
p2grace Posted February 10, 2008 Share Posted February 10, 2008 Really? That's weird. Link to comment https://forums.phpfreaks.com/topic/90311-solved-use-session-in-sql-statement/#findComment-463135 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.