Accessing MySql Database from PHP file on local host

断了今生、忘了曾经 提交于 2019-12-13 04:38:46

问题


I am attempting to do a simple connect and SELECT on a LAMP server.

I have made this php code work when connecting to a remote sql database but I cant get it to work with a local database.

 <?php
    //I also tried replacing the IP with localhost
    $db = mysql_connect('192.168.1.69','UserIcreated','MyPassword') or die("Connection error");
    $db = mysql_select_db("MyDataBaseName",$db) or die(mysql_error($db));

        $result = mysql_query("SELECT * FROM  MyTableName");

        if(mysql_num_rows($result)>0){
            echo "Try again";
        }
        else{
            echo "It works";
        }
    ?>

You can see here that my server is serving PHP pages(http://162.226.5.161/workingphp.php)

But when you try and connect to the above code (http://162.226.5.161/TestPhp.php) it errors out. With error 500

When I created the user I also gave the user SELECT privileges on the server

UPDATE

So the error is happening in line 1:

 $db = mysql_connect('192.168.1.69','UserIcreated','MyPassword') or die("Connection error");

I commented out the rest of the code and I still get the error. So I believe this to be a configuration issue. Any insight?


回答1:


Just make sure that you are using valid credential, If all thing are right then make a look on these:

Error with an .htaccess file

If you are using a .htaccess on your site, it may be interfering with the web page you are trying to load into your browser. Please double check the .htaccess configuration. Any syntax errors will cause a 500 Internal Server Error message to be displayed instead of your website.

To confirm whether a misconfiguration .htaccess is the cause of the 500 Internal Server error, either remove or rename the .htaccess file temporarily and then try to reload the page.

See also:

Using .htaccess rewrite rules Using .htaccess files

PHP Coding Timing Out

If your PHP script makes external network connections, the connections may time out. If too many connections are attempted and time out, this will cause a "500 Internal Server Error." To prevent these time outs and errors, you'll want to make sure that PHP scripts be coded with some timeout rules. Typically, however, catching a timeout error when connecting to a database or externally to remote resources (example: RSS feeds) are difficult. They, in effect, freeze the script from continuing to run.

Removing any external connections can increase both the performance of your website and decrease the chances of you receiving a "500 Internal Server Error."

Internal Server Error 500 is not a client or website/web fault...




回答2:


You have re-assigned connection resource id in $db in second line

Change $db to $db2 in second line (Database selection function)

Use

$db = mysql_connect('192.168.1.69','UserIcreated','MyPassword') 
      or die("Connection error");

$db2 = mysql_select_db("MyDataBaseName",$db) 
      or die(myql_error($db));

Instead of

$db = mysql_connect('192.168.1.69','UserIcreated','MyPassword') 
      or die("Connection error");

$db = mysql_select_db("MyDataBaseName",$db) 
      or die(myql_error($db));


来源:https://stackoverflow.com/questions/18946269/accessing-mysql-database-from-php-file-on-local-host

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!