dongzhi5386
2017-04-25 03:48
浏览 83
已采纳

为什么我的php没有连接到我的服务器?

I'm trying to take and get data from a database to a website using php. When I'm trying to connect I'm using this code:

<?php
error_reporting(-1);
ini_set('display_errors', 'On');
$db_host = 'localhost';
$db_user = 'root';
$db_pass = '';
$db_name = 'test';
$conn = new mysqli($db_host, $db_user, $db_pass, $db_name);
if ($conn->connect_errno) echo "failed to connect to database";
?>

but upon trying to open the php file I recieve this on firefox:

connect_errno) echo "failed to connect to database"; ?> (on firefox)

and this on opera/chrome:

<?php
error_reporting(-1);
ini_set('display_errors', 'On');
$db_host = 'localhost';
$db_user = 'root';
$db_pass = '';
$db_name = 'test';
$conn = new mysqli($db_host, $db_user, $db_pass, $db_name);
if ($conn->connect_errno) echo "failed to connect to database";
?> 

I have also tried loading it with internet explorer but it simply tries to save it again. I'm not understanding what I am doing wrong.

For extra detail I am trying to do this through xampp using phpmyadmin.

Okay, I fixed it just incase anyone looks back at this essentially the issue was my misunderstanding of the instruction 'open the php'. I was opening it directly to the browser, I needed to open it via localhost. so localhost/connect.php rather than just opening connect.php to the browser.

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用php从数据库获取数据到网站。 当我尝试连接时,我正在使用此代码:

 &lt;?php 
error_reporting(-1); 
ini_set('display_errors','On')  ; 
 $ db_host ='localhost'; 
 $ db_user ='root'; 
 $ db_pass =''; 
 $ db_name ='test'; 
 $ conn = new mysqli($ db_host,$ db_user,  $ db_pass,$ db_name); 
if($ conn-&gt; connect_errno)echo“无法连接到数据库”; 
?&gt; 
   
 
 

但是在 试图打开php文件我在firefox上收到了这个:

  connect_errno)echo“无法连接到数据库”;  ?&GT;  (在Firefox上)
   
 
 

这在opera / chrome上:

 &lt;?php 
error_reporting( -  1); 
ini_set('display_errors','On'); 
 $ db_host ='localhost'; 
 $ db_user ='root'; 
 $ db_pass =''; 
 $ db_name ='test';  
 $ conn = new mysqli($ db_host,$ db_user,$ db_pass,$ db_name); 
if($ conn-&gt; connect_errno)echo“无法连接到数据库”; 
?&gt;  
   
 
 

我也试过用Internet Explorer加载它,但它只是试图再次保存它。 我不明白我做错了什么。

有关额外的细节,我试图通过使用phpmyadmin的xampp来做到这一点。

好吧,我修复了它只是因为任何人都回头看这个本质上问题是我对“打开php”指令的误解。 我是直接打开它到浏览器,我需要通过localhost打开它。 而不是localhost / connect.php而不是只是打开connect.php到浏览器。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doumao8803 2017-04-25 04:00
    已采纳

    1) Make sure the extension of file is ".php"

    2) Make sure that you are running page from "http://localhost//" or your local host ip.

    You can test your apache/xampp by visiting "localhost" or "localhost:8080" if it is running on 8080 port.

    点赞 评论

相关推荐 更多相似问题