Page 1 of 1

problems with this code snippet

Posted: Sun Mar 23, 2003 3:01 pm
by sotonin
I am having problems with this code snippet. maybe somebody could perhaps show me where i am going wrong. i think it's probably the mysql_query im trying to do. The problem is i keep getting the "There was an error retrieving from the database". which means $query didnt return true in the mysql_query.

This is reading the arguement ?edit from the link which is called from a previous page.


Here is the news table structure
aid
int(11)
date
datetime
title
varchar(100)
text
text
ID
int(11) auto_increment

And the users table (Only the fields protaining to this snippet)
id
int(25) auto_increment
first_name
varchar(25)
last_name
varchar(25)

Code: Select all

if (isset($edit)) {
	$query = mysql_query("SELECT ID, aid, title, text FROM news WHERE ID='$edit'");
	
	if($query){
		while($row = mysql_fetch_array($query)){
			$id = $row['ID'];
			$aid = $row['aid'];
			$title = $row['title'];
			$text = $row['text'];
		}
	} else {
		header_valign_center();
		msg("There was an error retrieving from the database","");
		footer();
		exit();
	}
	
	$query = mysql_query("SELECT id, first_name, last_name, date FROM users WHERE id='$aid'");
	
	if($query){
		while($row = mysql_fetch_array($query)){
			$date = $row['date'];
			$name = $row['first_name'] . " " . $row['last_name'];
		}
	} else {
		header_valign_center();
		msg("There was an error retrieving from the database","");
		footer();
		exit();
	}
	
	header_valign_center();
	$news->edit($id, $title, $text, $date, $name);
	footer();
	exit();
}
Any help would be greatly appreciated. thanks!

Posted: Sun Mar 23, 2003 3:33 pm
by spammich
You didn't mention "date" as a field in the "user" table, but you seem to be asking for that as a result field in you query. That would definately cause a problem. Any help?

Posted: Sun Mar 23, 2003 5:50 pm
by Zoram
try using the mysql_error() in the failed case scenario.

Posted: Mon Mar 24, 2003 2:21 am
by twigletmac
To expand on what Zoram said, try:

Code: Select all

$sql = "SELECT id, first_name, last_name, date FROM users WHERE id='$aid'";
$query = mysql_query($sql); 
    
if ($query) { 
	while($row = mysql_fetch_array($query)){ 
		$date = $row['date']; 
		$name = $row['first_name'].' '.$row['last_name']; 
	} 
} else { 
	header_valign_center(); 
	msg('There was an error retrieving from the database.<br />'.mysql_error().'<br />'.$sql, '');
	footer(); 
	exit(); 
}
instead of

Code: Select all

$query = mysql_query("SELECT id, first_name, last_name, date FROM users WHERE id='$aid'"); 
    
   if($query){ 
      while($row = mysql_fetch_array($query)){ 
         $date = $row['date']; 
         $name = $row['first_name'] . " " . $row['last_name']; 
      } 
   } else { 
      header_valign_center(); 
      msg("There was an error retrieving from the database",""); 
      footer(); 
      exit(); 
   }
and do something similar for the first query.

Mac

Posted: Mon Mar 24, 2003 8:49 am
by sotonin
i figured out my problem, it was the syntax i was calling it, and in fact date was a culprit after i got the syntac correct. it was supposed to be in the top potion with news table. i changed syntax to this.

$query = "blahblah";

result = mysql_query($query);