0

I am trying to get a date from my database, but only when certain conditions are met. I want a table to populate with data from mySQL, only when the data is submitted under a users name. (i.e. this data is relevant to this user.)

This data has been saved as a TIMESTAMP.

$sql = "SELECT * FROM bug LIMIT 100";
$result = mysql_query($sql)or die(mysql_error());


     while($row = mysql_fetch_array($result)){

$bdate      =   $row['bugDate'];
$bfor      =    $row['bugFor'];


$finduserid= mysql_query
    ("SELECT UserId FROM user WHERE userName='{$_SESSION['myusername']}'"); 

          if($finduserid)

    {
$getuserid = mysql_fetch_assoc($finduserid);
    }

$findbdate = 
    mysql_query("SELECT bugDate FROM bug WHERE bugDate = '$bdate' 
    AND  '$bfor' = '" . $getuserid['UserId'] . "'");

        if($findbdate)
            {
        $getbdate = mysql_fetch_array($findbdate);
            }


$bdatetime = new DateTime($getbdate['bugDate']);
$formattedbdate = date_format($bdatetime, 'd,M Y');

If anyone can help me I'd greatly appreciate it.

Obviously the security is terrible and I'm pretty sure this method would be the most inefficient way of doing what I'm trying to do, however I'm a noob and kind of learning by doing. If you have some noob friendly documentation on security and seeing this is making you cringe, feel free to post a link. Any help is greatly appreciated.

(Let me know if you need to know more and sorry if I didn't include it in the first place!)

EDIT: I got it working. I was doing it the most roundabout, ridiculous way anyone could come up with. Although I didn't use JOIN it sent me on the right path. Condensed 250 lines of pointless not working code down to 3-4 that will be obvious to everyone but me.

I selected the row where my session login equalled my loginName on the database.

$finduserid=  "SELECT UserId FROM user WHERE userName='{$_SESSION['myusername']}'";     



$runuserid = mysql_query($finduserid)or die(mysql_error());

          while($getuserid = mysql_fetch_array($runuserid)){
$userid = $getuserid['0'];
                        }

I then, only Selected records that included my username (instead of everything and then trying to get rid of everything that didn't have my username in it.) -_- (Im an idiot.)

$sql = "SELECT * FROM bug WHERE bugFor = '$userid'";

$result = mysql_query($sql)or die(mysql_error());
Cash
  • 5
  • 3
  • Hi Cash, with regards to improving your learning of the language, have a read of this previous question on SO: http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – Brendan Bullen Oct 15 '13 at 17:15

1 Answers1

0

Read up on JOINs.

I can't exactly tell what your code is supposed to do, or completely decipher your DB's schema, but the below query should get all bugs for the current user that were filed on the given date:

SELECT b.*
FROM bug b INNER JOIN users u
  ON b.bfor = u.userID
WHERE u.userName = '{$_SESSION['myusername']}'
  AND b.bugDate = '$bdate'
Sammitch
  • 30,782
  • 7
  • 50
  • 77
  • I didn't use join, but thanks to you I worked out how wrong I was. If you want a good chuckle, come and have a read. I should be banned from coding anything. Thanks man!! – Cash Oct 15 '13 at 21:45
  • @Cash you're fired. :I – Sammitch Oct 15 '13 at 21:54
  • I couldn't see the wood for the trees! I had like 30 variables in my head along with 30 querys... The analogy I used to explain what an absolute idiot I was, was "I had taken everything out of the super market and was trying to put everything back I didn't want, when I should have just taken out what I wanted." If you hadn't shown me that join query I would have never thought it through fresh. Thankyou so much! :) – Cash Oct 16 '13 at 12:54