I'm encountering an issue when I try to use php to create a database. I've google around, ensured that the user has permission to create a database and it still doesn't work.
I'm able to use PHP to create and modify tables once I create the database manually, but I'd like this code to run in PHP for personal reasons.
Here's the code, just in case I'm missing something.
*edit: grammar
<?php
$servername = "localhost";
$username = "user";
$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 inkflow_backend";
if ($conn->query($sql) === TRUE){
echo "Database created successfully, continue.";
} else{
echo "Error creating database: " . $conn->error . "Please contact the developer or your network administrator.";
}
//Close connection
$conn->close();
?>
Review the quotes that you're using when printing a link to setup2.php.
They are causing a syntax error. Just putting a backslash before them would solve the problem:
<a href=\"setup2.php\" ...
Related
I've looked all over here. Please be patient as I am new to php and mysql.
I got WAMPP installed & seems to be working OK. I created a simple "test" database from phpMyAdmin and "firsttable" in that. I can do a simple connect using example from w3schools, but trying to select & display data I entered only throws back errors.
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Connect
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT reference, firstname, lastname, room FROM firsttable";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["reference"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "room:" . $row["room"]. "<br>";
}
} else {
echo "0 results";
}
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$conn->close();
?>
First off, I get a parse error on line 17. The one that reads:
if ($result->num_rows > 0) {
The error says: Trying to get property of non-object.
I tried wrapping the whole php code in tags and saving it as html, but then it appeared that no row data was ever found.
I am able to use very simple code that connects successfully. I can confirm the database is in there, so is the table, and the contents I added to it.
Please, what am I doing wrong?
You need to specify the database when you connect:
$database = 'test';
$conn = mysqli_connect($servername, $username, $password, $database);
where $database is the name of your database (test in this case). MySQL doesn't know which database your table resides in without you telling it.
In addition, you should always include error checking for your database connection (you have two of these, you don't need the last one) as well as any queries. Sans this, you can check your error logs for more information when something fails.
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 read a bunch of the other posts about similar issues but I am very new to this so I am likely missing something. Most of the other questions I found were a lot more complicated than mine. I am trying to follow the w3schools tutorial for this and am testing locally using XAMPP. Right now I am just trying to get this to work successfully and eventually I am planning to submit data into a mySQL database from a web form.
<?php
$servername="localhost";
$dbname="mysql";
// Create connection
$conn = mysqli_connect($servername, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO owner_table (ownerFirst, ownerLast, mobile)
VALUES ('John', 'Doe', '1111111111')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
NOTE:
The connection is not failing from what I can tell. Removing everything after the first if statement, and adding else { echo "success" } outputs success. Again though, I am new to PHP.
If someone knows that the answer has been given in another post PLEASE PLEASE comment here and ill close this out.
http://www.w3schools.com/php/php_mysql_insert.asp
You should follow the proper syntax:
$conn = mysqli_connect(<host Name>, <username>, <password>, <database name>);
Reference: http://php.net/manual/en/function.mysqli-connect.php#refsect1-function.mysqli-connect-examples
Add two more parameters- username and password here:
$username = "root"; // Default values
$password = ""; // Default values
$conn = mysqli_connect($servername, $username, $password, $dbname);
I am new in PHP and would need some explanation. Here is a code where we connect to MySQL with PHP. Can you please explain me where is the statement that makes the connection? I can see only that we define what the value of $conn is, but does it mean execution as well? The other thing is: where do we create the database? I can see that we give the string "CREATE DATABASE myDB" as a value to $sql and we have an if statement, but does the expression ($conn->query($sql) === TRUE) also evaluated? It is strange for me, can somebody please explain it to me?! :) Thanks!
<?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();
?>
Here is a simple explanation of which lines do what. If you would like to know specifically what the individual parts of these mean, then please say which ones so they can be further explained to you. Or the correct links pointed to.
I notice that you are using the W3Schools example, as an almost exact copy and paste. Have you installed MySQL on your machine and created a username and password?
<?php
$servername = "localhost"; // This is the location of your server running MySQL
$username = "username"; // This is the username for MySQL
$password = "password"; // This is the password for MySQL
// Create connection
$conn = new mysqli($servername, $username, $password); // This is where you create a connection
// Check connection
if ($conn->connect_error) { // This checks if the connection happened
die("Connection failed: " . $conn->connect_error); // and produces an error message if not
} // otherwise we move on
// Create database
$sql = "CREATE DATABASE myDB"; // This is the SQL query which is sent to the MySQL server
if ($conn->query($sql) === TRUE) { // When the if statement begins here, it executes the query and test if it returns true
echo "Database created successfully"; // If it returns true then here is the message is returns
}
else {
echo "Error creating database: " . $conn->error; // Or if there was error with the query this is returned
}
$conn->close(); // Close the connection when it is no longer in use
?>
Although, your question does not belong here (This place is to help with your coding issues), but I will give you a bit explanation.
PHP reads each line and EXECUTES It. the create connection part opens a new connection using the "new" object and save it a variable ($conn),
($conn->connect_error) checks if the connection was successful with connect_error property. if it was connected, continue, or else through and error and stop.
If connection was successful, then create the database based on connection opened in variable ($conn).
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!