6 Replies - 466 Views - Last Post: 08 November 2011 - 06:37 AM Rate Topic: -----

#1 Syfer  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 190
  • Joined: 08-October 10

error on either on my loop or if statement

Posted 08 November 2011 - 06:08 AM

<?php
	echo "<table border=\"1\">";
					$query = mysql_query("SELECT * FROM news ORDER BY news_id") or die(mysql_error());
                    $total_results= mysql_num_rows($query);
	                if ($total_results >0){
					echo "<tr><td>News ID</td><td>News Title</td><td>News Content</td><td>News Picture</td><td>News Date</td></tr>";	
					for($i = 0; $i < mysql_num_rows($query); $i++)				
						$a = mysql_result($query, $i, 'news_id');
						$b= mysql_result($query,$i,'news_title');
						$c= mysql_result($query,$i,'news_content');
						$d = mysql_result($query, $i, 'news_pic');
						$e = mysql_result($query, $i, 'news_date');
                         echo '<tr><td>'.$a.'</td><td>'.$b.'</td><td>'.$c.'</td><td>'.$d.'</td><td>'.$e.'</td><td><a href="editnews.php?action=edit&id='.$a.'">edit</a></td><td><a href="editnews.php?action=delete&id='.$a.'">delete</a></td>';	
						 
				}else{
						echo "<div style='position:absolute;left:420px;font-size:50px;'>No News created!</div>";
					}	
	echo "</table>";				



it shows this kind of error
arning: mysql_result() [function.mysql-result]: Unable to jump to row 2 on MySQL result index 6 in C:\xampp\htdocs\caps\editnews.php on line 82

Warning: mysql_result() [function.mysql-result]: Unable to jump to row 2 on MySQL result index 6 in C:\xampp\htdocs\caps\editnews.php on line 83

Warning: mysql_result() [function.mysql-result]: Unable to jump to row 2 on MySQL result index 6 in C:\xampp\htdocs\caps\editnews.php on line 84

Warning: mysql_result() [function.mysql-result]: Unable to jump to row 2 on MySQL result index 6 in C:\xampp\htdocs\caps\editnews.php on line 85



i paste the lines where i get errors.

p.s

or error from the misplacing of my if statement and its brackets

This post has been edited by Syfer: 08 November 2011 - 06:14 AM


Is This A Good Question/Topic? 0
  • +

Replies To: error on either on my loop or if statement

#2 dD3  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 16
  • Joined: 04-November 11

Re: error on either on my loop or if statement

Posted 08 November 2011 - 06:16 AM

just a quick look.. there are no opening nor closing bracket on this for statement:

 for($i = 0; $i < mysql_num_rows($query); $i++)               


Was This Post Helpful? 1
  • +
  • -

#3 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3715
  • View blog
  • Posts: 5,972
  • Joined: 08-June 10

Re: error on either on my loop or if statement

Posted 08 November 2011 - 06:16 AM

It's usually better to use mysql_fetch_assoc rather than mysql_result.
while($row = mysql_fetch_assoc($query)) {
    $a = $row['news_id'];
    // etc...
}


That way you aren't manually moving around inside the result set, you're just reading it in row by row.
Was This Post Helpful? 1
  • +
  • -

#4 satis  Icon User is offline

  • D.I.C Head

Reputation: 82
  • View blog
  • Posts: 231
  • Joined: 26-May 11

Re: error on either on my loop or if statement

Posted 08 November 2011 - 06:18 AM

You're missing a { on line 7 above, where you're opening your for loop. That may be the cause

for($i = 0; $i < mysql_num_rows($query); $i++) {               


I may also suggest you use PDO or MySQLi instead of the old MySQL functions... they're bad.

Also, lines 8-12 are superfluous... you can just spit out the results directly in your string on line 13. No need to put them in a holder variable. And lastly, using variables like $a, $b, $c etc etc is bad practice. If you ever write code you have to come back to 6 months later, you'll rue using non-descriptive variable names. :) Even if this is just for learning, it's a good idea to get in practice.

*edit* woo, jeez, all kinds of responses beat mine in. Need to remember to post faster.

This post has been edited by satis: 08 November 2011 - 06:19 AM

Was This Post Helpful? 0
  • +
  • -

#5 Syfer  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 190
  • Joined: 08-October 10

Re: error on either on my loop or if statement

Posted 08 November 2011 - 06:21 AM

View PostdD3, on 08 November 2011 - 06:16 AM, said:

just a quick look.. there are no opening nor closing bracket on this for statement:

 for($i = 0; $i < mysql_num_rows($query); $i++)               




View PostAtli, on 08 November 2011 - 06:16 AM, said:

It's usually better to use mysql_fetch_assoc rather than mysql_result.
while($row = mysql_fetch_assoc($query)) {
    $a = $row['news_id'];
    // etc...
}


That way you aren't manually moving around inside the result set, you're just reading it in row by row.

tried the both of this
and got the some error

Parse error: syntax error, unexpected T_ELSE in C:\xampp\htdocs\caps\editnews.php on line 88


Was This Post Helpful? 0
  • +
  • -

#6 satis  Icon User is offline

  • D.I.C Head

Reputation: 82
  • View blog
  • Posts: 231
  • Joined: 26-May 11

Re: error on either on my loop or if statement

Posted 08 November 2011 - 06:23 AM

That's actually a different error, bud. Check line 88... you're missing a closing brace. }
Was This Post Helpful? 1
  • +
  • -

#7 Syfer  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 190
  • Joined: 08-October 10

Re: error on either on my loop or if statement

Posted 08 November 2011 - 06:37 AM

oh yes , thanks :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1