Jump to content

$result = mysql_query("SELECT * hours WHERE member='$user'");


madjack87

Recommended Posts

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');
        

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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();
   }
}

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.