![]() |
mysql_real_escape
hey i need some help with this login form.
everytime i do this i get login failed even though it is in the database/table/row! Plz tell me whats wrong? Yes i already connected and selected the database. :
$username = mysql_real_escape_string($_POST['username']); |
Re: mysql_real_escape
You only need to use mysql_real_escape_string to quote a string for use in a query. MySQL returns strings from the database in a literal (unescaped) form. You should read about mysql_real_escape_string in the php manual.
Also, $row in your code is going to represent the FIRST user selected from the database. Your SQL query selects ALL users, instead of selecting ONLY the user identified by $username. This will result in checking the password against the wrong user most of the time if you have multiple users in the table. Example: :
$username = mysql_real_escape_string($_POST['username']); |
Re: mysql_real_escape
Quote:
@kishou: You can also use a loop to compare against all of the things pulled from the table, but using a WHERE clause in the SQL query like dr.p showed is what you want for this situation. (If for some reason you allow the same username multiple times, then you'll need the loop as well.) |
Re: mysql_real_escape
Quote:
|
Re: mysql_real_escape
Quote:
Just so that we're clear, $username contains the escaped version of $_POST['username'], which is returned by mysql_real_escape_string in your code. Whether you want to insert a string, update it, select with it, anything... if the contents of a variable (like $_POST['username']) need to go into a query/statement as a string, then you need to escape it. Read the PHP manual page for mysql_real_escape_string on php.net. It goes over what the function is for, provides examples, and has some very important security information. |
Re: mysql_real_escape
Quote:
And mysql_fetch_array stores two sets of the requested information in memory in order to make it available by numeric and associative. Best practice is to use which one you need, unless you actually need both. |
Re: mysql_real_escape
Quote:
Out of curiosity, why is using the specific array (assoc/numeric) considered a best practice? Just because you limit the ways to access the data? |
Re: mysql_real_escape
Quote:
Quote:
And the PHP docs say that _array doesn't cause a significant slow down, but I have doubts about that when it comes to large amounts of data, based on some of the scripts I've worked on. In all fairness to PHP, though, it had gotten to the point where every little bit helped. |
Re: mysql_real_escape
Before fetching the row, make sure it exists - if the user isn't there, you'll get 0 rows returned.
:
$result = mysql_query("SELECT * FROM registered_members WHERE username='$username'"); |
| All times are GMT -5. The time now is 3:36 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC