can't connect to mysql

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dragon52
    New Member
    • Jun 2009
    • 72

    can't connect to mysql

    Hi,

    I am writing a login page but the php code to authenticate user is not working.

    My login web page calls 'action="xLogin Check.php"'. Below is the php code. I am writing to a log file to trace program flow.
    I know it is ok up to the "hoho" bit. This code(version1) works.

    Code:
    <?php
    $host="localhost"; // Host name 
    $username="****"; // Mysql username 
    $password="****"; // Mysql password 
    $db_name="accounts"; // Database name 
    $tbl_name="userstbl"; // Table name 
    
    $FileName = "log.txt";
    $FileHandle = fopen($FileName,'w') or die("can't open file");
    fwrite($FileHandle, "hoho\n");
    fclose($FileHandle);
    ?>
    This code(version2) doesn't work, ie when I add the mysql_connect bit. Nothing inside the mysql_connect {} appear on the screen or the log file. I just get error page that says "website cannot display page"


    Code:
    <?php
    $host="localhost"; // Host name 
    $username="****"; // Mysql username 
    $password="****"; // Mysql password 
    $db_name="accounts"; // Database name 
    $tbl_name="userstbl"; // Table name 
    
    $FileName = "log.txt";
    $FileHandle = fopen($FileName,'w') or die("can't open file");
    fwrite($FileHandle, "hoho\n");
    
    if (!$connection = mysql_connect($host, $username, $password))
    {
      $message = mysql_error();
      fwrite($FileHandle, $message);
      fclose($FileHandle);
      die();
    }
    
    ?>
    Can anyone see where the problem is?? What is the best way to trace php code???

    thanks
    Last edited by Dormilich; Aug 23 '10, 08:58 AM. Reason: removed DB login information – for your own safety
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    #2
    Firstly, you should probably **** out your password as good practice for public forums.

    It might be legal, but your if condition on line 12 looks a bit sus. Humour me and try:
    Code:
    $connection = mysql_connect($host, $username, $password);
    if (! $connection) { 
      $message = mysql_error(); 
      fwrite($FileHandle, $message); 
    }
    fclose($FileHandle);
    Also, you were only closing the file if it failed to connect.

    Comment

    • dragon52
      New Member
      • Jun 2009
      • 72

      #3
      thanks for your reply !!

      I will replace user. Tried your suggestion but all I get in the log is "hoho 22". I have added an else line.

      I am sure mysql server is working because I can login using the administrator tool.

      I have just installed Apache, MySQL and PHP. Could it be it can't find the mysql db?? Where do i check??

      Code:
      <?php
      $host="localhost"; // Host name 
      $username="****"; // Mysql username 
      $password="****"; // Mysql password 
      $db_name="accounts"; // Database name 
      $tbl_name="userstbl"; // Table name 
      
      $FileName = "log.txt";
      $FileHandle = fopen($FileName,'w') or die("can't open file");
      fwrite($FileHandle, "hoho 22\n");
      
      $connection = mysql_connect($host, $username, $password);
      if (!$connection)
      {
        $message = mysql_error();
        fwrite($FileHandle, $message);
      }
      else
      {
        fwrite($FileHandle, "ok");
      }
      
      fclose($FileHandle);
      ?>
      Last edited by Dormilich; Aug 23 '10, 08:59 AM. Reason: removed DB login credentials

      Comment

      • TheServant
        Recognized Expert Top Contributor
        • Feb 2008
        • 1168

        #4
        Try run a script like this:
        Code:
        <?php
        $link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
        if (!$link) {
            die('Could not connect: ' . mysql_error());
        }
        echo 'Connected successfully';
        mysql_close($link);
        ?>
        What (if any) output do you get?

        Comment

        • dragon52
          New Member
          • Jun 2009
          • 72

          #5
          Tried your suggestion and I am getting Server Error. See attached.

          Which server is in error??

          The Apache server? When I "http://localhost/index.php" I get the right stuff on the screen.

          The MySQL server? I can login using the mysql administrator tool.

          Is there another server I don't know about?

          thanks
          Attached Files

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            does the same happen in Firefox, Safari, Chrome or Opera?

            Comment

            • dragon52
              New Member
              • Jun 2009
              • 72

              #7
              I have installed Apache, MySQL and PHP on my own PC a few days ago. I have done this so I can learn PHP and web development. This is the first time I am accessing MySQL using PHP since the install. Everything happens on my PC only. I use Internet Explorer (Windows 7) and I don't use these other web browsers.

              I have been surfing for an answer and several people suggest, in php.ini, uncommenting

              ;extension=php_ mysql.dll

              but my php.ini file only has

              ;extension=msql .dll

              Is this a problem? If I go ahead and uncomment this line it is worse.

              I cannot find 'php_mysql.ddl' or 'msql.dll' on my computer. But then I am new to Windows 7 and I am not sure how to search for a file properly

              I have a feeling PHP doesn't know where to find MySQL. What to do???

              Comment

              • johny10151981
                Top Contributor
                • Jan 2010
                • 1059

                #8
                your server giving 500 error. Its server internal error. make sure your server is working properly

                Comment

                Working...