Page 1 of 2

Variable error

Posted: Wed Aug 02, 2006 12:30 pm
by NiGHTFiRE
Hey,
I'm doing a profile script, diffrent things show depending on if it's the members profile that's being showed or some other users being showed. And when it's the same user that is watching their user i get undefinied variable $content. I know what it means but i don't understand why I get it cause I thought i've done it right.

I guess that the important information is where i've placed my sql quieries and where i've placed my $content variables.

Here is my code, (taken away unnessccary information:

Code: Select all

<?php 
$person2 = $_SESSION['sess_user'];
$sql2 = "SELECT * FROM members WHERE username = '{$person2}'"; 
$result2 = mysql_query($sql2) or die(mysql_error());


if(isset($_SERVER['QUERY_STRING'])) {
$person = substr($_SERVER['QUERY_STRING'], 1);
 
$sql = "SELECT * FROM members WHERE username = '{$person}'"; 
$result = mysql_query($sql) or die(mysql_error());
 
// stuff that should happen if it's someone elses profile.

 } else {
 


function birthday ($age){ 
     // assumes $birthdate is in YYYY-MM-DD format 
   list($dob_year, $dob_month, $dob_day) = explode('-', $age); 
   // determine current year, month, and day 
   $cur_year  = date('Y'); 
   $cur_month = date('m'); 
   $cur_day  = date('d'); 
   // either past or on the birthday 
   if($cur_month >= $dob_month && $cur_day >= $dob_day) { 
       $age1 = $cur_year - $dob_year; 
   } 
   // before the birthday 
   else { 
       $age1 = $cur_year - $dob_year; 
   } 
   // and your done 
   return $age1; 
  } 
  
  function fodelsedag($age){ 
     // assumes $birthdate is in YYYY-MM-DD format 
   list($dob_year, $dob_month, $dob_day) = explode('-', $age); 
   // determine current year, month, and day 
   $cur_year  = date('Y'); 
   $cur_month = date('m'); 
   $cur_day  = date('d'); 
   // either past or on the birthday 
   if($cur_month == $dob_month || $cur_day == $dob_day) { 
       $age2 = "Det är denna användares födelsedag idag  GRATTIS!"; 
	      return $age2; 
   } 
    
   // and your done 
  } 
while($rad = mysql_fetch_array($result2)) 
{ 
 
   $header = $person2;
   $header .= "&nbsp"; 
   $header .= $rad['sex']; 
   $header .= birthday("$age"); 
 
$age = $rad['age']; 
$kon = $rad['sex']; 

$content = "<table>";
$content .= "<tr>";
$content .= "<td><b><font size='4'>". $rad['username']."</b>&nbsp";
$content .=  $rad['sex'] . birthday("$age") . "<br>" . "</td>";
$content .= "</tr>";

$content .= "<tr>";
$content .= "<td>";
$content .= "<br>";
   if(empty($rad['picture'])) 
   { 
      $content .= "<img src='images/inget-foto.jpg' width='50' height='50'>"; 
   } 
   else 
   { 
      $content .= "<img src='images/". $rad['picture']."'>"; 
   } 
$content .= "<br>";
$content .= "</td></tr>";

$content .= "<tr>";
$content .= "<td width=220><b>Användarnamn:</b>".$rad['username']." </td><td width=50</td><td width=150><b>Civilstånd:</b> </td><td>".$rad['civil']."</td>";
$content .= "</tr>";

$content .= "<tr>";
$content .= "<td width=220><b>Län:</b> ".$rad['lan']."</td><td width=50></td><td width=150><b>Sexuell läggning:</b></td><td>".$rad['sexuel']."</td>";
$content .= "</tr>";

$content .= "<tr>";
$content .= "<td width=220><b>Längd:</b> ".$rad['height']."</td><td width=50></td><td width=150><b>Vikt:</b></td><td>".$rad['weight']."</td>";
$content .= "</tr>";

$content .= "<tr>";
$content .= "<td width=220><b>Ögonfärg:</b> ".$rad['eye_color']."</td><td width=50></td><td width=150><b>Hårfärg:</b></td><td>".$rad['hair_color']."</td>";
$content .= "</tr>";

$content .= "</table>";
$content .= "<table>";
$content .= "<td><br><br></td>";
$content .= "<td width=100><b>Gästbok</b></td><td width=100><b>Film Galleri</b></td><td width=100><b>Bild Galleri</b></td><td width=100><b>Gör favorit</b></td><td width=100><b>Blog</b></td>";
$content .= "</tr>";
$content .= "</table>";
   }
   }
 ?>
Thanks

Posted: Wed Aug 02, 2006 1:11 pm
by RobertGonzalez
See how $content is being set inside the while loop? If that while doesn't execute, $content never gets made. So you can't compare it to anything because according to PHP it is not defined yet. Before your while loop, set $content = ''; and the error goes away.

This is refered to as initializing your variables.

Posted: Wed Aug 02, 2006 2:59 pm
by NiGHTFiRE
Thanks now i don't get that error. But why isn't the $content code getting executed? Even if you check your own profile?
Thanks

Posted: Wed Aug 02, 2006 3:47 pm
by RobertGonzalez
There is something going on with this...

Code: Select all

while($rad = mysql_fetch_array($result2))
I would guess that that $result2 is either having problems or is returning as a zero length result. Maybe you can do a row count before you do the while.

Code: Select all

if (mysql_num_rows($result2))
{
    while(...)

Posted: Wed Aug 02, 2006 4:04 pm
by NiGHTFiRE
Hmm wierd.
I did that now. And now it didn't return anything. So i guess that it returns zero?
Wonder why, since i've populated the table.

Posted: Wed Aug 02, 2006 4:08 pm
by RobertGonzalez
Run the query in your db utility (phpMyAdmin, etc) and see what is returned. I can tell you this much, $result2 the way it is run right now, has no results.

Posted: Wed Aug 02, 2006 4:13 pm
by NiGHTFiRE
Then i'll need to replace $_SESSION['sess_user'] to a real username.


EDIT: Did that and I got results. Maybe the $_SESSION['sess_user'] has a value of zero.

EDIT: Nope, got the right value

Posted: Wed Aug 02, 2006 7:35 pm
by RobertGonzalez
Try this as your query...

Code: Select all

$sql2 = "SELECT * FROM members WHERE username = '$person2'";

Posted: Thu Aug 03, 2006 1:46 am
by NiGHTFiRE
Well when i try the hole

Code: Select all

$sql2 = "SELECT * FROM members WHERE username = '$person2'";
i get an error since it's php code.

Posted: Thu Aug 03, 2006 11:48 am
by NiGHTFiRE
*bump*

Posted: Thu Aug 03, 2006 12:53 pm
by RobertGonzalez
NiGHTFiRE wrote:Well when i try the hole

Code: Select all

$sql2 = "SELECT * FROM members WHERE username = '$person2'";
i get an error since it's php code.
Explain the error. Are you trying to run it through phpMyAdmin? If so, just run the query part.

Posted: Thu Aug 03, 2006 2:13 pm
by NiGHTFiRE
When i run the query with $person2 i get no results(...) but no errors as well. But when i run it with a username that excists it shows all information. Kinda wierd that my code doesn't work

Posted: Thu Aug 03, 2006 4:07 pm
by RobertGonzalez
Where is $person2 set? Also, echo out the $person2 var prior to hitting the db with it to see what the db server is seeing.

Posted: Fri Aug 04, 2006 11:24 am
by NiGHTFiRE
$person2 is set before the first if.
I'm gonna give you all my code so you can see if i've done any stupid things that can make this error.
Though i think it's because of this: When you want to go to a persons profile, not your own you go to profile.php?=_username_ but when you go to your profile you just go to profile.php.

So could that be the thing causing this error?

Code: Select all

<?php 
session_start(); // Alltid överst på sidan 
error_reporting(E_ALL);
include "configs.php"; // Databasanslutningen 
 
// Kolla om inloggad = sessionen satt
if (!isset($_SESSION['sess_user'])) {
  header("Location: index.php");
  exit;
}
?>
<?php 
$person2 = $_SESSION['sess_user'];
$sql2 = "SELECT * FROM members WHERE username = '{$_SESSION['sess_user']}'"; 
$result2 = mysql_query($sql2) or die(mysql_error());


if(isset($_SERVER['QUERY_STRING'])) {
$person = substr($_SERVER['QUERY_STRING'], 1);
 
$sql = "SELECT * FROM members WHERE username = '{$person}'"; 
$result = mysql_query($sql) or die(mysql_error());
 
 
 
function birthday ($age){ 
     // assumes $birthdate is in YYYY-MM-DD format 
   list($dob_year, $dob_month, $dob_day) = explode('-', $age); 
   // determine current year, month, and day 
   $cur_year  = date('Y'); 
   $cur_month = date('m'); 
   $cur_day  = date('d'); 
   // either past or on the birthday 
   if($cur_month >= $dob_month && $cur_day >= $dob_day) { 
       $age1 = $cur_year - $dob_year; 
   } 
   // before the birthday 
   else { 
       $age1 = $cur_year - $dob_year; 
   } 
   // and your done 
   return $age1; 
  } 
  
  function fodelsedag($age){ 
     // assumes $birthdate is in YYYY-MM-DD format 
   list($dob_year, $dob_month, $dob_day) = explode('-', $age); 
   // determine current year, month, and day 
   $cur_year  = date('Y'); 
   $cur_month = date('m'); 
   $cur_day  = date('d'); 
   // either past or on the birthday 
   if($cur_month == $dob_month || $cur_day == $dob_day) { 
       $age2 = "Det är denna användares födelsedag idag  GRATTIS!"; 
	      return $age2; 
   } 
    
   // and your done 
  } 
  $content = "<table>"; 
  while($rad = mysql_fetch_array($result)) 
{ 
 
   $header = $person;
   $header .= "&nbsp"; 
   $header .= $rad['sex']; 
   $header .= birthday("$age"); 
   $age = $rad['age']; 
   $kon = $rad['sex'];
   
   $content .= "<tr><td>"; 
   $content .= "<b><font color='orange'>". $rad['username']."</b>&nbsp;"; 
   $content .= $rad['sex'] . birthday("$age") . "<br>"; 
   $content .= "</td></tr>"; 
   $content .= "<tr><td><br>"; 
   if(empty($rad['picture'])) 
   { 
      $content .= "<img src='images/inget-foto.jpg' width='50' height='50'>"; 
   } 
   else
   { 
      $content .= "<img src='images/". $rad['picture']."'>"; 
   } 
   $content .= "<br><br>"; 
   $content .= fodelsedag("$age"); 
   $content .= "<br><br><b>Användarnamn</b> ".$rad['username']."<br /><b>Ålder:</b> ".birthday("$age")." år<br /><b>Jag är/ vi är:</b> ". $kon ."<br />"; 
   $content .= "</td></tr><tr><td>"; 
   $content .= "<b>Presentation:</b><br> ".$rad['information'].""; 
   $content .= "</td></tr></table><br><br>"; 
 }
 
 
 
 } else  if(isset($_SERVER['PHP_SELF']))  {
 echo "jadu borde funka....";
 } else {


function birthday ($age){ 
     // assumes $birthdate is in YYYY-MM-DD format 
   list($dob_year, $dob_month, $dob_day) = explode('-', $age); 
   // determine current year, month, and day 
   $cur_year  = date('Y'); 
   $cur_month = date('m'); 
   $cur_day  = date('d'); 
   // either past or on the birthday 
   if($cur_month >= $dob_month && $cur_day >= $dob_day) { 
       $age1 = $cur_year - $dob_year; 
   } 
   // before the birthday 
   else { 
       $age1 = $cur_year - $dob_year; 
   } 
   // and your done 
   return $age1; 
  } 
  
  function fodelsedag($age){ 
     // assumes $birthdate is in YYYY-MM-DD format 
   list($dob_year, $dob_month, $dob_day) = explode('-', $age); 
   // determine current year, month, and day 
   $cur_year  = date('Y'); 
   $cur_month = date('m'); 
   $cur_day  = date('d'); 
   // either past or on the birthday 
   if($cur_month == $dob_month || $cur_day == $dob_day) { 
       $age2 = "Det är denna användares födelsedag idag  GRATTIS!"; 
	      return $age2; 
   } 
    
   // and your done 
  } 
  $content = "<table>";
  
  if(mysql_num_rows($result2)) {
  
while($rad = mysql_fetch_array($result2)) 
{ 
 
   $header = $person2;
   $header .= "&nbsp"; 
   $header .= $rad['sex']; 
   $header .= birthday("$age"); 
 
$age = $rad['age']; 
$kon = $rad['sex']; 

$content .= "<tr>";
$content .= "<td><b><font size='4'>". $rad['username']."</b>&nbsp";
$content .=  $rad['sex'] . birthday("$age") . "<br>" . "</td>";
$content .= "</tr>";

$content .= "<tr>";
$content .= "<td>";
$content .= "<br>";
   if(empty($rad['picture'])) 
   { 
      $content .= "<img src='images/inget-foto.jpg' width='50' height='50'>"; 
   } 
   else 
   { 
      $content .= "<img src='images/". $rad['picture']."'>"; 
   } 
$content .= "<br>";
$content .= "</td></tr>";

$content .= "<tr>";
$content .= "<td width=220><b>Användarnamn:</b>".$rad['username']." </td><td width=50</td><td width=150><b>Civilstånd:</b> </td><td>".$rad['civil']."</td>";
$content .= "</tr>";

$content .= "<tr>";
$content .= "<td width=220><b>Län:</b> ".$rad['lan']."</td><td width=50></td><td width=150><b>Sexuell läggning:</b></td><td>".$rad['sexuel']."</td>";
$content .= "</tr>";

$content .= "<tr>";
$content .= "<td width=220><b>Längd:</b> ".$rad['height']."</td><td width=50></td><td width=150><b>Vikt:</b></td><td>".$rad['weight']."</td>";
$content .= "</tr>";

$content .= "<tr>";
$content .= "<td width=220><b>Ögonfärg:</b> ".$rad['eye_color']."</td><td width=50></td><td width=150><b>Hårfärg:</b></td><td>".$rad['hair_color']."</td>";
$content .= "</tr>";

$content .= "</table>";
$content .= "<table>";
$content .= "<td><br><br></td>";
$content .= "<td width=100><b>Gästbok</b></td><td width=100><b>Film Galleri</b></td><td width=100><b>Bild Galleri</b></td><td width=100><b>Gör favorit</b></td><td width=100><b>Blog</b></td>";
$content .= "</tr>";
$content .= "</table>";
   }
   }
   }
 ?>
Either way, i'm gonna try a diffrent way now.
Thanks

Posted: Fri Aug 04, 2006 11:53 am
by RobertGonzalez
What I would do is set profile.php to default to the current user. If there is a querystring var called 'user', then change the data fetched to that user. I would really recommend sanitizing your query string data just to validate that it is of the form and format that you expect.