无法创建数据库PHP

时间:2018-06-29 04:26:39

标签: php mysql prepared-statement

尽管我似乎无法使用底部的代码创建数据库,但我开始创建自己的与MySql一起使用的接口。其他所有方法也都可以,我可以回显$ ObtainDatabase变量以查看是否确实存储了值。任何建议都会很棒。

<?php
    session_start();

    //define connection
    $conn = new mysqli('localhost', 'over_watch','XXXXXXx','billing');

    //Variables
    $UserEmail = $_SESSION['email'];
    $MysqlUserDataBaseCreate  = $_POST['create_database'];

    //CheckIfUserExists
    $SeeIfUserExist = $conn->prepare("SELECT (email) FROM database_users WHERE email= ?;");
    $SeeIfUserExist->bind_param('s',$UserEmail);
    $SeeIfUserExist->execute();

     $SeeIfUserExist->bind_result($ObtainedEmail);
     $SeeIfUserExist->store_result();
     $SeeIfUserExist->fetch();
     $RowsReturnedFromPreparedStatment = $SeeIfUserExist->num_rows();


    if($RowsReturnedFromPreparedStatment < 1){
      $InsertIntoDatabase = $conn->prepare("INSERT INTO database_users(email,check_if_created) VALUES(?,?);");
      $InsertIntoDatabase->bind_param('ss',$UserEmail,$MysqlUserDataBaseCreate);
      $InsertIntoDatabase->execute();

      $SelectDatabaseToCreate = $conn->prepare(" SELECT (check_if_created) FROM database_users WHERE email = ?;");
      $SelectDatabaseToCreate->bind_param('s', $UserEmail);
      $SelectDatabaseToCreate->execute();

      $SelectDatabaseToCreate->bind_result($ObtainDatabase);
      $SelectDatabaseToCreate->fetch();
      $CreateDatabase = "CREATE DATABASE $ObtainDatabase ;";
      $conn->query($CreateDatabase);

    }else{
      echo 'user permitted to one database';
    }


    ?>

1 个答案:

答案 0 :(得分:1)

您可以尝试使用此代码吗?

有关此错误的详细信息可以在mysql文档中找到。阅读这些详细信息可以清楚地了解到,在同一连接上执行另一条预处理语句之前,必须完全获取一条预处理语句执行的结果集。

这是您可以refer

使用的文档
<?php
    session_start();

    //define connection
    $conn = new mysqli('localhost', 'over_watch','XXXXXXx','billing');

    //Variables
    $UserEmail = $_SESSION['email'];
    $MysqlUserDataBaseCreate  = $_POST['create_database'];

    //CheckIfUserExists
    $SeeIfUserExist = $conn->prepare("SELECT (email) FROM database_users WHERE email= ?;");
    $SeeIfUserExist->bind_param('s',$UserEmail);
    $SeeIfUserExist->execute();
    $SeeIfUserExist->store_result();

     $SeeIfUserExist->bind_result($ObtainedEmail);
     $SeeIfUserExist->store_result();
     $SeeIfUserExist->fetch();
     $RowsReturnedFromPreparedStatment = $SeeIfUserExist->num_rows();


    if($RowsReturnedFromPreparedStatment < 1){
      $InsertIntoDatabase = $conn->prepare("INSERT INTO database_users(email,check_if_created) VALUES(?,?);");
      $InsertIntoDatabase->bind_param('ss',$UserEmail,$MysqlUserDataBaseCreate);
      $InsertIntoDatabase->execute();
      $InsertIntoDatabase->store_result();

      $SelectDatabaseToCreate = $conn->prepare(" SELECT (check_if_created) FROM database_users WHERE email = ?;");
      $SelectDatabaseToCreate->bind_param('s', $UserEmail);
      $SelectDatabaseToCreate->execute();
      $SelectDatabaseToCreate->store_result();

      $SelectDatabaseToCreate->bind_result($ObtainDatabase);
      $SelectDatabaseToCreate->fetch();
      $CreateDatabase = "CREATE DATABASE $ObtainDatabase ;";
      $conn->query($CreateDatabase);

    }else{
      echo 'user permitted to one database';
    }


    ?>