madjack87 Posted November 30, 2010 Share Posted November 30, 2010 Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/aci/docs/admin/hours.php on line 20 <?php $user = $_SESSION['myusername']; $result = mysql_query("SELECT * hours WHERE member='$user'"); while ($row = mysql_fetch_array($result)){ echo $row['date'] . " " . $row['time'] . "<br />"; } ?> CREATE TABLE `hours` ( `hoursID` int(5) NOT NULL auto_increment, `member` varchar(20) NOT NULL default '', `date` date NOT NULL default '0000-00-00', `time` time NOT NULL default '00:00:00', PRIMARY KEY (`hoursID`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ; -- -- Dumping data for table `hours` -- INSERT INTO `hours` VALUES (4, 'aci', '2010-11-30', '14:31:39'); INSERT INTO `hours` VALUES (2, 'aci', '2010-11-30', '14:31:08'); INSERT INTO `hours` VALUES (3, 'aci', '2010-11-30', '14:31:23'); INSERT INTO `hours` VALUES (5, 'aci', '2010-11-30', '14:31:40'); Quote Link to comment Share on other sites More sharing options...
requinix Posted November 30, 2010 Share Posted November 30, 2010 http://php.net/manual/en/function.mysql-error.php Quote Link to comment Share on other sites More sharing options...
madjack87 Posted November 30, 2010 Author Share Posted November 30, 2010 It has nothing to do with my database connectivity. If there is some code I can add to give a better error please tell me how to add that to what I currently have. Quote Link to comment Share on other sites More sharing options...
madjack87 Posted November 30, 2010 Author Share Posted November 30, 2010 I added die (mysql_error()); this is the error it gives now. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'hours WHERE member='aci'' at line 1 Where member = aci the aci is pulled from the session id which is correct. Any help would be appreciated. Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted November 30, 2010 Share Posted November 30, 2010 http://dev.mysql.com/doc/refman/5.1/en/select.html Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted November 30, 2010 Share Posted November 30, 2010 I would write the query in two lines, so debugging is much easier: <?php $query = "SELECT * hours WHERE member='$user'"; $result = mysql_query($query) or die("Problem with the query: $query<br>" . mysql_error()); ?> That said, your error is that you left out the required word "from" in the query: <?php $query = "SELECT * from hours WHERE member='$user'"; ?> Ken Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted November 30, 2010 Share Posted November 30, 2010 It would be a good idea to: First: remove the query string from the query execution, store it in a variable, and use the variable in the execution. Second: check whether the query executed successfully. If yes, make sure it returned more than an empty result set. If no, echo the query with any error. Third: make sure that any script making use of $_SESSION variables has session_start() somewhere in it before any output is generated. Fourth: Since the query is really contingent on $_SESSION['username'] having a value, check for it, and if not set, don't execute the query. session_start(); if( !isset($_SESSION['username']) ) { // $_SESSION variable is not set. echo "Session variable not set."; } else { $user = $_SESSION['myusername']; $query = "SELECT * hours WHERE member='$user'"; if( $result = mysql_query($query) ) { if( mysql_num_rows($result) > 0 ) { while ($row = mysql_fetch_array($result)) { echo $row['date'] . " " . $row['time'] . "<br />"; } } else { // Query succeeded, but result set was empty echo "No Results Returned."; } } else { echo "<br>Query: $query<br>Failed with error: " . mysql_error(); } } Quote Link to comment Share on other sites More sharing options...
jdavidbakr Posted November 30, 2010 Share Posted November 30, 2010 Also ... you might change your query to: $query = "SELECT * from hours WHERE member='$user'"; Quote Link to comment Share on other sites More sharing options...
madjack87 Posted November 30, 2010 Author Share Posted November 30, 2010 I forgot the FROM in select * from hours I had select * hours. thanks for the solution. Also Session start was at the top of the html page so I know it was set if the session wasnt active it would have redirected to the login page. Also I knew that the session was working because I echo the username to make sure. Thank you all for you help and pikachu I will be saving this page so I can look at your code and study it more as it is more professional that I currently write.. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.