1

Possible Duplicate:
PHP Error: mysql_fetch_array() expects parameter 1 to be resource, boolean given

I am coding an php and mysql program to add and retrieve facebook app id and secret in database.But I get the following error.My link is http://freegiveaway.info/new/manageApp.php .btw my database is connected successfully.

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, 
boolean given in /home/free/public_html/new/manageApp.php 

My php contains follwing code

$flag = $_POST['flag'];
        $key = $_POST['key'];
        $secret = $_POST['secret'];
        $deleteApp=$_POST['deleteApp'];
        $appN=$_POST['appN'];
    $db = mysql_connect("localhost", "free_1", "password");
    mysql_select_db("facefunv_chuck", $db);    
    $app = mysql_query("SELECT * FROM tsp ORDER BY n",$db);
        while ($apps = mysql_fetch_assoc($app)){

I get error on line while ($apps = mysql_fetch_assoc($app)){

And my sql file is below

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `facefunv_chuck`
--

-- --------------------------------------------------------

--
-- Table structure for table `tsp`
--

CREATE TABLE IF NOT EXISTS `tsp` (
  `n` int(11) NOT NULL AUTO_INCREMENT,
  `id` text NOT NULL,
  `secret` text NOT NULL,
  `restricted` int(11) NOT NULL,
  `banned` int(11) NOT NULL,
  PRIMARY KEY (`n`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=40 ;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
Community
  • 1
  • 1
vishnu
  • 869
  • 1
  • 16
  • 25
  • if `mysql_query` fails, it will return a boolean `false` instead of a resource. call mysql_error() to learn what went wrong. – Basti Feb 21 '12 at 18:32
  • add an `echo mysql_error()` before the while and post that as well. It looks like that mysql_query function is not returning a proper result, probably something with the query/db – Matt Dodge Feb 21 '12 at 18:33
  • i got this error Query failed: No database selected – vishnu Feb 21 '12 at 18:40
  • Access denied for user 'free_1'@'localhost' to database 'facefunv_chuck' – vishnu Feb 21 '12 at 18:47

3 Answers3

1

mysql_query() returns FALSE on error so your query is failing and you are still trying to fetch rows from it.

Try checking the return value first:

$app = mysql_query("SELECT * FROM tsp ORDER BY n",$db);
if (!$app) {
    die("Query failed: " . mysql_error());
}

while (($apps = mysql_fetch_assoc($app)) !== false) {
   //...
}
drew010
  • 68,777
  • 11
  • 134
  • 162
1

In short, mysql_query() returns a resource if the select succeeds and FALSE otherwise. You should check for that error in your code.

There could be several reasons, but the obvious one is that you connect to the database and mysql_select_db() the database "auto" where you try to select data from your table. According to your dump, the database is actually called "facefunv_chuck", not "auto".

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
  • i got this error Query failed: No database selected ... but i selected the database – vishnu Feb 21 '12 at 18:40
  • @JustinBiaber As I said in the answer, you're selecting the database named "auto" but the dump says your database is actually named "facefunv_chuck". – Joachim Isaksson Feb 21 '12 at 18:41
  • i changed to mysql_select_db("facefunv_chuck", $db); .. but still no database selected – vishnu Feb 21 '12 at 18:44
  • Access denied for user 'free_1'@'localhost' to database 'facefunv_chuck' – vishnu Feb 21 '12 at 18:47
  • @JustinBiaber You need to grant the user "free_1" access to the database. If you're logged in to the database as an admin, try `GRANT SELECT,INSERT,UPDATE,DELETE ON facefunv.* TO free_1@localhost` – Joachim Isaksson Feb 21 '12 at 18:53
  • when i logged in as admin in phpmyadmin ..again i got access denied error for GRANT SELECT,INSERT,UPDATE,DELETE ON facefunv.* TO free_1@localhost – vishnu Feb 21 '12 at 18:55
  • @JustinBiaber Sounds like your admin isn't a full-blown admin then :-/ Are you supposed to use the user 'free_1' against that database? – Joachim Isaksson Feb 21 '12 at 19:06
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/8031/discussion-between-justin-biaber-and-joachim-isaksson) – vishnu Feb 21 '12 at 19:07
1

You're changing the db name to "auto" with the mysql_select_db. I'd guess there is no database named auto with a table tsp. Hence the result $app is null and you can't call fetch on a null.

Ray
  • 40,256
  • 21
  • 101
  • 138
  • i changed the code now..but still no database error – vishnu Feb 21 '12 at 18:46
  • Access denied for user 'free_1'@'localhost' to database 'facefunv_chuck' – vishnu Feb 21 '12 at 18:47
  • Looks like a permission access error. Try Changing localhost to an ip address 127.0.0.1. Is there an entry in the mysql.user table for that user (free_1) and localhost? Fyi localhost and 127.0.0.1 function diferent in mysql for access – Ray Feb 21 '12 at 19:28