0

I am having a real hard time fetching a value from my sql database. My code is below and I am trying to display $balance, but when I echo it, it just shows "Array".

Here is an image of my database table: database table

<?php 
session_start();
echo "<!DOCTYPE html>\n<html><head><script src='OSC.js'></script>";
include 'functions.php';

$userstr = ' (Guest)';

if (isset($_SESSION['user']))
{
    $user     = $_SESSION['user'];
    $loggedin = TRUE;
    $userstr  = " ($user)";
    $balancequery = mysql_query("SELECT balance FROM members WHERE user = '$user'"); 
    $balance = mysql_fetch_assoc($balancequery); 
}
else $loggedin = FALSE;

echo "<title>$appname$userstr</title><link rel='stylesheet' " .
     "href='styles.css' type='text/css' />" .
     "</head><body><div class='appname'>$appname$userstr</div>";

if ($loggedin)
{
    echo "<br ><ul class='menu'>" .
         "<li><a href='members.php?view=$user'>Home</a></li>" .
         "<li><a href='members.php'>Members</a></li>" .
         "<li><a href='stockdata.php'>Stocks</a></li>" .
         "<li><a href='logout.php'>Log out</a></li></ul><br />";
}
else
{
    echo ("<br /><ul class='menu'>" .
         "<li><a href='index.php'>Home</a></li>" .
         "<li><a href='signup.php'>Sign up</a></li>" .
         "<li><a href='login.php'>Log in</a></li></ul><br />" .
         "<span class='info'>&#8658; You must be logged in to " .
         "view this page.</span><br /><br />");
}
?>
6
  • What is print_r($balance) displaying you ? By the way, mysql_query is depreciated. Commented Apr 29, 2013 at 21:28
  • that's because $balance IS an array. try using print_r($balance) and see what comes out. and search for php arrays to learn how to echo a value from within the array. Commented Apr 29, 2013 at 21:29
  • @RelevantUsername that gave me "Array ( [balance] => 1000000 )" Commented Apr 29, 2013 at 21:33
  • @jimmy Why is this created as an array? Is there a way to just get the value itself? Commented Apr 29, 2013 at 21:33
  • Then access the value using $balance['balance']; Commented Apr 29, 2013 at 21:34

2 Answers 2

2

mysql_fetch_assoc returns an associative array of your fields for that row. To echo a field within that row, you must give it the proper index, which will be the name of the field:

echo $balance["balance"];

Also the mysql php extension is deprecated. Please look into MySQLi or PDO_MySQL.

Sign up to request clarification or add additional context in comments.

Comments

1

This is because you are trying to echo an associatove array, wich does not work. Try to print as value by indexing your array

echo $balance['balance'];  //this is just array_name['column_name']

I would like to remember you that mysql_ functions are deprecated so i would advise you to switch to mysqli or PDO.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.