...

View Full Version : Unexpected t_string error



Socca
03-27-2009, 06:57 PM
Hello,

Gettin back to php once again. I'm starting simple but I can't figure out what the problem with my code is:


echo '<p><b>'.$row['title'].'</b><br />
'.$row['sd'].'

'.$row['post'].'<br /><br />


<i>Posted by</i> <b>'.$row['author'].'</b>;

}

}else {

echo 'Apologies from Sean! At the moment, there are no displaying news publications!
Please check back soon!';

}

?>

I am getting the "
Parse error: syntax error, unexpected T_STRING, expecting ',' or ';'" error near the Apologies line.

Thanks,

Sean

F-b0mb
03-27-2009, 07:06 PM
There's no quote on the end of your last echo.

Socca
03-27-2009, 07:52 PM
Actually it was already there but the [PHP] tags don't show it so I changed it to [CODE] on the forum post. Any other ideas?

steelaz
03-27-2009, 08:04 PM
First echo is missing single quote. Replace:



echo '<p><b>'.$row['title'].'</b><br />
'.$row['sd'].'

'.$row['post'].'<br /><br />


<i>Posted by</i> <b>'.$row['author'].'</b>;


with:



echo '<p>
<b>'.$row['title'].'</b><br />
'.$row['sd'].' '.$row['post'].'<br /><br />
<i>Posted by</i> <b>'.$row['author'].'</b>';

Socca
03-27-2009, 09:39 PM
Ah Stupid mistake! Thanks Steelaz!
Do you know maybe Why the system doesn't show the else echo?
This is the full file:

<html>

<head>


</head>

<body>

<?php

include ('_mysql.php');


$query = "SELECT id, title, author, post, DATE_FORMAT(date, '%M %d, %Y') as sd FROM news_posts ORDER BY date DESC";

$result = @mysql_query($query);



if ($result) {

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

$news_id = $row['id'];



echo '<p><b>'.$row['title'].'</b><br />


'.$row['post'].'<br /><br />


<i>Posted by</i> <b>'.$row['author'].'</b> on '.$row['sd'].'';

}

}else {

echo '<b>Apologies from Sean Lofgren! At the moment, there are no displaying news publications! Please check back soon!<b>';

}

?>

</body>

</html>




Any ideas?

steelaz
03-27-2009, 11:15 PM
if ($result) - will check if query was successful. Query can be successful but return 0 results. For that you need mysql_num_rows() function. See the code below.




<html>
<body>

<?php
include ('_mysql.php');

$query = "SELECT id, title, author, post, DATE_FORMAT(date, '%M %d, %Y') as sd FROM news_posts ORDER BY date DESC";

$result = @mysql_query($query);

if ($result)
{
if (mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$news_id = $row['id'];
echo '<p><b>'.$row['title'].'</b><br />
'.$row['post'].'<br /><br />
<i>Posted by</i> <b>'.$row['author'].'</b> on '.$row['sd'].'';
}
}
else
{
echo '<b>Apologies from Sean Lofgren! At the moment, there are no displaying news publications! Please check back soon!<b>';
}

}
else
{
echo '<b>Database error: '. mysql_error();
}
?>

</body>
</html>

Socca
03-28-2009, 12:59 AM
Works now! Thanks alot. Last but not least do I use the Select tool to only display the last 3 or 5 articles or do I need to add some more lines? Is there no

$query = "SELECT id, title, author, post, DATE_FORMAT(date, '%M %d, %Y') as sd FROM news_posts LATEST 5 DESC"


? THanks again!

PappaJohn
03-28-2009, 01:20 AM
$query = "SELECT id, title, author, post, DATE_FORMAT(date, '%M %d, %Y') as sd FROM news_posts ORDER BY date DESC LIMIT 5";

assuming you want it ordered by the date field DESC

Socca
03-28-2009, 11:36 AM
Ah It was limit. I saw on some work they used latest but i guess that was probably a different function. Thanks once again!

Socca
03-28-2009, 02:36 PM
If I wanted a function whcih allowed me to view news post seperately...like if i did news.php?id=2 it would show me Id#2 news article how would i get around that? If you could simplify your explanation so that I could try first on my own..and if I needed help ill ask again that would be great.

steelaz
03-28-2009, 02:58 PM
First you would need to get id parameter from URI string:



// (int) typecasting makes sure that value is integer
$id = (int)$_GET['id'];


Then you can use it in the query:



$query = "SELECT * FROM `news_posts` WHERE `id` = '{$id}'";

Socca
03-30-2009, 05:10 PM
OK I tried first adding those two things into the news.php file and that didn't work. I guess it wasn't so easy. Would it be better if I made a new php file to do the above? Or what should i do in news.php to get that result?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum