php script can't connect to local mysql, what the heck?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • d3vkit
    New Member
    • Nov 2006
    • 34

    php script can't connect to local mysql, what the heck?

    Okay so I am at a loss here. I have a website that I've previously had no trouble connecting to the mysql DB on. I have an include to a connect file with the relevant connection info, and it was working fine until today. I am trying to implement some ajax with the javascript framework mootools (although I don't see how this is causing the problem it started happening right around this time sooo...)

    I am sending info from my login form to the processLogin.ph p page, which in turn logs me in and everything, and then is supposed to write new info to the relevant div which sent the call.

    But my processLogin page started saying it "couldn't connect to the localhost", which most definitely is not my intent nor have it set it to. So I included the connect.php page on the page itself; nothing. It still had a mistake (occurring right at my first query). So I wrote the connection info directly before the query, and no longer receive that error, but still no data. I echo'd the query and get nothing. So, here is the connect.php code I include in index.php, so it can call it each time:
    [PHP]<?php
    $dbServer = 'mysql***.secur eserver.net';
    $dbDatabase = '****;
    $dbUser = '****';
    $dbPass = '****';

    // Connect to database
    $db = mysql_pconnect( $dbServer, $dbUser, $dbPass) or die("Couldn't connect to database due to error in page: " . mysql_error());
    mysql_select_db ($dbDatabase, $db) or die("Couldn't select database due to error in page: " . mysql_error()); ?>[/PHP]

    Neither of these error message ever appear, btw, meaning the connection is working here.

    Here is the code that is throwing the error:
    [php]$sql = mysql_query("SE LECT * FROM users WHERE username=" . $username . " AND password=" . $password . " AND activated='1'") ;
    echo 'sql=' . $sql;
    $login_check = mysql_num_rows( $sql);[/php]

    Where I echo sql=$sql, I get "sql=" and nothing else, and this is the error message:

    Warning: mysql_num_rows( ): supplied argument is not a valid MySQL result resource in /blahblahblah on line 51.
    And finally, when I include the connect info instead of writing it straight to the page (and I'm including it to the page, as opposed to index.php [where I normally include it]):

    Warning: mysql_query(): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) in /webpage on line 47
    Sorry for the lengthy first post, but I wanted to get all relevant info out there.
    My guess is it's because I'm calling for the page through ajax, but I can't figure out how that causes the problem or how to fix it. Any ideas or help MUCH appreciated!
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Let's start with the select statement. I assume that the userid and password are CHAR fields, so you'll have to include them within quotes. The entire select string incl. the variables van be code in one double-quote enclose string. So your statement is:
    [php]$sql = mysql_query("SE LECT * FROM users WHERE username='$user name' AND password='$pass word' AND activated='1'")
    or die("Invalid query: " . mysql_error());
    [/php]
    Ronald :cool:

    Comment

    • d3vkit
      New Member
      • Nov 2006
      • 34

      #3
      Originally posted by ronverdonk
      Let's start with the select statement. I assume that the userid and password are CHAR fields, so you'll have to include them within quotes. The entire select string incl. the variables van be code in one double-quote enclose string. So your statement is:
      [php]$sql = mysql_query("SE LECT * FROM users WHERE username='$user name' AND password='$pass word' AND activated='1'")
      or die("Invalid query: " . mysql_error());
      [/php]
      Ronald :cool:
      Ah, you're right about that... but it still didn't quite fix it. When I write the connect script into the page, it works; when I include the script, it doesn't. Is this some sort of scope that I don't understand? Can it not connect to an included script with ajax maybe?
      Well, it works, but not as well as I'd like, but I suppose it's all the same. Thank you for your help; I can't believe I didn't see that.

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        Is the connect script, the one that you include, in a different path from the script that includes it and, if so, are you correctly backtracking or forward tracking the include. E.g. if the connect script is one dir below (child dir) the including script, you must include it as [php]include('connec t/dbinclude.php"' );[/php].
        If it is in a dir above (parent dir) the including script you define [php]include('../dbinclude.php"' );[/php].
        If it is in another dir down from the one above you (sibling dir), you do [php]include('../connect/dbinclude.php"' );[/php].
        Is that the reason?

        Ronald :cool:

        Comment

        • d3vkit
          New Member
          • Nov 2006
          • 34

          #5
          Originally posted by ronverdonk
          Is the connect script, the one that you include, in a different path from the script that includes it and, if so, are you correctly backtracking or forward tracking the include. E.g. if the connect script is one dir below (child dir) the including script, you must include it as [php]include('connec t/dbinclude.php"' );[/php].
          If it is in a dir above (parent dir) the including script you define [php]include('../dbinclude.php"' );[/php].
          If it is in another dir down from the one above you (sibling dir), you do [php]include('../connect/dbinclude.php"' );[/php].
          Is that the reason?

          Ronald :cool:
          I'm sorry I never replied to you; I was going to that night, then got busy and it never let up apparently.

          Just wanted to say thanks for the suggestions. I went to the code, did the include as I normally would (backtracking to the scripts directory), and it worked no problem. No idea what I was doing wrong before, but it's all sorted out now. Seemed I had done this in the first place, but oh well. I do appreciate thehelp; I learned that I don't need to include variables in my mysql call with periods. Forgot what that's called. Dang it.

          Comment

          Working...