tyman1294 tyman1294 - 1 year ago 44
MySQL Question

PHP MYSQL > search for username and echo a columns data

I'm looking for a simple php script that will look through my database for a username and echo a column. The column I want to echo is a date.

I'm making a script that checks if the date assigned to the user is todays date.

$datenow = date("Y-m-d");
$user = $_SESSION['username'];

$connection = mysql_connect('localhost', 'username', 'password');
mysql_select_db('database name');

$result = mysql_query("SELECT accessfrom FROM users WHERE username = $user");

// Now I need a simple way to check if the results date = $datenow(from above)

mysql_close(); //Make sure to close out the database connection

Thank you!

Answer Source

This is too long for a comment.

Don't use mysql. It is deprecated. So, use mysqli_.

Presumably, username is a string. So, you have a mismatch in your comparisons. The naive solution is to add single quotes:

$result = mysql_query("SELECT accessfrom FROM users WHERE username = '$user'");

The correct solution is to use mysqli_ and use parameters for passing in values. This not only solves your problem. It also prevents SQL injection attacks, and teaches you how to correctly write queries.