Mysql ID problem

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Mysql ID problem

Post 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
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

Show us your SQL statement that's getting $result.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

<?php
   $result = @mysql_query("SELECT * FROM news ORDER BY `id` DESC LIMIT 10");
?>
This?
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

PHP variables are case-sensitive.

If your field is called id then use $row['id'] instead of $row['ID'].
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

hehe how did I miss that one :D

Thanks man
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

Phenom wrote:hehe how did I miss that one :D

Thanks man
lol, I do it all the time, too. :P
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Post Reply