Page 1 of 1

Mysql ID problem

Posted: Sun Jan 25, 2004 3:25 pm
by John Cartwright

Code: Select all

<?php
	// Display the text each row
    while ( $row = mysql_fetch_array($result) ) { 
	$date = $row["newsDate"]; 
	$title = $row["title"]; 
	$message = $row["message"]; 
 	$newsid = $row["ID"];
	
 echo  "<tr>"
	  ."<td>$title</td>"
      ."<td>$newsid</td>"
      ."<td><div align="center"><a href='$PHP_SELF?deletenews=$newsid'>Delete this News</a></div></td>"
	  ."</tr>";
 } 
?>
I'm just curious why isn't $newsid = $row["ID"]; being picked up???
Everything else like $title is working fine

Posted: Sun Jan 25, 2004 3:38 pm
by microthick
Show us your SQL statement that's getting $result.

Posted: Sun Jan 25, 2004 3:47 pm
by John Cartwright

Code: Select all

<?php
   $result = @mysql_query("SELECT * FROM news ORDER BY `id` DESC LIMIT 10");
?>
This?

Posted: Sun Jan 25, 2004 3:50 pm
by microthick
PHP variables are case-sensitive.

If your field is called id then use $row['id'] instead of $row['ID'].

Posted: Sun Jan 25, 2004 3:52 pm
by John Cartwright
hehe how did I miss that one :D

Thanks man

Posted: Sun Jan 25, 2004 4:08 pm
by microthick
Phenom wrote:hehe how did I miss that one :D

Thanks man
lol, I do it all the time, too. :P

Posted: Mon Jan 26, 2004 5:22 am
by twigletmac
When doing SQL queries it is a good idea to 1) separate the SQL statement into it's own variable and 2) always specify the column names you would like to return. It will make debugging much easier in the long run, e.g.:

Code: Select all

$result = @mysql_query("SELECT * FROM news ORDER BY `id` DESC LIMIT 10");
becomes

Code: Select all

$sql = "SELECT id, newsDate, title, message FROM news ORDER BY `id` DESC LIMIT 10"
$result = mysql_query($sql) or die(mysql_error().'<p>'.$sql.'</p>');
That way if you mispell a column name you'll get MySQL telling you about it before you get to the PHP processing bit.

Mac