Jump to content

PHP MySql query


Cagecrawler

Recommended Posts

My code
[code]<?php
include("include/connect.php");
$user_level = mysql_query("SELECT user_level FROM users WHERE username='$username'");
$user_level = mysql_fetch_assoc($user_level);
echo $user_level;
if ($user_level == 2)
{
echo("<a href=\"admin/admin.php\" target=\"main\">Admin</a><br/>");
}
?>[/code]

When the script is run, I get the word 'Array' from echo $user_level, rather than '2', the user's level.  This means that the IF function is false and the link is not displayed.  The echo is in there just to see what I'm outputting while writting the script.  Why does it not output the right thing?
Link to comment
https://forums.phpfreaks.com/topic/27368-php-mysql-query/
Share on other sites

That's because you need to pull the correct field from the array.
try this
[code]<?php
include("include/connect.php");
$user_level = mysql_query("SELECT user_level FROM users WHERE username='$username'") or die (mysql_error());
$row = mysql_fetch_assoc($user_level);
echo $row['user_level'];
if ($row['user_level'] == 2)
{
echo("<a href=\"admin/admin.php\" target=\"main\">Admin</a><br/>");
}
?>[/code]

What this does is put the returning row into an array called $row. Then you call on your row by putting the row name between the brackets ie $row['user_level'].

Ray
Link to comment
https://forums.phpfreaks.com/topic/27368-php-mysql-query/#findComment-125152
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.