+ Reply to Thread
Results 1 to 7 of 7

Thread: SQL Query Problem

  1. #1
    gavicus's Avatar
    gavicus is offline x10Hosting Member gavicus is an unknown quantity at this point
    Join Date
    May 2008
    Posts
    9

    SQL Query Problem

    I'm having problems with my login script. I use the code:

    $connection=mysql_connect($server,$username,$passw ord);
    $db=mysql_select_db($database,$connection);
    $myusername=$_POST['myusername'];
    $mypassword=$_POST['mypassword'];

    $q = "SELECT * FROM users WHERE username='$myusername' and password='$mypassword';";
    $result = mysql_query($q);
    $count = mysql_num_rows($result);

    ...but regardless of my form entry, I get the error:

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/gavicus/public_html/checklogin.php on line 22

    ...where line 22 is the call to mysql_num_rows.

    Am I doing anything obviously stupid? I've wondered if x10's SQL server is having problems because everything is slow since I started yesterday, but that only makes it impossible for a newby like me to know if I'm doing something wrong.

  2. #2
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: SQL Query Problem

    The first thing I'm going to mention is it's a very very very very very very very bad practice to not sanitize input directly from a user. I could use
    Code:
    ' OR 1=1
    as my username, which means your query will ALWAYS return a result, even if I'm not a user of your site. See what I mean :P

    You also could use some error checking throughout.
    Code:
    $connection=mysql_connect($server,$username,$password) or exit(mysql_error()); //password was passw ord before. now if the connection fails, it will say why.
    $db=mysql_select_db($database,$connection) or exit(mysql_error()); //if db select fails, say why
    if (!isset($_POST['myusername'], $_POST['mypassword'])) {
         exit('Missing user name or password');
    }
    $myusername = addslashes($myusername);
    $mypassword = addslashes($mypassword);
    
    $q = "SELECT * FROM users WHERE username='$myusername' and password='$mypassword'"; //don't put semicolons in your queries
    $result = mysql_query($q);
    if ($result === false) {
         exit(mysql_error()); // if the query fails, say why
    }
    $count = mysql_num_rows($result);
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  3. #3
    gavicus's Avatar
    gavicus is offline x10Hosting Member gavicus is an unknown quantity at this point
    Join Date
    May 2008
    Posts
    9

    Re: SQL Query Problem

    I use stripslashes and mysql_real_escape_string on both username and password, but I trimmed them out of my message as I don't think they're the problem. Thanks for the response, though. I couldn't agree more.

    Oh, I also removed the semicolon from the query string, but got the same results. Why don't you use semicolons in your queries? Isn't that standard SQL?
    Last edited by gavicus; 07-03-2009 at 11:27 AM. Reason: Automerged Doublepost

  4. #4
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: SQL Query Problem

    You use semicolons if you're sending a group of queries. I don't think it's the problem, I just take them out because they don't help the query. Did you put the error checking in? That should tell you what's wrong.
    Last edited by garrettroyce; 07-03-2009 at 11:42 AM.
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  5. #5
    gavicus's Avatar
    gavicus is offline x10Hosting Member gavicus is an unknown quantity at this point
    Join Date
    May 2008
    Posts
    9

    Re: SQL Query Problem

    Ok, I found the problem--it was in my SQL query. Well, like I said, I'm new at this. Still, garrettroyce, how do you put in error checking?
    Last edited by gavicus; 07-03-2009 at 02:14 PM.

  6. #6
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: SQL Query Problem

    That's the lines I added in my code. You need to check if the connection is successful before changing databases. You need to check if the database change is successful before querying it. You have to check if the query is successful before using the result.
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  7. #7
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: SQL Query Problem

    Error messages from mysql_error() are going to be too technical and potentially contain sensitive information. They won't contain information the average user can use, and may contain information a malicious user can use. If you need to see the full error, log it with error_log() so that only a site admin can see all the information.

    If you use addslashes, your script is still vulnerabel to SQL injection because it doesn't take into account multibyte strings or the syntax used on the DBMS. Prepared statements (via mysqli_prepare or PDO::prepare) are the most secure way of preventing SQL injection.

    Does the password column contain plaintext passwords? A much more secure approach is to store a salted hash. There's still the issue of how you'll transmit authentication information if you're not using HTTPS.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. Microsoft SQL Server (a rant)
    By merrillmck in forum Programming Help
    Replies: 6
    Last Post: 07-08-2009, 10:43 AM
  2. sql query
    By markl1988 in forum Programming Help
    Replies: 14
    Last Post: 05-30-2009, 05:14 PM
  3. Problem with SQL
    By kostas in forum Free Hosting
    Replies: 5
    Last Post: 09-12-2008, 11:18 AM
  4. problem on creating sql database
    By joseph0829 in forum Free Hosting
    Replies: 1
    Last Post: 12-05-2007, 01:07 PM
  5. i am having my sql problem
    By xtone in forum Free Hosting
    Replies: 2
    Last Post: 10-27-2006, 01:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers