Display News Function Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
partiallynothing
Forum Commoner
Posts: 61
Joined: Fri Nov 21, 2003 5:02 pm
Location: connecticut, usa

Display News Function Problem

Post by partiallynothing »

I am trying to display the news. In the mysql news table I have the user id of who posted it, so somehow I have to get the also query the user table for the information of the person with the specific id. I think this will work, but I am getting an error: "You have an error in your SQL syntax near '5`' at line 1". Any help is appreciated!

Here is my code so far...

Code: Select all

<?php
//**********************************************************//
//StudentCenter                                                                                 //
//Display News Function Version 0.1                                                     //
//                                                                                                      //
//Written by: Rob Frawley                                                                    //
//************************************* *********************//

require('config2.inc.php');

function displaynews($limit)
 {
 //query database for news
 $news->query1 = "SELECT * FROM `news` ";
 $news->query1 .= "ORDER BY `id` DESC ";
 $news->query1 .= "LIMIT 0, `{$limit}`";
 $news->result1 = mysql_query($news->query1) or die (mysql_error());

 //display news
 while ($row1 = mysql_fetch_array($news->result1)) {

        //query database for poster info
        $news->query2 = "SELECT * FROM `users` ";
        $news->query2 .= "WHERE `id` = `{$row1['userid']}`";
        $news->result2 = mysql_query($news->query2) or die (mysql_error());
        $row2 = mysql_fetch_array($news->result2);

        //display news
        echo 'NEWS:' . '<br>';
        echo '<u>' . 'Date:' . '</u>' . ' ' . $row1['timestamp'] . ' ' . '<u>' . 'Author:' . '</u>' . ' ' . $row2['firstname'] . ' ' .
$row2['lastname'] . '<br>';
        echo $row1['news'];

        }

 }

displaynews(5);
?>
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

First of change this...
$news->query1 .= "LIMIT 0, `{$limit}`";
to...
$news->query1 .= "LIMIT 0, {$limit}";

And also change this...
$news->query2 .= "WHERE `id` = `{$row1['userid']}` ";
to...
$news->query2 .= "WHERE `id` = '{$row1['userid']}' ";
Post Reply