Page 1 of 1

Two rogue fields not printing their contents...

Posted: Sun Dec 14, 2003 5:08 pm
by robster
Hi :)

I have a database with a bunch of stuff in the tables, it contains these things:

$id = $xcontent["id"];
$uid = $xcontent["uid"];
$year = $Xcontent["year"];
$month = $Xcontent["month"];
$number = $Xcontent["number"];
$title = $Xcontent["title"];
$animator = $Xcontent["animator"];
$hardware = $Xcontent["hardware"];
$software = $Xcontent["software"];
$creationtime = $Xcontent["creationtime"];
$rendertime = $Xcontent["rendertime"];
$viewrecommend = $Xcontent["viewrecommend"];
$animdescription = $Xcontent["animdescription"];
$descriptionofcreation = $Xcontent["descriptionofcreation"];
$popularity = $Xcontent["popularity"];
$mpg = $Xcontent["mpg"];
$jpg = $Xcontent["jpg"];
$rating = $Xcontent["rating"];
$ratingtally = $Xcontent["ratingtally"];
$ratingavg = $Xcontent["ratingavg"];
$ratingafter = $Xcontent["ratingafter"];

Now, what I want to do, for this example, is simply print the contents of these variables (which I've created above). Here's my code:

Code: Select all

$connection = mysql_connect($dbhost, $dbusername, $dbpassword);

// connect to current table
$dir = "ASC";
mysql_select_db($dbname);
$sql = "SELECT * FROM current ORDER BY id $dir";
$content = mysql_query($sql);
$Xcontent = mysql_fetch_array($content);
				
// how many entries are there?
$ShowMax = mysql_num_rows($content);
echo "Total number of entries so far: <b>$ShowMax</b> <br /><br />";
				
// see if this person has already entered
for ($y=1; $y<=$ShowMax; $y++)
{ 
	$id = $xcontent["id"];								
	$uid = $xcontent["uid"];				
       $year = $Xcontent["year"];
      $month = $Xcontent["month"];
	$number = $Xcontent["number"];
	$title =  $Xcontent["title"];
	$animator =  $Xcontent["animator"];
	$hardware = $Xcontent["hardware"];
	$software = $Xcontent["software"];
	$creationtime = $Xcontent["creationtime"];
	$rendertime =  $Xcontent["rendertime"];
	$viewrecommend =  $Xcontent["viewrecommend"];
	$animdescription = $Xcontent["animdescription"];
	$descriptionofcreation = $Xcontent["descriptionofcreation"];
	$popularity =  $Xcontent["popularity"];
	$mpg =  $Xcontent["mpg"];
	$jpg =  $Xcontent["jpg"];
	$rating =  $Xcontent["rating"];
	$ratingtally =  $Xcontent["ratingtally"];
	$ratingavg =  $Xcontent["ratingavg"];
	$ratingafter =  $Xcontent["ratingafter"];

					
	echo "id = $id <br />";
	echo "uid = $uid <br />";
	echo "number = $number <br />";
	echo "year = $year <br />";
	echo "month = $month <br />";
	echo "animator = $animator <br />";
	echo "title = $title <br /><br />";
			
	//empty database query stuff so we can cycle and see what is next thru the loop
	$Xcontent = mysql_fetch_array($content);
					
	}

Now I should say there is tonnes more code in this and I want to do more than just print, but what I find bizzarre is that the variables $id and $uid print nothing even though there IS content in the database. EVERY other field prints out its contents just fine.

To see if the content was coming through I changed the code to this:

Code: Select all

$sql = "SELECT * FROM current ORDER BY uid $dir";
(note I used uid to order rather than id)

This DID have an effect showing that the original $sql query can see the contents of the database in those fields, but when I try and access them with my $uid = $xcontent["uid"]; it doesn't seem to work.

Does anyone have any suggestions?

It's wierding me out and slowing me down and I just can't see what it could be. I've spent two nights on this now and just HAVE to get through it.

Thanks a TON to anyone who can offer a suggestion.

:)

Rob

Posted: Sun Dec 14, 2003 6:17 pm
by Weirdan
Variable names in PHP are case sensitive so $xcontent and $Xcontent are different variables.

Posted: Mon Dec 15, 2003 3:33 am
by robster
Weirdan wrote:Variable names in PHP are case sensitive so $xcontent and $Xcontent are different variables.
Thank you SO much!
It's always the little things isn't it :) I really appreciate that.

Rob

Posted: Mon Dec 15, 2003 3:46 am
by twigletmac
Aside from the variable case issue, your logic is a bit strange for retrieving the info from the database, you could modify it to:

Code: Select all

<?php

$connection = mysql_connect($dbhost, $dbusername, $dbpassword);
mysql_select_db($dbname);

$dir = "ASC";

// naming all the fields you want to return is better because it helps 
// you find problems later on and ensures you aren't returning
// fields you don't need
$sql = "SELECT id, uid, year, month, number, title, animator, hardware, software, creationtime, rendertime, viewrecommend, animdescription, descriptionofcreation, popularity, mpg, jpg, rating, ratingtally, ratingavg, ratingafter FROM current ORDER BY id $dir";

$content = mysql_query($sql);

// don't return the first row of the result here, wait until you're in
// the loop
      
// how many entries are there?
$ShowMax = mysql_num_rows($content);
echo '<p>Total number of entries so far: <b>'.$ShowMax.'</b></p>';

// simplify the loop by using a while loop instead of a for loop
// using a simple variable such as $row to hold the information
// within each row makes it easier to type and causes less
// problems with case
while ($row = mysql_fetch_assoc($content)) {

	$id                    = $row['id'];            
	$uid                   = $row['uid'];      
	$year                  = $row['year'];
	$month                 = $row['month'];
	$number                = $row['number'];
	$title                 = $row['title'];
	$animator              = $row['animator'];
	$hardware              = $row['hardware'];
	$software              = $row['software'];
	$creationtime          = $row['creationtime'];
	$rendertime            = $row['rendertime'];
	$viewrecommend         = $row['viewrecommend'];
	$animdescription       = $row['animdescription'];
	$descriptionofcreation = $row['descriptionofcreation'];
	$popularity            = $row['popularity'];
	$mpg                   = $row['mpg'];
	$jpg                   = $row['jpg'];
	$rating                = $row['rating'];
	$ratingtally           = $row['ratingtally'];
	$ratingavg             = $row['ratingavg'];
	$ratingafter           = $row['ratingafter'];
        
	echo '<p>id = '.$id.' <br />';
	echo 'uid = '.$uid.' <br />';
	echo 'number = '.$number.' <br />';
	echo 'year = '.$year.' <br />';
	echo 'month = '.$month.' <br />';
	echo 'animator = '.$animator.' <br />';
	echo 'title = '.$title.' </p>';

}

?>
Mac

Posted: Mon Dec 15, 2003 1:12 pm
by Weirdan
It can be simplified even more using [php_man]extract[/php_man] funtion:

Code: Select all

$connection = mysql_connect($dbhost, $dbusername, $dbpassword); 
mysql_select_db($dbname); 

$dir = "ASC"; 

// naming all the fields you want to return is better because it helps 
// you find problems later on and ensures you aren't returning 
// fields you don't need 
$sql = "SELECT id, uid, year, month, number, title, animator, hardware, software, creationtime, rendertime, viewrecommend, animdescription, descriptionofcreation, popularity, mpg, jpg, rating, ratingtally, ratingavg, ratingafter FROM current ORDER BY id $dir"; 

$content = mysql_query($sql); 

// don't return the first row of the result here, wait until you're in 
// the loop 
      
// how many entries are there? 
$ShowMax = mysql_num_rows($content); 
echo '<p>Total number of entries so far: <b>'.$ShowMax.'</b></p>'; 

// simplify the loop by using a while loop instead of a for loop 
// using a simple variable such as $row to hold the information 
// within each row makes it easier to type and causes less 
// problems with case 
while ($row = mysql_fetch_assoc($content)) { 
   extract($row); //we do not rename any vars, so we can extract them           
   echo '<p>id = '.$id.' <br />'; 
   echo 'uid = '.$uid.' <br />'; 
   echo 'number = '.$number.' <br />'; 
   echo 'year = '.$year.' <br />'; 
   echo 'month = '.$month.' <br />'; 
   echo 'animator = '.$animator.' <br />'; 
   echo 'title = '.$title.' </p>'; 

}

Posted: Tue Dec 16, 2003 2:31 am
by twigletmac
Just be sure when using extract that you aren't overwriting any other variables you may have set with the same names as one's from the row of database data.

Mac