Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notice: Array to string conversion in

I'm trying to select a value from a database and display it to the user using SELECT. However I keep getting this error:

Notice: Array to string conversion in (pathname) on line 36.

I thought that the @mysql_fetch_assoc(); would fix this but I still get the notice. This is the part of the code where I'm getting the error:

  {
  $loggedin = 1;

  $get = @mysql_query("SELECT money FROM players WHERE username = 
 '$_SESSION[username]'");
  $money = @mysql_fetch_assoc($get);

  echo '<p id= "status">'.$_SESSION['username'].'<br>
  Money: '.$money.'.
  </p>';
  }

What am I doing wrong? I'm pretty new to PHP.

like image 258
Sunden Avatar asked Jul 21 '12 23:07

Sunden


People also ask

How do I fix notice array to string?

The five ways to solve this error are as follows:Using Builtin PHP Function print_r. Using Built-in PHP Function var_dump. Using PHP Inbuilt Function implode() to Convert Array to String. Using Foreach Loop to Print Element of an Array.

Which function converts array into string?

Using implode() Function By using the implode() function, we can convert all array elements into a string. This function returns the string.

Is PHP an array?

Definition and Usage. The is_array() function checks whether a variable is an array or not. This function returns true (1) if the variable is an array, otherwise it returns false/nothing.


2 Answers

The problem is that $money is an array and you are treating it like a string or a variable which can be easily converted to string. You should say something like:

 '.... Money:'.$money['money']
like image 67
Razvan Avatar answered Oct 24 '22 05:10

Razvan


Even simpler:

$get = @mysql_query("SELECT money FROM players WHERE username = '" . $_SESSION['username'] . "'");

note the quotes around username in the $_SESSION reference.

like image 1
user1046243 Avatar answered Oct 24 '22 05:10

user1046243