I am trying to have a database created for each user that signs up for my web app. This database will be based on a template to store their data.
The problem I'm running into is that Google Could SQL doesn't seem to be creating the Database via PHP and MySQL queries.
Connecting to the Instance works fine and triggers no errors based on this code:
//CLOUD SQL CONNECTION FOR SUBSCRIPTIONS
$hostname = details;
$username = details;
$password = details;
$port = details;
$socket = details;
$connection = new mysqli($hostname, $username, $password, $port, $socket);
// Check Connection
if($connection->connect_error) {
trigger_error('Connection failed: ' . $connection->connect_error, E_USER_ERROR);
}
However, when I go to create a simple database, mysqli fails without an error...:
//Create Database
$username = 'account';
$database = 'sub_'. $username .'_db';
$query = "CREATE DATABASE IF NOT EXISTS `{$database}`;";
if(mysqli_query($connection, $query)) {
echo "Database my_db created successfully";
} else {
echo "Error creating database: " . mysqli_error($connection);
}
The output of this is simply: Error creating database:
Am I missing something? How do I create a database in a Cloud SQL instance via PHP? Thanks for any input.
Can anyone confirm that this can actually be done on GAE PHP and Cloud SQL?
It seems the $connection variable was actually NULL despite not throwing an error. This worked for me though:
I connected to an existing database:
//CLOUD SQL CONNECTION FOR SUBSCRIPTIONS
$hostname = details;
$username = details;
$password = details;
$db = details;
$port = details;
$socket = details;
$connection = new mysqli($hostname, $username, $password, $db, $port, $socket);
// Check Connection
if($connection->connect_error) {
trigger_error('Connection failed: ' . $connection->connect_error, E_USER_ERROR);
}
Then I created a new database:
//Create Database
$username = 'account';
$database = 'sub_'. $username .'_db';
$query = "CREATE DATABASE IF NOT EXISTS `{$database}`;";
if(mysqli_query($connection, $query)) {
echo "Database my_db created successfully";
} else {
echo "Error creating database: " . mysqli_error($connection);
}
Then used mysqli_select_db() to select the newly created database.
//Select Newly Created DB
mysqli_select_db($connection, $database);
Related
I'm working in a web app with PHP and Postgresql. I`m looking for a PHP code to create a database online using a PHP form that sends the name, template, UTF8, etc.
Something like this for MYSQL but for with Postgresql:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Create database
$sql = "CREATE DATABASE myDB";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $conn->error;
}
$conn->close();
?>
SERVER RESPONSE
Error: INSERT INTO epiz_19848473_Liste1 (Rowcount, Level1) VALUES (0, 'any Value')
No database selected
PHP CODE
UPDATE
<?php
$servername = "sql308.epizy.com";
$username = "epiz_19848473";
$password = "huihuibuh";
$dbname = "Liste1";<--UPDATED
// Create connection
$conn = mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO Liste1 <--UPDATED (Rowcount, Level1)
VALUES (0, 'any Value')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
This should work, right?
NEW STATUS
Connection failed: Access denied for user 'epiz_19848473'#'192.168.0.%' to database 'Liste1'
The code is perfectly all right , the problem is with mysql configuration .
Please login to mysql as **root user ** and do the following,
GRANT ALL PRIVILEGES ON . TO 'epiz_19848473'#'192.168.0.%' (use ip of php server)
FLUSH PRIVILIGES;
The above allows the user to connect to mysql table from the ip specified.
you did not defined the database name in the connection
<?php
$servername = "sql308.epizy.com";
$username = "epiz_19848473";
$password = "huihuibuh";
$dbname = "DATABASE_NAME_HERE";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
else you can define define the database name in the connection
$conn = new mysqli($servername, $username, $password, "DATABASE_NAME_HERE");
Just define the dbname,like this:
<?php
$servername = "sql308.epizy.com";
$username = "epiz_19848473";
$password = "huihuibuh";
$dbname = "test"; //You should create this db in mysql
....
You Can Use This:
CREATE TABLE [database_name].[table_name] (
[your_table_things]
);
I've been wondering if it's possible to create a new database using mysqli_query() even though I'm already connected to my database?
$create_new_db = mysqli_query($user_conn, "CREATE DATABASE sample_db");
if($create_new_db) {
echo 'new database has been created';
} else {
echo 'Error while creating new database.';
}
my variable $user_conn has a database connection named db_maindb. So is it still possible to create a new database when I'm connected to my db_maindb database?
Like you have in you code ..
assuming you can connect to an existing database and your connected user has grant for create database you can
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Create database
$sql = "CREATE DATABASE sample_db";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $conn->error;
}
$conn->close();
?>
I have been developing a CRUD application using PHP & MySQL database.
I was succeeded by creating, displaying, updation parts. But I stuck at the deletion part of a row from a database table.
I tried my best solving all the PHP shown errors but now in final it is now showing a message which I wrote to echo in case of failure.
I request someone to please help me with this problem.
Thankyou in advance.
Code I wrote for deletion:
//include database connection
include 'db_connect.php';
//$mysqli->real_escape_string() function helps us prevent attacks such as SQL injection
$query = "DELETE
FROM `grocery`
WHERE `GrocerID` ='".$mysqli->real_escape_string($_GET['id'])."'
limit 0,1";
//execute query
if( $mysqli->query($query) ){
//if successful deletion
echo "User was deleted.";
}else{
//if there's a database problem
echo "Database Error: Unable to delete record.";
}
$mysqli->close();
?>
Code I wrote for delete link in display table:
//just preparing the delete link to delete the record
echo "<a href='delete.php?id={$GrocerID}'>Delete</a>";
Code I wrote for db config:
<?php
//set connection variables
$host = "localhost";
$username = "root";
$password = "secret";
$db_name = "crud"; //database name
//connect to mysql server
$mysqli = new mysqli($host, $username, $password, $db_name);
//check if any connection error was encountered
if(mysqli_connect_errno()) {
die("Connection failed: " . $conn->connect_error);
exit;
}
?>
I tried this and got working, can you update the code and see if this works?
$host = "localhost";
$username = "root";
$password = "secret";
$db_name = "crud"; //database name
//connect to mysql server
$mysqli = new mysqli($host, $username, $password, $db_name);
//check if any connection error was encountered
if(mysqli_connect_errno()) {
die("Connection failed: " . $conn->connect_error);
exit;
}
// Delete row
if ($mysqli->query (sprintf( "DELETE FROM grocery WHERE email = '".$mysqli->real_escape_string($_GET['id'])."' LIMIT 1") )) {
printf ( "Affected Rows %d rows.\n", $mysqli->affected_rows );
}
I hope this helps.
Provide a connection :
if( $mysqli->query($con, $query) ){
I just made my own wamp server on my computer to handle twitch donations for a friend. The friend has a program that I made using c# that connects to the my server and reads the database. This works fine, the program reads my database just fine and lists the entries. BUT I have made a php website to send the entry (a new donation) to the database but I receive a connection timed out error just seconds after I press the button... my mysql server is set to allow every user and host with the correct password. I used the same IP user and password as on the c# program!
I can't figure out why the damn php website hosted on one.com can't connect!!
this is my code:
<?php
if (!empty($_GET['act'])) {
$servername = "81.83.95.34";
$username = "root";
$password = "mypassword";
$dbname = "tbc";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("server failed: " . $conn->connect_error);
}
$sql = "INSERT INTO donations (donations)
VALUES ('1')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
} else {
?>
can anyone help me?
thank you very much!