Notice: Array to string conversion in

2020-01-23 12:15发布

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.

6条回答
Fickle 薄情
2楼-- · 2020-01-23 12:25

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']
查看更多
放荡不羁爱自由
3楼-- · 2020-01-23 12:29

You cannot echo an array. Must use print_r instead.

<?php
$result = $conn->query("Select * from tbl");
$row = $result->fetch_assoc();
print_r ($row);
?>
查看更多
干净又极端
4楼-- · 2020-01-23 12:32

Even simpler:

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

note the quotes around username in the $_SESSION reference.

查看更多
仙女界的扛把子
5楼-- · 2020-01-23 12:46

Store the Value of $_SESSION['username'] into a variable such as $username

$username=$_SESSION['username'];

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

it should work!

查看更多
该账号已被封号
6楼-- · 2020-01-23 12:50

One of reasons why you will get this Notice: Array to string conversion in… is that you are combining group of arrays. Example, sorting out several first and last names.

To echo elements of array properly, you can use the function, implode(separator, array) Example:

implode(' ', $var)

result:

first name[1], last name[1]
first name[2], last name[2]

More examples from W3C.

查看更多
劫难
7楼-- · 2020-01-23 12:52

mysql_fetch_assoc returns an array so you can not echo an array, need to print_r() otherwise particular string $money['money'].

查看更多
登录 后发表回答