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

为什么我的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.

  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵