本文介绍了连接到托管服务器上的数据库时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

警告:mysql_select_db():中没有这样的文件或目录 /home/vol14_1/byethost31.com/b31_16461744/htdocs/Mysql/con.php在线 8

Warning: mysql_select_db(): No such file or directory in /home/vol14_1/byethost31.com/b31_16461744/htdocs/Mysql/con.php on line 8

警告:mysql_select_db():无法链接到服务器 建立在 /home/vol14_1/byethost31.com/b31_16461744/htdocs/Mysql/con.php在线 8

Warning: mysql_select_db(): A link to the server could not be established in /home/vol14_1/byethost31.com/b31_16461744/htdocs/Mysql/con.php on line 8

我有以下代码

<?php
     $localhost="localhost";
     $username=b31_16461744;
     $pass=test123;
     $dbname=b31_16461744_user;
     $a= mysqli_connect($localhost,$user,$pass);
     mysql_select_db($dbname);
     if($a)
     {
       echo "connected..";
     }
     else
     {
       echo "not...!!";
     }
?>

推荐答案

侧注:假设凭据是正确的,由您的虚拟主机提供.

Sidenote: Assuming the credentials are correct, given to you by your web host.

此代码存在一些问题(摘自您的评论).

There are several problems with this code (taken from a comment you left).

首先,您的三个声明未加引号,而是被视为常量.

Firstly, three of your declarations are not quoted and are being treated as constants.

PHP错误报告会抛出未定义常量的通知.

PHP error reporting would have thrown notices of undefined constants.

这些被视为常量:

 $username=b31_16461744;
 $pass=test123;
 $dbname=b31_16461744_user;

您还为用户名$user引用了错误的变量,该用户名应为$username.错误报告会签署未定义的变量通知.

You are also referencing the wrong variable for the username being $user which should be $username. Error reporting would have signabled an undefined variable notice.

然后您将mysql_mysqli_语法混合.那些不同的MySQL API不会相互混合.您必须在整个代码中使用同一代码.

Then you're mixing mysql_ with mysqli_ syntax. Those different MySQL APIs do NOT intermix. You must use the same one throughout your code.

旁注:您发布的另一个问题用户'test123'@'192.168.0.38'的访问被拒绝(使用密码:NO)您正在使用sql306.byethost31.com作为主机.确保这是正确的.我不知道主机要您使用什么设置.

Sidenote: The other question you posted Access denied for user 'test123'@'192.168.0.38' (using password: NO) you are using sql306.byethost31.com for the host. Make sure that is correct. I have no idea what settings that host wants you to use.

<?php
     $localhost="localhost";
     $username="b31_16461744";
     $pass="test123";
     $dbname="b31_16461744_user";
     $a= mysqli_connect($localhost, $username, $pass);
     mysqli_select_db($a, $dbname);
     if($a)
     {
       echo "connected..";
     }
     else
     {
       echo "not...!!";
     }
?>

或仅使用所有四个参数:

or just use all four parameters:

<?php
     $localhost="localhost";
     $username="b31_16461744";
     $pass="test123";
     $dbname="b31_16461744_user";
     $a= mysqli_connect($localhost, $username, $pass, $dbname);

     if($a)
     {
       echo "connected..";
     }
     else
     {
       echo "not...!!" . mysqli_error($a);
     }
?>

但是,带有回显的else不能帮助您.使用mysqli_error()获取实际错误.

However, your else with the echo does not help you. Use mysqli_error() to get the real error.

即:or die("Error " . mysqli_error($a));

手册中的示例

$link = mysqli_connect("myhost","myuser","mypassw","mydb")
        or die("Error " . mysqli_error($link));

参考:

  • http://php.net/manual/en/function.error-reporting.php
  • http://php.net/manual/en/mysqli.error.php
  • http://php.net/manual/en/function.mysqli-connect.php
  • http://php.net/manual/en/language.constants.php

向其中添加 错误报告 文件顶部,这将有助于查找错误.

Add error reporting to the top of your file(s) which will help find errors.

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

边注:显示错误只能在舞台上进行,绝不能制作

Sidenote: Displaying errors should only be done in staging, and never production

这篇关于连接到托管服务器上的数据库时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 15:47