duandu8707 2015-12-19 05:55
浏览 6
已采纳

下面的PHP代码对我不起作用

I am trying to create a new user to my mysql database and login with it to create a new database. I tried the following code. But it results some errors.

<?php

$mysqli=new mysqli("localhost","username","password@123") or die(".........");

$mysqli->query("CREATE USER 'new_username'@'localhost' IDENTIFIED BY '123456';");
$mysqli->query("GRANT ALL ON db1.* TO 'new_username'@'localhost'");

$mysqli->close();

$mysqli=new mysqli("localhost","new_username","123456") or die(".........");

if($mysqli->query("Create database if not exists New_DB_Name")){

   "Failed creating new database: ".$mysqli->connect_errno();
    die();

}

$mysqli->select_db("New_DB_Name");
$mysqli->close(); ?>

and here is the errors

    Warning: mysqli::mysqli(): (28000/1045): Access denied for user 'new_username'@'localhost' (using password: YES) in /home/public_html/sign-up/conn.php on line 10

Warning: mysqli::query(): Couldn't fetch mysqli in /home/public_html/sign-up/conn.php on line 12

Warning: mysqli::select_db(): Couldn't fetch mysqli in /home/public_html/sign-up/conn.php on line 19

Warning: mysqli::close(): Couldn't fetch mysqli in /home/public_html/sign-up/conn.php on line 20

Edit: Actually its not creating new user. that's the trouble here.

  • 写回答

3条回答 默认 最新

  • doujia4041 2015-12-19 07:04
    关注

    Check your database permissions. The error says that sql user username does not have create user permission.

    Creating a user is an unusual thing to do from a script, unless you want to be creating many, many users in an automated way.

    Probably you want to use the admin interface to create needed username once and then use PHP scripting to create all the aspects of schemas: databases, tables, indexes, etc.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化