Easy A
Would you like to react to this message? Create an account in a few clicks or log in to continue.


 
HomeHome  Latest imagesLatest images  SearchSearch  RegisterRegister  Log inLog in  

Share
 

 PHP variable issue with database

View previous topic View next topic Go down 
AuthorMessage
Axe
Member
Member
avatar

Posts : 13
Join date : 2011-04-08

PHP variable issue with database Empty
PostSubject: PHP variable issue with database   PHP variable issue with database EmptyFri Jun 17, 2011 2:39 am

I'm having trouble getting my $X $Y and $Z variables declared and usable. The mySQL information has been changed for protection, but rest assure that's all working. So here are the two functions from the code.
Code:

<?php
function doSomething()
{
$con = mysql_connect("localhost","hostname","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("database", $con);
$result = mysql_query("SELECT * FROM Users WHERE thisField='1'");
while($row = mysql_fetch_array($result))
  {
  $X=$row['X'];
  $Y=$row['Y'];
  $Z=$row['Z'];
  thisUser();
  }
mysql_close($con);
}
function thisUser()
{
echo $X;
echo $Y;
echo $Z;
}
?>

So when I execute this, nothing is echoed. Any ideas?

Also, a second question for bonus points. My code has an ample amount of mysql_connect and mysql_close. Would this be more/less efficient or equal as opening from the start and not closing til the end/indefinitely?
Back to top Go down
System
Member
Member
System

Posts : 3
Join date : 2011-05-11

PHP variable issue with database Empty
PostSubject: Re: PHP variable issue with database   PHP variable issue with database EmptyFri Jun 17, 2011 2:40 am

Variables declared inside of a function are scoped to that function. You need to declare them before you run the function or make them global. This manual page explains it pretty clearly.
Back to top Go down
Axe
Member
Member
avatar

Posts : 13
Join date : 2011-04-08

PHP variable issue with database Empty
PostSubject: Re: PHP variable issue with database   PHP variable issue with database EmptyFri Jun 17, 2011 2:41 am

Ah yes, scope. I'm pretty sure the global keyword will resolve this one. Thank you
Back to top Go down
Skilletrockz
Super Moderator
Super Moderator
Skilletrockz

Posts : 45
Join date : 2011-05-11

PHP variable issue with database Empty
PostSubject: Re: PHP variable issue with database   PHP variable issue with database EmptyFri Jun 17, 2011 2:42 am

or instead of using global variables, another option is to pass the row to the function and then get the data you need from it and echo it.
Back to top Go down
Sponsored content




PHP variable issue with database Empty
PostSubject: Re: PHP variable issue with database   PHP variable issue with database Empty

Back to top Go down
 

PHP variable issue with database

View previous topic View next topic Back to top 
Page 1 of 1

 Similar topics

-
» Database access related issue
» Cfdiv Binding Variable with # sign Issue
» C / C++ code for Database connectivity
» [java] Share a variable?
» storing the data to database from input panel

Permissions in this forum:You cannot reply to topics in this forum
Easy A :: PHP variable issue with database Edit-trash Useless :: Trash-