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

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.