0
$fetch=mysql_fetch_assoc(mysql_query("select * from users")); 

Can we use these type of fetch statements?

Is this a proper way of using it?

I the following warning:

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given

Paul
  • 437
  • 5
  • 15
  • 1. Consider not using `mysql`. 2. It's a bad practice. 3. `mysql_query` might return a boolean value (`false`) on error - so in this format you won't have any way of how to deal with it. – Ofir Baruch Jun 24 '15 at 10:38
  • *Is this a proper way of using it...?? I get warning* ... I think the answer to that, lies in the question. If you're getting a warning then no, it's not the proper way of using it. There are innumerable PHP tutorials online to get you started... look for a newer one that doesn't use the deprecated `mysql_*` extension – CD001 Jun 24 '15 at 10:39
  • You get warning because it returns 0 if no results are there – Varun Garg Jun 24 '15 at 10:40
  • @UzumakiIchigo, please notice that `false` (`0`) is being returned on error and not on `no results`. – Ofir Baruch Jun 24 '15 at 10:42

3 Answers3

0

Php is a multi-returnable language so that mysql_query return the array and the boolean (if query worked well or not). In this case youre pushing php to check boolean type but if you assign it before it will check either the ways. So go with;

$result = mysql_query("select * from users");
$fetch = mysql_fetch_assoc($result);
0

Yes, this will work!! But in case when there are multiple rows of record returned by the query, it will consider the first row only!

Abhi
  • 104
  • 5
0

You can use mysql_query but it is a deprecated function check here

I suggest you to go with PDO or mysqli which is more advanced and Object oriented more malicious (sql injection) proof and easy to learn

keviveks
  • 308
  • 3
  • 16