无法在PHP中创建数据库
我开始创建我自己的接口来使用MySql,尽管我似乎无法使用底部的代码创建数据库。其他一切都可以正常工作,我可以回显$AcquinDatabase变量,以查看它是否存储了值。任何建议都很好无法在PHP中创建数据库,php,mysql,prepared-statement,Php,Mysql,Prepared Statement,我开始创建我自己的接口来使用MySql,尽管我似乎无法使用底部的代码创建数据库。其他一切都可以正常工作,我可以回显$AcquinDatabase变量,以查看它是否存储了值。任何建议都很好 <?php session_start(); //define connection $conn = new mysqli('localhost', 'over_watch','XXXXXXx','billing'); //Variables $UserEmai
<?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';
}
?>
你能试试这个代码吗
有关此错误的详细信息可以在mysql文档中找到。阅读这些细节可以清楚地看出,在同一连接上执行另一条准备好的语句之前,需要完全获取一条准备好的语句执行的结果集
这是你能找到的文件
我是否缺少定义$AcquinDatabase
的地方?我记得不久前看过这篇文章。那是怎么回事?另外,关于您的问题,请添加错误报告,这样您就可以知道您的代码出了什么问题。@smith我正在通过prepare语句(如果创建了,请检查),从数据库中选择它,然后使用bind_result()和fetch()获取值。请打印回显$ObtainDatabase时得到的输出。我们需要查看create table语句以确定其不起作用的原因。@SeanW333$ACCOUTAINDATABASE变量实际上是在代码顶部定义的$MysqlUserDataBaseCreate变量。在我使用了准备好的语句绑定结果函数后,我更改了变量名。是的,谢谢!!这个作品你介意和我分享你的见解吗?@jim。我已经给了一个链接,你可以参考更多信息
<?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';
}
?>