Jump to content


Photo

News problem - same scripts... different problem


  • Please log in to reply
10 replies to this topic

#1 lssjg

lssjg
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 14 May 2006 - 10:31 AM

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

<?

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 />"; }

?>


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

#2 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 14 May 2006 - 12:21 PM

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 MySQL Data Retrieval 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]



#3 lssjg

lssjg
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 15 May 2006 - 04:04 AM

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?

#4 lssjg

lssjg
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 17 May 2006 - 06:51 AM

bump..

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

#5 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 17 May 2006 - 07:35 AM

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:
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 />";
}


Example 2:
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 />";
}


#6 lssjg

lssjg
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 17 May 2006 - 07:50 AM

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

#7 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 17 May 2006 - 06:35 PM

I've updated the 2nd example. You really should use one query. It will be a whole lot faster.

Good luck.


#8 lssjg

lssjg
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 17 May 2006 - 11:06 PM

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




#9 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 18 May 2006 - 02:48 AM

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 Q: Is there another way of getting around escaping quotes all the time?
.

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]

#10 lssjg

lssjg
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 18 May 2006 - 09:25 AM

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

#11 lssjg

lssjg
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 22 May 2006 - 09:43 AM

still need help making this appear...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users