Jump to content

[SOLVED] use session in sql statement


dare87

Recommended Posts

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

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.';
?>

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";
?>

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.