dongzhi5386 2017-04-24 19: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-24 20: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 vue请求不到数据,返回状态200,数据为html
  • ¥15 访问url时不会自动调用其 Servlet的doGet()
  • ¥15 用白鹭引擎开发棋牌游戏的前端为什么这么难找
  • ¥15 MATLAB解决问题
  • ¥35 哪位专业人士知道这是什么原件吗?哪里可以买到?
  • ¥15 关于#c##的问题:treenode反序列化后获取不到上一节点和下一节点,Fullpath和Handle报错
  • ¥15 一部手机能否同时用不同的app进入不同的直播间?
  • ¥20 输入import torch显示Intel MKL FATAL ERROR,系统驱动1%,: Cannot load mkl_intel_thread.dll.
  • ¥15 点云密度大则包围盒小
  • ¥15 nginx使用nfs进行服务器的数据共享
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部