I am brand new to PHP and I need to know why this doesn't work.
[code]
$result = mysql_query("SELECT id_group FROM smf_members WHERE member_name = '$user->username'");
if (!$result)
{
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($result);
function getgroup()
{
if ($row[0] == "1")
{
return "Owner";
}
else
{
return "bullshit";
}
}
echo ("<h1>Current Level</h1>");
echo (getgroup());
[/code]
Even though my group id is "1" in the sql table the function returns "bullshit" every time. Im sure its something dumb and I've been googling for a while now with no luck.
try print_r( $row ) and see the structure
It printed this "Array ( [0] => 1 )"...I'm not sure what to do with that.
is this in a class? i just noticed you have a function getGroup which uses the global $row without declaring it global.
try to put it like
function getgroup()
{
global $row;
if ($row[0] == "1")
{
Still the same...returns "bullshit".
[editline]12th June 2012[/editline]
Got it to work by removing the function and using a variable instead...like so.
[code]
$result = mysql_query("SELECT id_group FROM smf_members WHERE member_name = '$user->username'");
if (!$result)
{
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($result);
if ($row[0] == "1")
{
$groupid = "Owner";
}
else
{
$groupid = "bullshit";
}
echo ("<h1>Username</h1>");
echo nl2br("$user->username\n\n");
echo ("<h1>Current Level</h1>");
echo nl2br($groupid);
?>
[/code]
You need to review variable scope. Inside a function. $row is not defined, unless you pass it as a parameter or declare it. Also mysql_fetch_assoc() is a little handier then mysql_fetch_row() because it will have Array keys as the column string.
[URL="http://ca3.php.net/manual/en/language.variables.scope.php"]Variable Scope[/URL]
[URL="http://ca3.php.net/manual/en/function.mysql-fetch-assoc.php"]mysql_fetch_assoc()[/URL]
[QUOTE=Topgamer7;36304336]You need to review variable scope. Inside a function. $row is not defined, unless you pass it as a parameter or declare it. Also mysql_fetch_assoc() is a little handier then mysql_fetch_row() because it will have Array keys as the column string.
[URL="http://ca3.php.net/manual/en/language.variables.scope.php"]Variable Scope[/URL]
[URL="http://ca3.php.net/manual/en/function.mysql-fetch-assoc.php"]mysql_fetch_assoc()[/URL][/QUOTE]
What is the difference between mysql_fetch_assoc and mysql_fetch_array? I thought mysql_fetch_array was associative as well. PHP and its damn ambiguity...
[QUOTE=Overv;36304555]What is the difference between mysql_fetch_assoc and mysql_fetch_array? I thought mysql_fetch_array was associative as well. PHP and its damn ambiguity...[/QUOTE]
fetch_array you can select whether you want numeric, associative, or both. fetch_assoc is just fetch_array with associated keys. He's using mysql_fetch_row which is essentially fetch_array with numeric set if I recall.
If you're working with a query with like 40 columns like wordpress or other software, its probably much easier to go by the column name in your result then some random number. :P