View Full Version : Resolved Select..From...Where

08-25-2011, 09:29 AM

I'm trying to do a SELECT command to retrieve two columns of data from my MySQL database in php. I want it to search one particular table and show records that have the users username in column one.
I know I will need a
SELECT...FROM...WHERE but I would be grateful for any advice on the finer details.


08-25-2011, 09:49 AM
select col1, col2 from myTable where col1 = 'someUsername'

08-25-2011, 02:39 PM
Thanks, this is great, but I would like it so that when the user logs in, the 'someusername' is the logged in users username. Is this possible?

08-25-2011, 04:30 PM
It would be because you'd store the information in a variable and use the variable in place.

select col1, col2 from myTable where col1 = $loggedinuser

08-25-2011, 08:21 PM

Thanks both for your replies. I think I may have a more general issue here because I am still getting a syntax error in this select command, so I will post more of my code to see if that helps:

if (!isset($_SESSION['myusername']) || empty($_SESSION['myusername'])) {
include "logout.php";
} ?><br />
Welcome <? $_SESSION[myusername] ?>! Thank you for logging in.
<p>Your current absences this month:</p>
SELECT date, service, FROM personsick, personabsent, WHERE person_sick = $session['myusername'] OR person_absent = $session['myusername'] ?>

By reading my code (I know, its very messy!), you have probably realized that I am a complete beginner with next to no experience, so any help with any part of the code would be very much appreciated.


Old Pedant
08-25-2011, 08:41 PM
If those fields, person_sick and person_absent are TEXT fields of any kind (that is, not numbers) then you need apostrophes around the values being tested for.

Further, in case the values being tested might contain an apostrophe, you need to "sanitize" them.

And you seem to be trying to JOIN two tables, but you don't show how the are to be joined. Further, you don't show which field the date and service fields are from. And and and... Are you sure you understand your own database?

Finally, you can't just stick a SQL query into your PHP code like that. You have to make a connection to the database and use mysql_query and and and...

I don't code in PHP, so I'll simply direct you to

But I will show you the correct form of the SQL you need to use.

$uname = mysql_real_escape_string( $session["myusername"] );
$sql = "SELECT `date`, service, FROM personsick, personabsent "
. " WHERE ...you need the join condition between those two tables... "
. " AND (person_sick = '" . $uname . "' OR person_absent = '" . $uname . "')";

echo "<hr>DEBUG SQL: " . $sql . "<hr>"; // remove when it starts to work

$result = mysql_query( $sql );

... and much more ... go read the tutorial ...

Old Pedant
08-25-2011, 08:45 PM
In your first post, you wrote

I want it to search one particular table
but that SQL query you showed is using *TWO* tables. Which is it?

08-26-2011, 09:36 AM
Thanks for your reply,
I only posted part of my code and yes, I have made a connection to the MySQL database before hand and run the query afterwards.
The tables person_sick and person_absent are independent of each other, so I think I will do them as two separate queries.

I think that is pretty much my question answered, thanks guys!

Old Pedant
08-26-2011, 09:41 PM
You could do the query as a UNION, if you wanted a single set of records to process in PHP.