I want to create a table in a MySQL Database with PHP. This is my try:
$dbhost = 'rdbms.strato.de';
$dbusername = 'Userxxx';
$dbuserpass = 'Passwordxxx';
$dbname = 'DBxxx';
$link_id = mysql_connect ($dbhost, $dbusername, $dbuserpass);
echo "success in database connection.";
if (!mysql_select_db($dbname)) die(mysql_error());
echo "success in database selection.";
$result = "CREATE TABLE address_book (first_name VARCHAR(25), last_name VARCHAR(25), phone_number VARCHAR(15))";
if (mysql_query($result)){
echo "TABLE created.";
}
else {
echo "Error in CREATE TABLE.";
}
But this give me the error
success in database connection.Access denied for user XXX to database XXX
I search a lot but find no successfull solution. Have anyone an idea?
Try connecting without involving PHP:
mysql -u Userxxx -h rdbms.strato.de -pPasswordxxx
If this doesn't work, and I suspect it won't, contact your database administrator to reset your password.
If the password isn't the problem, the remote machine may not be ready to accept your connection. See Access remote database from command line for more on this.
If you can get in, then try to access the database you want:
USE DBxxx;
SHOW TABLES;
Thing is, PHP isn't your problem here; it's the connection. So that's what to investigate.
...and when you get that established, you might look into updating your code; mysql_connect and mysql_select_db are deprecated.
I need some clarification about your hostname,username,password,database name.How ever following code will works in my local server.If you have any doubt about my code please share your host name,password,dbname,etc.
$dbhost = 'localhost';
$dbusername = 'root';
$dbuserpass = '';
$dbname = 'test';
$link_id = mysqli_connect ($dbhost, $dbusername, $dbuserpass,$dbname);
echo "success in database connection.";
$result = "CREATE TABLE address_book (first_name VARCHAR(25), last_name VARCHAR(25), phone_number VARCHAR(15))";
if (mysqli_query($link_id,$result)){
echo "TABLE created.";
}
else {
echo "Error in CREATE TABLE.";
}
Related
I have a problem connecting to sql database:
<?php
$servername = "localhost";
$username = "";
$password = "";
$myDB = "udemy";
// Create connection
$link = mysqli_connect($servername, $username, $password, $myDB );
if (mysqli_connect_error()){
die ("There was an error connecting to the database");
}
$query = "SELECT * FROM users";
if (mysqli_query($link, $query)){
echo "Query was successfull"
}
?>
Error is showing when I try to connect database named "udemy"...
Set username and password or add a new user in your mysql server and try with those user details.
To create new user :-
To create user in MySQL/MariaDB 5.7.6 and higher, use CREATE USER syntax:
CREATE USER 'new_user'#'localhost' IDENTIFIED BY 'new_password';
then to grant all access to the database (e.g. my_db), use GRANT Syntax, e.g.
GRANT ALL ON my_db.* TO 'new_user'#'localhost';
Where ALL (priv_type) can be replaced with specific privilege such as
SELECT, INSERT, UPDATE, ALTER etc
Then to reload newly assigned permissions run:
FLUSH PRIVILEGES;
Now set newly created username and password in your code.
Hope it helped.
I'm trying to use Firebird in my PHP application. I already managed to make interbase and Firebird PDO extensions work, and the connection is established without any problems.
But when I try to make a simple select into one of the tables (select * from filial), it always returns empty, just like there's nothing recorded in the table.
I already tested my script in another database, and it worked properly, so I guess it's not a PHP problem, but I think it has something with my database.
This is how I created the database and table with ISQL:
create database 'C:\My\Database\Path.fdb' page_size 4096 user 'myuser' password 'mypass' default character set UTF8;
connect 'C:\My\Database\Path.fdb' user 'myuser' password 'mypass';
create table filial (id int not null primary key, nome varchar(45));
insert into filial (id, nome) values (1, 'test');
When I run the 'select' query in ISQL, it returns the one inserted row. But doing it with interbase or PDO, I get an empty object.
I also tried using capital letters for the table and columns names.
What am I doing wrong?
I'm running this project in a Windows 7, with WAMP and Firebird server installed.
Interbase PHP code:
$db_server = 'localhost';
$db_user = 'SYSDBA';
$db_passw = 'masterkey';
$db_name = 'C:\Users\Joao\Firebirds\Desenvolvimento.fdb';
$host = $db_server.':'.$db_name;
$dbh = ibase_connect($host, $db_user, $db_passw);
$stmt = 'select * from filial';
$sth = ibase_query($dbh, $stmt);
while ($row = ibase_fetch_object($sth)) {
echo $row->ID.'<br />';
}
ibase_free_result($sth);
ibase_close($dbh);
PDO PHP code:
$db_server = 'localhost';
$db_user = 'SYSDBA';
$db_passw = 'masterkey';
$db_name = 'C:\Users\Joao\Firebirds\Desenvolvimento.fdb';
$str_conn='firebird:host='.$db_server.';dbname='.$db_name;
$conn = new PDO($str_conn, $db_user, $db_passw);
$q = $conn->prepare('SELECT * FROM filial;');
$q->execute();
$dados = $q->fetchAll(PDO::FETCH_OBJ);
foreach($dados as $row){
echo $row->ID.'<br/>';
}
As I'm working locally, I also put connection data.
So I've been trying to learn how to use MySQL with PHP, and I've managed to create a connection and create a database along with a table. What I don't know how to do is create the database along with the tables all in one go.
What I mean by this is easier shown in my code (Which will show unable to connect error message because the connect method is trying to connect to a database that does not exist.
<?php
$servername = isset($_POST["servername"]) ? $_POST["servername"] : '';
$username = $_POST["username"];
$password = $_POST["password"];
$dbname = $_POST["dbname"];
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Create database
$sql = "CREATE DATABASE myDB";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $conn->error;
}
// sql to create table
$sql = "CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";
if (mysqli_query($conn, $sql)) {
echo "Table MyGuests created successfully";
} else {
echo "Error creating table: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
So, all I am trying to achieve is Connect to MySQL, create the database, create a table for said database and close the connection all within one .php file.
On a side note, due to the user being able to define a database name ($dbname), how would I add this value into the MySQL code above? I heard somewhere that you're supposed to add the variable into quotes? So '$dbname'. Any help with that would be good too! Thanks in advance!
Okay, the reason for this question is because I am creating a setup-type page where the user will be able to connect to their own database, allowing them to give it a name and connect using their credentials. Obviously I am not very experienced within this field, I hope I have explained it better.
All the code you have looks fine to me. The only thing I think your missing is after you create a database you have to call
$conn->select_db("myDB");
Also if you want to have the database name be $dbname then
$sql = "CREATE DATABASE myDB";
should be
$sql = "CREATE DATABASE " . $dbname;
If I didn't cover your problem please give me more detail on your problem.
where you passing all of this variable ?
$servername = isset($_POST["servername"]) ? $_POST["servername"] : '';
$username = $_POST["username"];
$password = $_POST["password"];
$dbname = $_POST["dbname"];
just simply hardcode the servername, username, password and your dbname.
My PHP code
$handle = mysql_connect();
echo ($handle) ? "Connected to mysql" : "Could not connect";
$db = mysql_select_db("users");
if ($db ==false) {
die(' Could Not select database so Exiting;');
} else {
echo " Database selected;";
}
mysql_close($handle);
This code works with default mysql databases like "test" or "mysql".
My Data Base creation code sequence:
mariadb[none]> create database users;
mariadb[none]> use users;
mariadb[users]> create table registered_users ( user_name varchar(18), user_pass varchar(18) );
You should use mysqli_ functions instead:
$localhost=''; //SQL Host
$dbuser=''; //Database Username
$dbpass=''; //Database Password
$dbname=''; //Database Name
$connect=mysqli_connect($localhost,$dbuser,$dbpass,$dbname);
mysql_ functions are deprecated.
What's basically happening is you are connected to test by default please supply parameters mysql_connect("localhost", "username", "password") ;
Should work with no doubt.
I want to know how to connect to a MySql database in a remote server from windows 7 .I want to give an external connection from Administrative panel.How to give connection to the database ? I have a php coding to insert and retrieve values from the remote database .But before that i need to connect to the remote database manually ? Pls can you help me out with this .#
<?PHP
$user_name = "fbapp";
$password = " 123";
$database = "fb_db";
$server = "mysql.dfw1.stabletransit.com";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found) {
$SQL = "INSERT INTO Sample(Firstname, Lastnamme)
VALUES (35,67)");
$result = mysql_query($SQL);
mysql_close($db_handle);
echo "Records added to the database";
}
else {
echo "Database NOT Found ";
mysql_close($db_handle);
}
?>
Simply create a mysql user and allow the same from your host to connect. So create user at
example1.com MYSQL and allow your new user to connect from your example2.com.
Then allow a firewall rule on mysql port from that very host
open command prompt
mysql -u user_name -ppassword -h ip-adress
use ip-adress of machine where mysql is running