Jump to content

Archived

This topic is now archived and is closed to further replies.

lssjg

News problem - same scripts... different problem

Recommended Posts

for the 2nd time i am stuck.,... this time it isn't just adding { and }.


The following my edited version of the whole php script... the bits i took out is just the table and the layout of each table of news...

[code]<?

include 'include/config.php';

$query = mysql_query('SELECT * FROM news ORDER BY newsid DESC');

while($news = mysql_fetch_array($query)) {

$username = mysql_query("SELECT username FROM users WHERE loginname='{$news['author']}'");

echo "<td align=\"center\" colspan=\"2\">
Posted by <a href=\"user.php?action=profile&mode=view&user={$news['author']}\">$username</a> | Posted at {$news['date']}
<br />
<br />"; }

?>[/code]


Ok... now explanation time...


For each row in 'news' it will
- grab the username for the 'author'
- stick the username in after the words 'posted by'
- and a few dozen other things that aren't related to the problem

Now... what's happening... there isn't any error... (thus why i don't have anything to go on)

instead this happens Posted by Resource id #8 starting at 8... the next news is 9 then 10 and then 11.... that's the 4 test news entries i've done. though the newsid for them isn't 8, 9 10 or 11.. it's 1, 2, 4 and 5... cause i deleted one.

Anyway... of course the author part is still working because the link is still going to the correct address


i tested putting test1 as the author of an entry and also a member in users... and thus it pointed at [a href=\"http://localhost/site/user.php?action=profile&mode=view&user=test1\" target=\"_blank\"]http://localhost/site/user.php?action=prof...view&user=test1[/a] so it can make the link between the tables... it just gives me resource link all the time.


It is doing the same in FF and IE


Can anyone please help?


P.S. the author is simply the login name of the user that wrote the news.... author isn't username because i'm allowing changing of username....

Share this post


Link to post
Share on other sites
1) You can't use $username like that because you forgot to do a fetch for the second query!

2) You're code doesn't check for errors. Click on the [a href=\"http://www.phpfreaks.com/forums/index.php?showtopic=31047&view=findpost&p=153359\" target=\"_blank\"]PHP F.A.Q.[/a] link. Find and read the [b]MySQL Data Retrieval[/b] section for example code of getting data from MySQL with error checking after every MySQL command.

3) You can do one query instead of two by using a join. See tutorial: [a href=\"http://www.keithjbrown.co.uk/vworks/mysql/mysql_p5.shtml\" target=\"_blank\"]http://www.keithjbrown.co.uk/vworks/mysql/mysql_p5.shtml[/a]

Share this post


Link to post
Share on other sites
Huh........ that kinda doesn't make much sense... could you give an example...

like i have read it but i don't kinda understand ... could you show me what you mean?

Share this post


Link to post
Share on other sites
bump..

i really would quite like to know this... could someone give an example

Share this post


Link to post
Share on other sites
The FAQ had MySQL fetch examples. So, does the join tutorial. Anyway, below are a couple of untested examples (there maybe PHP or SQL syntax errors).

Example 1:
[code]
include 'include/config.php';

$query = mysql_query('SELECT `author`, `date` FROM `news` ORDER BY newsid DESC');
if (!$query) {
   echo 'SQL error on news select', mysql_error();
   exit;
}

while($news = mysql_fetch_assoc($query)) {

$result = mysql_query("SELECT `username` FROM `users` WHERE `loginname`='{$news['author']}'");
if (!$result) {
   echo 'SQL error on users select', mysql_error();
   exit;
}
$row = mysql_fetch_assoc($result);
$username = $row ? $row['username'] : '';  // if no data, set to empty string

echo "<td align=\"center\" colspan=\"2\">
Posted by <a href=\"user.php?action=profile&mode=view&user={$news['author']}\">$username</a> | Posted at {$news['date']}
<br />
<br />";
}
[/code]


Example 2:
[code]
include 'include/config.php';

$query = mysql_query('SELECT `news`.author, `news`.date, `users`.username FROM `news` LEFT JOIN `users` ON `news`.author = `users`.loginname ORDER BY `news`.newsid DESC');
if (!$query) {
   echo 'SQL error on select', mysql_error();
   exit;
}

while($row = mysql_fetch_assoc($query)) {

echo "<td align=\"center\" colspan=\"2\">
Posted by <a href=\"user.php?action=profile&mode=view&user={$row['author']}\">{$row['username']}</a> | Posted at {$row['date']}
<br />
<br />";
}
[/code]

Share this post


Link to post
Share on other sites
Hey, thanks....

i have taken a look at both... the 2nd doesn't work... but the 1st does, so i'm using that, but i have edited it quite a bit....... it almost looks like a different language.... hee [img src=\"style_emoticons/[#EMO_DIR#]/laugh.gif\" style=\"vertical-align:middle\" emoid=\":laugh:\" border=\"0\" alt=\"laugh.gif\" /]

anyway..... now my only problem, is now the actual news isn't showing up on ff or ie

Share this post


Link to post
Share on other sites
I've updated the 2nd example. You really should use one query. It will be a whole lot faster.

Good luck.

Share this post


Link to post
Share on other sites
ok... now the 2nd never really worked... and without editing this new 3nd i get

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in E:\Websites\Server\www\site\site\news.php on line 21

Anyway.. that's href=\"user.php?action=profile&mode=view&user={$news['author']}\">$username</a> |


what i was really trying to do is...

before this thread i could get everything woring but, i got resource id # something as the username..... so i start using the 1st example.... and suddenly the news (the actual) news doesn't turn up......

what iw ant is for the news to come back


Share this post


Link to post
Share on other sites
When needing help, always post the most current relevant code with error and line number of where the error is.

To help with echoing variables click on the [a href=\"http://www.phpfreaks.com/forums/index.php?showtopic=31047&view=findpost&p=153359\" target=\"_blank\"]PHP F.A.Q.[/a] link. Find and read the answer to [b]Q: Is there another way of getting around escaping quotes all the time?
[/b].

Another approach is to use the heredoc method:
[a href=\"http://us2.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc\" target=\"_blank\"]http://us2.php.net/manual/en/language.type....syntax.heredoc[/a]

Parse Errors are caused by human error. Check on the line of the error, but usually one to many lines before it, for missing closing tags (i.e. ", ), }, ;, etc.).

Here's a list of what those PHP tokens seen in error messages mean:
[a href=\"http://us2.php.net/manual/en/tokens.php#tokens\" target=\"_blank\"]http://us2.php.net/manual/en/tokens.php#tokens[/a]

Share this post


Link to post
Share on other sites
the reason why i made another post... is because i couldn't work it out... i tried 3 other methods.... and the script i am currently trying is the 1st example (in a couple of posts above). Once i get that working i will modifiy it to my hearts content...

Share this post


Link to post
Share on other sites

×

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.