Jump to content

The php code works but i have no variables returned


Frank74

Recommended Posts

Hello,  :)

 

I'm new on this forum and i have not seen the rules to post a code, so i will try to explain my best :

 

I have learned the basics of php during the past few days and i have a php code that is supposed to read parameters in several fields of a Mysql database and to return those parameters as variables in an array. (well that's how i understand it... please correct me if i'm wrong)

 

So here the code and the part that doesn't seem to work properly (colored in red) :

//I have changed the following values which are confidential...
$DBName = "MyDatabase";
$DBHostName = "MyMysqlServer";
$DBUserName = "MyUsername";
$DBPassword = "MyPassword";
$Table = "MyTable";
//The fields which are in MyTable :
// MemberID SMALLINT
// MemberName VARCHAR(20)
// MemberPassword VARCHAR(20)
// MemberEmailAddress VARCHAR(50)
// MemberDateTimeInscription VARCHAR(19)
//Reading member parameters
  echo"<br>Reading the parameters of a member.";
  echo"<br>Defining the parameters of the member to read.";
  $CurrentName = "Ashley";
  $CurrentPassword = "65hl3y";
  $CurrentEmailAddress = "Ashley@HisDomain.com";
  echo"<br>Trying to start a connection with the Mysql server.";
  mysql_connect($DBHostName,$DBUserName,$DBPassword) OR DIE(mysql_error());
  echo"<br>Selecting the table.";
  mysql_select_db($DBName) OR DIE(mysql_error());
  echo"<br>Searching for the fields corresponding to the CurrentName.";
  $Query = "SELECT * FROM ".$Table." WHERE MemberName = '".$CurrentName."'"; 
  $Result = mysql_query($Query) or die(mysql_error());
  echo"<br>Returning the parameters stored in the fields.";
  while($Row = mysql_fetch_array($Result,MYSQL_ASSOC)){
   /////This is the start of the part that does not seem to work properly.
   $MemberId = $row["MemberId"];
   $MemberName = $row["MemberName"];
   $MemberPassword = $row["MemberPassword"];
   $MemberEmailAddress = $row["MemberEmailAddress"];
   $MemberDateTimeInscription = $row["MemberDateTimeInscription"];
   echo"<br>MemberId : ".$MemberId;
   echo"<br>MemberName : ".$MemberName;
   echo"<br>MemberPassword : ".$MemberPassword;
   echo"<br>MemberEmailAddress : ".$MemberEmailAddress;
   echo"<br>MemberDateTimeInscription : ".$MemberDateTimeInscription;
  /////This is the end of the part that does not seem to work properly.  
  }
  echo"<br>Ending the connection with the Mysql server.";
  mysql_close();

 

All of echo are here for debug, i'm a beginner with php.

 

This is what the php page shows :

Reading the parameters of a member.

Defining the parameters of the member to read.

Trying to start a connection with the Mysql server.

Selecting the table.

Searching for the fields corresponding to the CurrentName.

Returning the parameters stored in the fields.

MemberId :

MemberName :

MemberPassword :

MemberEmailAddress :

MemberDateTimeInscription :

Ending the connection with the Mysql server.

 

I don't understand why the variables $MemberId, $MemberName, $MemberPassword, $MemberEmailAddress, $MemberDateTimeInscription are empty.

 

Your advices are welcome,

Thanks,

Link to comment
Share on other sites

Here is the issue:

 

while($Row = mysql_fetch_array($Result,MYSQL_ASSOC)){   /////This is the start of the part that does not seem to work properly.   $MemberId = $row["MemberId"];

 

Your result set is assigned to $Row, and your trying to echo out the elements of $row (notice the casing of the letter R).

Link to comment
Share on other sites

Huff has it right.

 

You should be running with error reporting at max and display errors on. (this can be set in php.ini)

 

Setting it at runtime (each script)  place this block at the top of the script.

error_reporting(-1);
ini_set('display_errors',1);

 

This would have told you that $row did not exist.

 

PS. welcome to the forum, let us know if an answer given doesn't work, we always check back. Otherwise, solved button is on lower right.  :-*

Link to comment
Share on other sites

Hi :)

 

It was indeed the "r" of "$row" which should be Uppercase.

 

I will remind this lesson thanks ;)

 

 

 

Setting it at runtime (each script)  place this block at the top of the script.

 

error_reporting(-1);

ini_set('display_errors',1);

 

 

This would have told you that $row did not exist.

 

Thanks for the trick!

 

 

 

Otherwise, solved button is on lower right.

 

I have found the solved button on the lower left :confused:

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.