【问题标题】:How to connect to this database? I can't connect如何连接到这个数据库?我无法连接
【发布时间】:2016-11-29 22:38:17
【问题描述】:



这是来自我的 phpmyadmin 的图像。我无法在 phpmyadmin 中创建数据库,我必须通过 cPanel 创建它们,然后它会像这样出现在 phpmyadmin 中。但我无法将它们连接到我的 php 应用程序。

无论我做什么,它都无法连接到 sql server。但在我的计算机上的本地主机上正常工作。

代码如下,

<?php
// Connection
$conn = mysql_connect("localhost", 'root', '');
$db = mysql_select_db("grplusbd_mychat", $conn);
$json = '';
if(isset($_GET['rq'])):
    switch($_GET['rq']):
        case 'new':
            $msg = $_POST['msg'];
            $myid = $_POST['mid'];
            $fid = $_POST['fid'];
            if(empty($msg)){
                //$json = array('status' => 0, 'msg'=> 'Enter your message!.');
            }else{
                $qur = mysql_query('insert into msg set `to`="'.$fid.'", `from`="'.$myid.'", `msg`="'.$msg.'", `status`="1"');
                if($qur){
                    $qurGet = mysql_query("select * from msg where id='".mysql_insert_id()."'");
                    while($row = mysql_fetch_array($qurGet)){
                        $json = array('status' => 1, 'msg' => $row['msg'], 'lid' => mysql_insert_id(), 'time' => $row['time']);
                    }
                }else{
                    $json = array('status' => 0, 'msg'=> 'Unable to process request.');
                }
            }
        break;
        case 'msg':
            $myid = $_POST['mid'];
            $fid = $_POST['fid'];
            $lid = $_POST['lid'];
            if(empty($myid)){

            }else{
                //print_r($_POST);
                $qur = mysql_query("select * from msg where `to`='$myid' && `from`='$fid' && `status`=1");
                if(mysql_num_rows($qur) > 0){
                    $json = array('status' => 1);
                }else{
                    $json = array('status' => 0);
                }
            }
        break;
        case 'NewMsg':
            $myid = $_POST['mid'];
            $fid = $_POST['fid'];

            $qur = mysql_query("select * from msg where `to`='$myid' && `from`='$fid' && `status`=1 order by id desc limit 1");
            while($rw = mysql_fetch_array($qur)){
                $json = array('status' => 1, 'msg' => '<div>'.$rw['msg'].'</div>', 'lid' => $rw['id'], 'time'=> $rw['time']);
            }
            // update status
            $up = mysql_query("UPDATE `msg` SET  `status` = '0' WHERE `to`='$myid' && `from`='$fid'");
        break;
    endswitch;
endif;

@mysql_close($conn);
header('Content-type: application/json');
echo json_encode($json);
?>

【问题讨论】:

  • 数据库名称:grplusbd_mychat 就够了
  • 我相信您的数据库应该使用“grplusdb_mychat”来调用。在图像中,您会看到 phpmyadmin 使用的 UI 表示,以更友好的方式向您展示 db 列表
  • 据我所知,在 cpanel 中,您无法创建名称为 root 的用户。并且密码不能为空。你缺少这些东西。检查你的 cpanel。
  • 以上两个cmet是你的主要问题。您还使用了贬值的 mysql 函数,并且很容易受到 sql 注入攻击
  • 看看很多很多关于在 localhost 连接到 mysql 的问题(提示:尝试 127.0.0.1 而不是 localhost - 就 mysql 而言,它们不一样)

标签: php mysql phpmyadmin cpanel


【解决方案1】:

您需要使用 CPanel 中的 MySQL® 数据库 链接来创建一个 MySQL 用户,然后可以通过 PHP 脚本调用该用户。与 CPanel 生成 MySQL 表的方式相同,CPanel 界面也生成 MySQL 用户帐户。

您可能还需要在同一帐户创建页面中将新帐户与一个或多个数据库相关联。那么:

mysql_connect("localhost", 'grplusbd_usernm', 'password');

用户名 - 类似于数据库名称 - 以帐户参考为前缀,在本例中为 grlusbd

CPanel 不会让 MySQL 根用户通过第三方脚本访问数据库。 root 是专门为 CPanel 保留的(我想也是服务器命令行)。

附言。 mysql_select_db("grplusbd_mychat", $conn); 是在您的问题实例中引用数据库名称的正确方法。无需为grplusbd/ 生气。

注意:

您不应再在 PHP 中使用 MySQL_ 函数,而应使用 MySQLi_PDO 数据库交互函数。 MySQL_ 现在已弃用,并从 PHP7 中删除。

尽快停止使用它,改用 MySQLiPDO


我的参考:CPanel 版本 56.0

【讨论】:

    【解决方案2】:

    在 cpanel 中创建一个新用户并将 db 分配给该用户,或者如果您已经创建了用户,请将 db 分配给该用户

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-26
      • 2018-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-13
      • 2012-10-29
      • 2011-06-16
      相关资源
      最近更新 更多