I'm getting the MySQL database backup using the php. But currently facing the given error. Anybody can tell me that how can I get rid from this error.
<?php
$db_server="server";
$db_username="username";
$db_password="";
$db_database="database";
$db_tablename="myguests";
$db_connection = mysqli_connect("$db_server","$db_username","$db_password","$db_database");
if(!$db_connection){
die("Database connection error: ".mysqli_errorno());
}
$db_backup= "/db_backup/";
$db_select = "SELECT * INTO OUTFILE '$db_backup' FROM '$db_tablename'";
$retval = mysqli_query($db_connection,$db_select);
if(!$retval){
die(/*"Could not take data backup: "*/mysqli_error($db_connection));
}else{
echo "Database backup successfully done";
}
mysqli_close($db_connection);
?>
You have put your database name in single quotes.
It should be back ticks.
Change:
$db_select = "SELECT * INTO OUTFILE '$db_backup' FROM '$db_tablename'";
To:
$db_select = "SELECT * INTO OUTFILE '$db_backup' FROM `$db_tablename`";
Also, no need to put double quotes here, it will work without them also:
$db_connection = mysqli_connect($db_server, $db_username, $db_password, $db_database);
Related
i'm in a middle of making a query with my new database and i keep getting the message (Database query failed) through the code below:
<?php
//create a database connection
$dbhost= "localhost";
$dbname= "widget_corp";
$connection=mysqli_connect($dbhost , $dbname);
if(mysqli_connect_errno()){
die("Database connection failed :" . mysqli_connect_error ."(". mysqli_connect_errno .")");
}
?>
<?php
//perform a database query
$query = "SELECT * FROM subjects";
$result = mysqli_query($connection ,$query);
if (!$result){
die("Database query failed.");
}
?>
please advise
You aren't passing in a username or password. mysqli_connect() requires four parameters to be passed in : http://php.net/manual/en/function.mysqli-connect.php
You're only passing in a host and database name.
I used
mysqli_connect("infos in here");
at the top of my page, and tried to use
mysqli_query("INSERT INTO and other info here");
When I do that, I get this error:
Warning: mysqli_query() expects at least 2 parameters, 1 given in (...)
But if I instead use
$con = mysqli_connect("infos in here");,
$mysqli_query($con,"INSERT INTO and other info here");
The error goes away, and my script works.
My problem is that I need to use mysqli_query two different times in my page, and I don't want to open the connection again when it's already open.
How can I handle this?
Thanks.
My problem is that I need to use mysqli_query two different times in
my page, and I don't want to open the connection again when it's
already open.
How is it a problem ? open once query as many times then close the connection, example:
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Perform queries
mysqli_query($con,"SELECT * FROM Persons");
//one more
mysqli_query($con,"INSERT INTO Persons (FirstName,LastName,Age)
VALUES ('Glenn','Quagmire',33)");
mysqli_close($con);
?>
No need to connect two times. An example here..
$con = mysqli_connect("host", "user", "password", "db");
if(mysqli_connect_errno()){
die(mysqli_connect_error());
}
//Query one
$result1 = mysqli_query($con, 'Query String');
//Query Two
$result2 = mysqli_query($con, 'Query String'); //Used same $con variable
//After finishing all queries
mysqli_close($con);
If you need to select multiple database then
$con = mysqli_connect("host", "user", "password");
//for DB bd_name1
mysqli_select_db($con, 'bd_name1');
$result1 = mysqli_query($con, 'Query String');
//for DB bd_name2
mysqli_select_db($con, 'bd_name2');
$result2 = mysqli_query($con, 'Query String');
But not connect frequent time.
Don't know if someone can help but for some reason when using call stored procedure that has a select before a dynamic SQL syntax error PHP doesn't report an error. If calling the stored procedure in workbench the error shows.
Basic setup MySQL:
create table testtable (testcolumn varchar(10), PRIMARY KEY (testcolumn));
DELIMITER ///
CREATE PROCEDURE teststoredproc ()
BEGIN
SELECT 'Select text';
SET #sql := 'SELECT * FROM testtable WHERE IFNULL(DATE(testcolumn), ''1900/01/01'' = DATE(''1900/01/01'')';
PREPARE stmtteststoredproc FROM #sql;
EXECUTE stmtteststoredproc;
DEALLOCATE PREPARE stmtteststoredproc;
END
DELIMITER ;
Basic setup php
<?php
$DB_NAME = '';
$DB_HOST = '';
$DB_USER = '';
$DB_PASS = '';
$mysqli = mysqli_connect($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$result = mysqli_query($mysqli, "CALL teststoredproc();") or die("Query fail: " . mysqli_error());
while ($row = mysqli_fetch_array($result)){
echo $row[0];
}
?>
Now instead of the die executing because there is a syntax error in the dymanic SQL, the while will run and echo 'Select text'.
The question is how do I correctly handle mysql stored procedure calls in php so that I will get the errors no matter the amount of selects, etc before the error happens?
This is my PHP code written for website. When I executed this, the query doesn't execute and doesn't show any error. I also checked data types of values that are to be inserted.
The database username and password and all credentials are correct. What could be the problem?
<?php
$password ='abcdef';
$host="localhost"; // Host name
$username="futureti_dsatya"; // Mysql username
$password="D2e3e4v1i"; // Mysql password
$db_name="futureti_db"; // Database name
$tbl_name="users"; // Table name
// Connect to server and select databse.
$con = mysqli_connect($host, $username, $password,$db_name);
if(!$con)
{
die('Could not connect: '. mysql_error());
}
else
{
$res = mysqli_query($con,"insert into users values(55555623,'saran1','satya_saran',$password)");
if($res){
print("i am ok");
}
else{
print("bad");
}
}
?>
Wrap $pass in quotes (55555623,'saran1','satya_saran','$pass') as shown below with an explanation about "$password", and change mysql_error()); to mysqli_error()); those two functions don't mix and that is why you did not get the proper error messages to show up.
As already stated, you're using $password twice; change one of the variables to something else.
What you're presently doing is overwriting your $password variable.
I am assuming you want to enter abcdef into your DB. If so, then do this instead:
<?php
$pass ='abcdef';
$host = "localhost"; // Host name
$username = "futureti_dsatya"; // Mysql username
$password = "D2e3e4v1i"; // Mysql password
$db_name = "futureti_db"; // Database name
$tbl_name = "users"; // Table name
// Connect to server and select databse.
$con = mysqli_connect($host, $username, $password,$db_name);
if ( !$con ) {
die('Could not connect: '. mysqli_error());
} else {
$res = mysqli_query($con, "insert into users values (55555623,'saran1','satya_saran','$pass' )");
if( $res ) {
print("i am ok");
} else {
print("bad");
}
}
?>
Also, inserting data into a table without telling it which columns to use is not a recommended method.
Use something to the effect of:
($con, "insert into users (column1, column2, column3, column4) values (55555623,'saran1','satya_saran','$pass' )
Sidenote: If the column for your first value isn't an (int) you will need to wrap that in quotes as well.
Also, if your first column is an AUTO_INCREMENT, you will need to remove the AUTO_INCREMENT from the column's type.
you dont get any error because you are making using mysql. not mysqli.
your code is wroking just wrap password . i guess the connection is not connecting.
replace this:
die('Could not connect: '. mysql_error());
to
die('Could not connect: '. mysqli_error()); //to see the error
I have this query:
$res=mysql_query("SELECT * FROM `t_modules` WHERE nPageM='61'") or die(mysql_error());
In phpmyadmin it returns (as expected) 2 rows but on page it returns 0.
If i use
$res=mysql_query("SELECT * FROM `t_modules` WHERE nPageM<>'61'") or die(mysql_error());
or
$res=mysql_query("SELECT * FROM `t_modules`") or die(mysql_error());
it runs on the page correctly it's just the WHERE and = combination that doesn't work
I also checked that the type for nPageM is int(11)
UPDATE
I can run comparisons on other columns in the table but not on nPageM
$res=mysql_query("SELECT * FROM `t_modules` WHERE id_md='5'") or die(mysql_error());
Is working. But i still don't have a clue about why it's not working on the nPageM column
have you ensured that you have included a connection to the databse in your php script before running this code?
<?php
$con = mysql_connect('sqluser', 'sqlpassword', 'sqlserver');
$db = mysql_select_db('dbame', $con);
//now, make sure it's connecting
if (!$con) {
die('mysql connection error' . mysql_error());
}
if (!$db) {
die('mysql Database error' . mysql_error());
}
?>
Instead try putting brackets around it:
$res=mysql_query("SELECT * FROM `t_modules` WHERE (nPageM<>'61') ") or die(mysql_error());