Page 1 of 1
mysql query inside a function
Posted: Wed Oct 01, 2008 2:38 pm
by sHobbsWW
What I am trying to do is execute a function. The function will (when parased) set the $query variables to do what is needed. Here is the function::
Code: Select all
function selAccountLogins(){
$query = "SELECT user_login FROM userAccounts ORDER by user_login ASC";
$querying = mysql_query($query) or die(mysql_error());
$i = 0;
while ($row = mysql_fetch_array($querying)) {
$user[$i] = $row["user_login"];
$i++;
}
}
^
That code is being included.php, however it will not work. If I cut the code out of the function and just paste it into the page between <?php ?> it works fine.
Is this the wrong way about going about it?
The actually page being displayed has this in it::
Code: Select all
<?php
selAccountLogins();
echo "$editAccountPassWord";
echo "test : $user[1]";
?>
Re: mysql query inside a function
Posted: Wed Oct 01, 2008 4:49 pm
by koen.h
The scope of the variable $user in your function is limited to that function. You might want to read up on variable scoping in PHP. A possibility is returning $user from the function and using this return value.
Re: mysql query inside a function
Posted: Thu Oct 02, 2008 2:50 am
by papa
Code: Select all
<?php
function selAccountLogins(){
...
return $user;
}
$user = selAccountLogins();
echo "$editAccountPassWord";
echo "test : $user[1]";
?>
Something like that .
However you still need to pass the MySQL connection to your function so this won't work either.
Re: mysql query inside a function
Posted: Thu Oct 02, 2008 9:59 am
by sHobbsWW
Thanks, but in fact I did not need to extract the variables outside of it's local scope inside the function.
But I did learn something thank you.
But I found it easier to just to do this.
Code: Select all
function selAccountLogins(){
$query = "SELECT user_login FROM userAccounts ORDER by user_login ASC";
$querying = mysql_query($query) or die(mysql_error());
$i = 0;
while ($row = mysql_fetch_array($querying)) {
$user[$i] = $row["user_login"];
$i++;
}
echo "
<div id=\"selAccountName\">
<select name=\"accountSelection\">
<option value=$user[0]>$user[0]</option>
<option value=$user[1]>$user[1]</option>
<option value=$user[2]>$user[2]</option>
<option value=$user[3]>$user[3]</option>
</select>
</div>
";
}
Re: mysql query inside a function
Posted: Thu Oct 02, 2008 10:17 am
by papa
Code: Select all
function selAccountLogins(){
$query = "SELECT user_login FROM userAccounts ORDER by user_login ASC";
$querying = mysql_query($query) or die(mysql_error());
$i = 0;
echo "<div id=\"selAccountName\">\n",
"<select name=\"accountSelection\">\n";
while ($row = mysql_fetch_array($querying)) {
$user[$i] = $row["user_login"];
echo "<option value=".$user[$i].">".$user[$i]."</option>\n";
$i++;
}
echo "</select>\n",
"</div>\n";
}
In case you want to add more users and skip changing the code

Re: mysql query inside a function
Posted: Thu Oct 02, 2008 10:19 am
by koen.h
sHobbsWW wrote:Thanks, but in fact I did not need to extract the variables outside of it's local scope inside the function.
But I did learn something thank you.
But I found it easier to just to do this.
Code: Select all
function selAccountLogins(){
$query = "SELECT user_login FROM userAccounts ORDER by user_login ASC";
$querying = mysql_query($query) or die(mysql_error());
$i = 0;
while ($row = mysql_fetch_array($querying)) {
$user[$i] = $row["user_login"];
$i++;
}
echo "
<div id=\"selAccountName\">
<select name=\"accountSelection\">
<option value=$user[0]>$user[0]</option>
<option value=$user[1]>$user[1]</option>
<option value=$user[2]>$user[2]</option>
<option value=$user[3]>$user[3]</option>
</select>
</div>
";
}
That's a possibility too. The reason we talked about returning the result from the function is that generally you want to try to avoid mixing PHP with HTML as much as possible. Mixing the two is what is called spaghetti code and leads to your code and layout much more difficult to change afterwards. Also, suppose you would want to reuse your function but with a slightly different HTML output. You would have to write the same function again with the different HTML inside, meaning you have the same PHP code twice.