PHP & mySQL seem to not like each...

Discussion in 'Web Design and Development' started by jlewis2k1, Nov 25, 2008.

  1. jlewis2k1 macrumors 6502a

    jlewis2k1

    Joined:
    Jan 14, 2005
    Location:
    in your closet
    #1
    So, I have repeatedly reentered the mySQL query and I been getting the same error every time. I have used the myPHPAdmin and copied the code directly and I'm still getting an error.
    Code:
    SELECT * FROM `users` WHERE `usern` = 'jeff.lewis' AND `passw` = '1f3870be274f6c49b3e31a0c6728957f' LIMIT 0 , 30
    Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/content/j/e/l/jelew1985/html/nfm/Config/NFM-Functions.php on line 63
    I am sorry there seems to be a problem with given credentials. This is the error that is given to me: - Charlie
    and this is what I copied from mySQL
    PHP:
    $sql 'SELECT * FROM `users` WHERE `usern` = \ 'jeff.lewis\' AND `passw` = \'1f3870be274f6c49b3e31a0c6728957f\' LIMIT 0, 30 ';
    I am getting nowhere with this thing. Every forum I go to says that its a bad query. But, when I run the code in myPHPAdmin it runs it with no problems. Yet, when I plug it in my php code it hates it...

    PHP:
    function UserPass($username,$password) {
        global 
    $SQLC;
        
    $password=md5($password);
        
    $SQLQ "SELECT * FROM  `users` WHERE  `usern` =  'jeff.lewis' AND  `passw` =  '1f3870be274f6c49b3e31a0c6728957f' LIMIT 0 , 30";
        echo 
    $SQLQ;
        
    $SQLR=mysql_query($SQLQ$SQLC)
            or die(
    "I am sorry there seems to be a problem with given credentials. This is the error that is given to me: \n\n".mysql_error()."\n\n- Charlie");
            while(
    $SQLO mysql_fetch_array($SQLRMYSQL_ASSOC))
            {
                
    $UID $SQLO['uid'];
                } 
                return 
    false;
    }
    Any suggests my fellow PHP guys?
     
  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #2
    I think the issue is possibly with this line,
    PHP:
    $SQLR=mysql_query($SQLQ$SQLC)
    I'm not sure it likes that second argument. See manual for this function. The second argument is for the link_identifier, and the error message you provided is saying "... supplied argument is not a valid MySQL-Link resource ..." and so they seem related. Try doing it without that second argument. This is a quick assessment though, so no guarantees.
     
  3. jlewis2k1 thread starter macrumors 6502a

    jlewis2k1

    Joined:
    Jan 14, 2005
    Location:
    in your closet
    #3
    so, I got it working again. Now, the problem is its refusing to set a cookie... :-( and my script needs to have cookies to work properly
     
  4. SrWebDeveloper macrumors 68000

    SrWebDeveloper

    Joined:
    Dec 7, 2007
    Location:
    Alexandria, VA, USA
    #4
    Actually, the second argument is the link established from the initial connection to the database. That portion of the code was not shown by the OP but I see they referenced the variable containing the link resource ID globally so we'll have to assume that's correct.

    What caught my eye and the OP might laugh with me a little at this one, as we all make mistakes like this ---> Check out the function which has the $password variable passed to it, then converted to MD5. Good so far, but then a MD5 password string of 32 chars is hard coded into the query string! Probably there from testing or copied/pasted and not changed by accident. Oops.

    Find:
    `passw` = '1f3870be274f6c49b3e31a0c6728957f'

    Replace with:
    'passw` = '$password'

    This assumes the field named 'passw' is varchar(32) meaning it's 32 bytes in size.

    One final note about PHP's MD5 function - it creates a lower case 32 byte string while most other languages create it in upper, so make it "strtoupper(md5($password))" if you know it's UPPER CASE in the database.

    -jim
     
  5. SrWebDeveloper macrumors 68000

    SrWebDeveloper

    Joined:
    Dec 7, 2007
    Location:
    Alexandria, VA, USA
    #5
    Be sure to read what I wrote above -- your query as originally coded will only work for one password! heh

    As to cookies, make sure no browser output occurs before issuing cookie commands since they're sent as part of the HTTP headers which PHP manages. Remove all newlines or spaces at the top of the script where you cookie code is, for example, or add the code to a page intended to handle form submissions where processing is done before browser output. Get the idea? This is the #1 most common issue assuming syntax is right and you're simply scratching your head! Been there!

    Beyond this, please post your code and we'll gladly help so we can see if its a session cookie, check the cookie path, etc.

    -jim
     
  6. jlewis2k1 thread starter macrumors 6502a

    jlewis2k1

    Joined:
    Jan 14, 2005
    Location:
    in your closet
    #6
    thanks for the tips guys! There was one little mistake that I wasn't doing...I wasn't adding the DB_Connection() for it to connect :eek: ... but everything is working smoothly. I did take your guy's tips and used them since well, it been awhile since I have done PHP & mySQL stuff.
     
  7. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #7
    I thought it was that and was trying to imply it, but was in a hurry so I probably wasn't clear at all. Did that fix your cookie issue as well or is it still in need of fixing?
     
  8. jlewis2k1 thread starter macrumors 6502a

    jlewis2k1

    Joined:
    Jan 14, 2005
    Location:
    in your closet
    #8
    It WAS working but all the sudden it stopped. Its not even processing the sql query now. Everything was working...I tried tracing back to see if I made an error... but so far not finding it...
     
  9. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #9
    Anything showing up in either the MySQL error logs or the PHP logs?
     
  10. jlewis2k1 thread starter macrumors 6502a

    jlewis2k1

    Joined:
    Jan 14, 2005
    Location:
    in your closet
    #10
    haven't check, but will do that in a few.

    Update: Apparently I don't have access to the logs. Any way I can create a php script to few them?
     
  11. jlewis2k1 thread starter macrumors 6502a

    jlewis2k1

    Joined:
    Jan 14, 2005
    Location:
    in your closet
    #11
    So, my problem is now that it wont make cookies... here is my code for that
    PHP:
            if(isset($_POST["Login"]))
            {
                
    $username=$_POST["username"];
                
    $password=$_POST["password"];
                
    DB_Connection();
                
    $password=md5($password);
            
    $SQLQ "SELECT * FROM users WHERE usern='$username' AND passw='$password'";
            
    $SQLR mysql_query($SQLQ)or die("<BR>I am sorry there seems to be a problem with given credentials. This is the error that is given to me: \n\n".mysql_error()."<BR><BR>\n\n- Charlie");
            while(
    $SQLO mysql_fetch_array($SQLRMYSQL_ASSOC))
            {
                
    $UID $SQLO['uid'];
                
    setcookie("MYA_UID"$UID);
                
    header("Location: index.php");
            Echo 
    "Success!";
            }
            }
     
  12. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #12
    Since we're only seeing a piece of code, are you outputting any HTML before the code you're showing? The setcookie function can only be used before you output any HTML.

    How do you know the cookie is not working? What are you doing to check (e.g., outputting it, or error messages. etc.)? Is your browser set to block cookies?
     
  13. SrWebDeveloper macrumors 68000

    SrWebDeveloper

    Joined:
    Dec 7, 2007
    Location:
    Alexandria, VA, USA
    #13
    Ditto, and I mentioned the output before cookie thing earlier as well so that's #1 on your list to check.

    setcookie("MYA_UID", $UID);

    That sets a session cookie, meaning it will disappear after the browser window is closed (removed from the client's cookie.txt in their browser). When testing, you can check for the cookie many ways. One of the easiest is to use FFox and install the Web Developer plugin and examine cookies that way, another is use the browser to view cookies, i.e. in MSIE Tools -> Options -> Privacy tab -> Show Cookies.

    In PHP, on any page you expect the cookie to be pre-set and will be reading the cookie's value, use this, for example:

    PHP:
    if (isset($_COOKIE['MYA_UID']) && !empty($_COOKIE['MYA_UID'])) {print "Cookie exists";}
    else { print 
    "Cookie was not found";} 
    -jim
     
  14. jlewis2k1 thread starter macrumors 6502a

    jlewis2k1

    Joined:
    Jan 14, 2005
    Location:
    in your closet
    #14
    so, I know that its not setting for a couple of reasons.
    1) It doesn't show up in phpinfo()
    2) I tried the script provided up above and well all I am seeing is a blank page
    3) its not redirecting when setting the cookie

    So, this tells me a few things...
    1) there is a problem with the code because it stopping before setting
    2) That I may need to re-write the code
    3) well it has a mind of its own i think...
     
  15. SrWebDeveloper macrumors 68000

    SrWebDeveloper

    Joined:
    Dec 7, 2007
    Location:
    Alexandria, VA, USA
    #15
    If you mean my code that does a cookie check, it's valid and tested code - I checked to be sure. This means there is something wrong with your code, as you suggested. Please post the relevant code if you want us to help you further, speculation will drive you nuts. We can tell from your code what's wrong, usually.

    If you feel your webhost is blocking cookies in their PHP setup (which is almost unheard of) then download MAMP and install a sandbox and test it locally with a default PHP setup.

    -jim
     
  16. jlewis2k1 thread starter macrumors 6502a

    jlewis2k1

    Joined:
    Jan 14, 2005
    Location:
    in your closet
    #16
    PHP:
    function UserPass($user,$pass) {
        
    DB_Connection();
        
    $SQLQ="SELECT * FROM `users` WHERE usern='$user' AND passw='$pass'";
        
    $SQLR=mysql_query($SQLQ)or die("I am sorry there seems to be a problem with retrieving the user's details. This is the error that is given to me: \n\n".mysql_error()."\n\n - Charlie");
        while(
    $SQLO mysql_fetch_array($SQLRMYSQL_ASSOC))
        {
            
    session_register('MYA_Active'); 
            
    session_register('MYA_UID'); 
            
    $_SESSION['MYA_UID'] = $SQLO[uid]; 
            
    $_SESSION['MYA_Active'] = "Yes"
            
    header("Location: /");
        }
        return 
    false;

    At the moment the sessions aren't registering. I do have session_open() at the very beginning.
     
  17. SrWebDeveloper macrumors 68000

    SrWebDeveloper

    Joined:
    Dec 7, 2007
    Location:
    Alexandria, VA, USA
    #17
    Try this please:

    PHP:
    function UserPass($user,$pass) { 
        
    DB_Connection(); 
        
    $SQLQ="SELECT count(*) found  FROM `users` WHERE usern='$user' AND passw='$pass' limit 1"
        
    $SQLR=mysql_query($SQLQ)or die("I am sorry there seems to be a problem with retrieving the user's details. This is the error that is given to me: \n\n".mysql_error()."\n\n - Charlie"); 
        
       
    $data=mysql_fetch_assoc($SQLR);
       if (
    $data['found']==1) {   
      
            
    session_register('MYA_Active');  
            
    session_register('MYA_UID');  
            
    $_SESSION['MYA_UID'] = $SQLO[uid];  
            
    $_SESSION['MYA_Active'] = "Yes";  
            
    header("Location: /"); 
        } 
        else {return 
    false;} 

    }  
    As you can see, I changed your query and used a different means of determining true/false if the user is authenticated (limiting the result set to 1 item) and then put the session stuff in the true section of the conditional.

    After you replace the above code, if still errors, please click each link below, in order:

    Make sure PHP is configured properly for session support
    Read this tutorial on sessions - do the example on that page in a new script to test

    For now, please give the new code a shot and follow the links above for session help. I'm sure you'll do great on your own! :)

    -jim
     
  18. jlewis2k1 thread starter macrumors 6502a

    jlewis2k1

    Joined:
    Jan 14, 2005
    Location:
    in your closet
    #18
    I got everything working now. thanks for all of your help :-D
     

Share This Page