-2
$bankquery = mysql_query($con, "SELECT bank FROM users WHERE username = $theUser");
$cashquery = mysql_query($con, "SELECT cash FROM users WHERE username = $theUser");
$bank = mysql_query($bankquery);
$cash = mysql_query($cashquery);

I'm trying to assign a column value from my DB to a php variable. But it gives the error: mysql_query() expects parameter 1 to be string, object given in ///

llrs
  • 3,203
  • 33
  • 63
user3324006
  • 11
  • 1
  • 2
  • Wrap it in quotes? `"SELECT cash FROM users WHERE username = '$theUser'"`. What's the content of `$theUser`? Also you can do those 2 queries in 1 query, annnnd don't use `mysql_` functions. – MLeFevre Feb 18 '14 at 15:11
  • [`mysql_query ( string $query [, resource $link_identifier = NULL ] )`](http://www.php.net/mysql_query), not the other way around. Notice the red (well, pink..) yelling text on that page about it being a bad idea to use `mysql_*` functions anymore. Or are you mixing up your `mysql_` & `mysqli_` calls? – Wrikken Feb 18 '14 at 15:12
  • RTFM or introductory tutorial on php/mysql. – Mike B Feb 18 '14 at 15:13
  • 1
    You should use PDO or MySQLi instead of mysql_ functions, they've been deprecated. Make sure $theUser has been sanitized with something like mysql_real_escape_string before inserting in a query –  Feb 18 '14 at 15:16

5 Answers5

1

The correct syntax is:

 mysql_query ( "SELECT bank FROM users WHERE username = $theUser", $con )

Check the php manual

Ganz
  • 187
  • 5
0

You have placed the connection as the first parameter... it should be the second one...

Reference: http://in3.php.net/mysql_query

resource mysql_query ( string $query [, resource $link_identifier = NULL ] )

$bankquery = mysql_query("SELECT bank FROM users WHERE username = $theUser", $con);
$cashquery = mysql_query("SELECT cash FROM users WHERE username = $theUser", $con);
$bank = mysql_query($bankquery);
$cash = mysql_query($cashquery);
gurudeb
  • 1,746
  • 21
  • 29
0

mysql_query expects first parametr to be string. There is no need to pass connection. Hovewer, use mysqli instead (mysqli_query)

Correct should be

mysql_query("SELECT bank FROM users WHERE username = '$theUser'", $con);
Martin Perry
  • 8,795
  • 8
  • 46
  • 106
0

You have a few issues here.

The first is that you have your parameters backwards in your mysql_query() call - the "query" goes first, then the connection object:

$bankquery = mysql_query("SELECT bank FROM users WHERE username = $theUser", $con);
$cashquery = mysql_query("SELECT cash FROM users WHERE username = $theUser", $con);

The second is the statements immediately following these:

$bank = mysql_query($bankquery);
$cash = mysql_query($cashquery);

They don't do anything (except error); the original $bankquery and $cashquery values are the return-results of the mysql_query() results. You don't have to run them again.

A third issue is that you're not sanitizing the $theUser variable and you're using it directly in your queries (not surrounded by quotes). Without them, unless you have numeric values, you'll receive SQL errors. Try:

$theUser = mysql_real_escape_string($theUser);
$bankquery = mysql_query("SELECT bank FROM users WHERE username = '" . $theUser . "'", $con);
newfurniturey
  • 35,828
  • 9
  • 90
  • 101
0

These entire script is not correct

$bankquery = mysql_query($con, "SELECT bank FROM users WHERE username = $theUser");
$cashquery = mysql_query($con, "SELECT cash FROM users WHERE username = $theUser");
$bank = mysql_query($bankquery);
$cash = mysql_query($cashquery);

First thing -

mysql_query() definition says

mysql_query ( string $query [, resource $link_identifier = NULL ] )

http://in1.php.net/mysql_query

So the first parameter is string your query followed by connection.Also you are missing the quotes for the string so the correct one is

$bankquery = mysql_query("SELECT bank FROM users WHERE username = '$theUser'",$con);
$cashquery = mysql_query("SELECT cash FROM users WHERE username = '$theUser'",$con);

Second thing is

$bank = mysql_query($bankquery);
$cash = mysql_query($cashquery);

What you are trying here is doing mysql_query() to a resource from previous query which is completely wrong. If you want to fetch the result you may need to use mysql_fetch_array() or mysql_fetch_assoc()

Third thing :

mysql_*() functions are deprecated so start using mysqli_* functions or PDO

Abhik Chakraborty
  • 43,914
  • 5
  • 48
  • 61