从本地主机上的PHP文件访问MySql数据库 [英] Accessing MySql Database from PHP file on local host

查看:233
本文介绍了从本地主机上的PHP文件访问MySql数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在LAMP服务器上进行简单的连接和选择.

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

我已经使该php代码在连接到远程sql数据库时可以工作,但是我无法使其与本地数据库一起工作.

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";
        }
    ?>

您可以在此处看到我的服务器正在提供PHP页面(http://162.226.5.161/workingphp.php)

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

但是,当您尝试连接到上面的代码(http://162.226.5.161/TestPhp.php)时,它会出错.错误500

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

创建用户时,我还为用户提供了服务器上的SELECT特权

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

因此错误发生在第1行:

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?

推荐答案

只需确保您使用的是有效凭据,如果一切正常,请查看以下内容:

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

.htaccess文件错误

如果您在网站上使用.htaccess,则它可能会干扰您试图加载到浏览器中的网页.请仔细检查.htaccess配置.任何语法错误都会导致显示500 Internal Server Error消息,而不是您的网站.

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.

要确认配置错误.htaccess是否是500 Internal Server错误的原因,请暂时删除或重命名.htaccess文件,然后尝试重新加载页面.

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.

另请参阅:

使用.htaccess重写规则 使用.htaccess文件

Using .htaccess rewrite rules Using .htaccess files

PHP编码超时

如果您的PHP脚本建立了外部网络连接,则连接可能会超时.如果尝试了太多连接并超时,则将导致"500 Internal Server Error".为了防止这些超时和错误,您需要确保使用某些超时规则对PHP脚本进行编码.但是,通常很难在连接到数据库或从外部连接到远程资源(例如RSS提要)时捕获超时错误.实际上,它们冻结了脚本,使其无法继续运行.

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.

删除任何外部连接都可以提高网站的性能,并减少出现"500 Internal Server Error"的机会.

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

内部服务器错误500不是客户端或网站/网站错误...

这篇关于从本地主机上的PHP文件访问MySql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆