CREATE DATABASE 'some database name' ;
Works as expecting using mysql's client.
The same query (different database name) from php/mysqli - fails kind of.
It writes to the INFORMATION_SCHEMA.SCHEMATA
But fails to write to the mysql.db table?
This is actually an issue of a larger problem.
What is going on. Why does my mysqli fail?
code:
$mysqli = new mysqli("p:127.0.0.1", "root", "showtech123", "mysql");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
if(!$mysqli->query( "CREATE DATABASE $database_name;"))
{echo "DATABASE encountered error:" . $mysqli->error . "<br /";}
Errors:
db_name: terarydatabase3333, un:teraryuser5999
Warning: mysqli::query(): MySQL server has gone away in /www/admin.showtechllc.com/public_html/adddb.php on line 21
Warning: mysqli::query(): Error reading result set's header in /www/admin.showtechllc.com/public_html/adddb.php on line 21
USER encountered error:MySQL server has gone away
the fourth parameter in the constructor is the database you want to use.
you're actually trying to create a new database inside an existing one.
try this :
$mysqli = new mysqli("p:127.0.0.1", "root", "showtech123");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}else{
echo "connected";
}
if(!$mysqli->query( "CREATE DATABASE $database_name;")){
echo "DATABASE encountered error:" . $mysqli->error . "<br /";
}else{
echo $database_name . "created";
}
Related
I have Apache (with PHP) and MySQL installed and running on my Raspberry Pi. I've done some simple tests with PHP, and it seems to be working. And MySQL is working perfectly from the terminal, and even another computer.
I made this PHP file:
<?php
echo("Connecting");
$connection = new mysqli("127.0.0.1", "admin", "password", "test");
if ($connection->connect_error) {
die("Connection error");
}
echo("Connection successful");
?>
Yet when I go to this page in my web browser, all I see is "Connecting". If I comment out the connection command, I see "Connecting Connection successful" in the browser.
It seems as if the PHP code stops running or hangs at the connection command.
Any ideas why I'm having this strange behavior?
Try adding these lines at the top: error_reporting(E_ALL);
ini_set('display_errors',1);
I hope You should do following
if ($connection->connect_error) {
echo "Connection error";
}else{
echo "Connection successful";
}
Try this instead
<?php
$link = mysqli_connect("127.0.0.1", "admin", "password", "test");
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
mysqli_close($link);
?>
This is my code to connect to Azure:
<?php
$servername='myproject.database.windows.net';
$username='somename';
$password='somepassword';
$database='some_db';
$conn=New mysqli($servername,$username,$password,$database);
//Connection error handling:
// echo #mysqli_ping($conn) ? 'true' : 'false';
if(mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
} else {
// echo "Connection success";
}
?>
I get the error:
Connection failed: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
I have tried launching this php from a WAMP server and from azurewebsites but both do not seem to work. It works with PDO but I have some pages already setup with mysqli_fetch_assoc queries and would like to preserve them.
Any suggestions?
I have a file login.php to try to connect to my database.
I put the file login.ph inside the server folder and started the server.
Then i call the file in the browser and it shows a blank page. It does not respond even if i change the values of the database to an incorrect value.
I don't know if the error is inside the code or if it is another problem.
Thanks.
login.php:
<?php
$username = $_GET['fname'];
$password = $_GET['fpass'];
$con=mysqli_connect("localhost","user","pass","db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$qz = "SELECT contact_id FROM contacts" ;
$qz = str_replace("\'","",$qz);
$result = mysqli_query($con,$qz);
while($row = mysqli_fetch_array($result))
{
echo $row['contact_id'];
}
mysqli_close($con);
?>
You must check $con variable that you have set with the result of the connection:
if (!$con) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
Do you have web server (say apache) installed and running on your server? You have to. Then put your file on the web server folder (say /var/www/html) and test in the browser.
I have been banging my head against a wall for hours now, reading several answers on SO, searching the 'net, trying to find a solution. While I realize similar questions have been asked and answered here, my code is different so I can't extrapolate from previous answers what's wrong with mine.
I'm new to PHP/MySQL. I have a poll script that I patched together using some code I found online and my own knowledge; the MySQL is mine, standard stuff. Each time a person votes for one of two things, its corresponding value in the database in increased by one. I'm trying to retrieve and assign each value to its own variable and perform a calculation. I can retrieve the first one just fine, but I get the dreaded
Trying to get property of non-object in..." error and
"Fatal error: Call to a member function bind_param() on a non-object in...
for the second one. It's exactly the same as the first prepare, so I'm not sure what's wrong? This may not be the most efficient way to do this, but like I said, I'm new to this. I DO understand what the error is trying to convey; I just don't understand why I'm getting the error when the prepare statement is identical to the one before it except for the id in the WHERE clause. var_dump($query2) returns bool(false), so I can clearly see nothing is there. But there IS something in the database.
<?php
// Turn on error reporting
ini_set('display_errors', 'On');
// Connect to database
$mysqli = mysqli_connect('localhost', 'dbuser', 'dbpwd', 'dbname');
// Check connection
if (!$mysqli || $mysqli->connect_errno)
{
echo "Connection error: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
exit();
} else if (isset($action)) // This is set through a page redirect
{
// DISPLAY ITEM - THIS WORKS FINE
if (!($query = $mysqli->prepare("SELECT votes FROM influential WHERE id = 1")))
{
echo "Prepare failed: " . $query->errno . " " . $query->error;
}
if (!$query->execute()){
echo "Execute failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if (!$query->bind_result($infl1))
{
echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if ($query->fetch())
{
echo $infl1;
}
// DISPLAY ITEM - THIS DOESN'T WORK
if (!($query2 = $mysqli->prepare("SELECT votes FROM influential WHERE id = 2")))
{
echo "Prepare failed: " . $query2->errno . " " . $query2->error;
} else echo "Prepare succeeded.";
$query2->bind_param("i", $votes);
if (!$query2->execute()){
echo "Execute failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
} else echo "Execute succeeded.";
if (!$query2->bind_result($infl2))
{
echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
} else echo "Bind succeeded.";
if ($query2->fetch())
{
echo $infl2;
}
exit();
$mysqli->close();
}
?>
In mysqli, when you issue the command:
.bind_param()
you are binding parameters to placeholders in the original query, not in the result. Your select query should read:
SELECT `votes` FROM `influential` WHERE `id`=?;
Then, you use bind_param to bind an integer to the placeholder, thusly:
.bind_param('i', $id);
After calling .execute(), the result is bound using references by calling .bind_result(). Whatever variable you pass into .bind_result() will contain the data afterwards.
Cheers
I was running an example from a mysqli tutorial.
$mysqli = new mysqli("localhost", "user", "password", "database");
if($mysqli->connect_errno)
{
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ) "
. $mysqli->connect_error;} echo $mysqli->host_info . "\n";
}
I was getting the error:
Warning: mysqli::mysqli(): (42000/1049): Unknown database 'world' in /home/...
What should I do to create the database?
You have to use this:
$mysqli=new mysqli("localhost","user","password") or die(".........");
if($mysqli->query("Create database if not exists MyDB")){
"Failed creating new database: ".$mysqli->connect_errno();
die();
}
$mysqli->select_db("MyDB");
$mysqli->close();
try something like this
$mysqli = new mysqli("localhost", "user", "password");
in this way you are connected to database
and put this query using mysqli Create Database database1