Mysql error "Access denied for user" - php

I am trying to backup mysql database using php.
I tried the code below:
<?php
$dbhost = 'my_host';
$dbuser = 'my_user';
$dbpass = 'my_pass';
$db = 'my_dbname';
$conn = mysql_connect($dbhost, $dbuser, $dbpass, $db);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
$table_name = "my_table";
$backup_file = "/tmp/my_table.sql";
$sql = "SELECT * INTO OUTFILE '$backup_file' FROM $table_name";
mysql_select_db($db);
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not take data backup: ' . mysql_error());
}
echo "Backedup data successfully\n";
mysql_close($conn);
?>
but I got this error:
"Could not take data backup: Access denied for user 'my_user'#'%' (using password: YES)"
Could you help me, please? Thanks!

Try
<?php
$dbhost = 'my_host';
$dbuser = 'my_user';
$dbpass = 'my_pass';
$db = 'my_dbname';
$conn = mysql_connect($dbhost, $dbuser, $dbpass, $db);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
$table_name = "my_table";
$backup_file = "/tmp/my_table.sql";
$sql = "SELECT * INTO OUTFILE '$backup_file' FROM $table_name";
// set current db with connection resource
mysql_select_db(db, $conn);
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not take data backup: ' . mysql_error());
}
echo "Backedup data successfully\n";
mysql_close($conn);

Related

select data from mysql and display it in order of expiry date

I want to be able to display document name and expiry date in order of expiry date in my browser. Mysql table name is employee_doc and the database name is employee_info. Here is my code
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'employee_info';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
if(! $conn ) {
die('Could not connect: ' . $mysqli->connect_error());
}
$mysqli = 'SELECT * FROM `employee_doc` ORDER BY `employee_doc`.`PPExp` ASC';
mysqli_connect($conn,"employee_info");
$retval = mysqli_query($conn,$mysqli);
if(! $retval ) {
die('Could not get data: ' . $mysqli->connect_error());
}
while($row = mysqli_fetch_array($retval, MYSQLI_ASSOC)) {
echo $row['PPNO'];
echo "<br>";
echo $row['PPExp'];
echo "<br>";
}
mysqli_close($conn);
?>
PPExp and PPNO are the column headings for expiry date and document name respectively. I am using Xampp. When I open the file from localhost die('Could not get data: ' . mysql_error()); is executed. What am I doing wrong?
You need to include the database name in your mysql connection.
Your code:
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
Revised Code:
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, "employee_info");
mysqli_connect: Open a new connection to the MySQL server.
mysqli_select_db: Selects the default database to be used when
performing queries against the database connection. This function
should only be used to change the default database for the connection.
This explains the basic concepts of both the functions.
Check mysqli_connect and mysqli_select_db for detailed explanation.
Change your code as follows:
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'employee_info';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
if(! $conn )
{
die('Could not connect: ' . mysqli_connect_error());
}
$sql = 'SELECT * FROM `employee_doc` ORDER BY `employee_doc`.`PPExp` ASC';
$retval = mysqli_query($conn,$sql);
if(! $retval )
{
die('Could not get data: ' . mysqli_error($conn));
}
while($row = mysqli_fetch_array($retval, MYSQLI_ASSOC))
{
echo $row['PPNO'];
echo "<br>";
echo $row['PPExp'];
echo "<br>";
}
mysqli_close($conn);
?>
Also you are mixing mysql and mysqli
mysqli_select_db is use for changing current database to new database and using that for selecting database is not currect
You should select db in the process of mysqli_connect as others explained.
see: http://php.net/manual/en/mysqli.select-db.php
Provide database name:
$conn = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname);
and changes in
$retval = mysqli_query($conn,$sql);
and
while($row = mysqli_fetch_array($retval))

How can I use the data I get from my database using PHP, MySQL, Bootstrap?

My question simple Ex. Country, --> State --> City.
How to make like above this, I need to implement in my website. Like dropdown menu.
After the select the City its redirect to the City peoples list.
How its will make ???
// Create Connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
trigger_error("Connection failed: " . mysqli_connect_error());
}
//Run Query
$stmt = "SELECT * FROM country";
$result = mysqli_query($conn,$stmt) or die(mysqli_error($conn));
while(list($category) = mysqli_fetch_row($result)){
echo '<option value="'.$category.'">'.$category.'</option>';
}
mysqli_close($conn);
?>
</select>
Any other alternative code ?
mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified
<?php
$dbname = "myDB";
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ){
die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT * FROM country';
mysql_select_db($dbname);
$retval = mysql_query( $sql, $conn );
if(! $retval ){
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC)){
echo '<option value="'.$row["id"].'">'.$row["name"].'</option>';
}
mysql_close($conn);
?>
http://php.net/manual/en/function.mysql-query.php
example http://www.tutorialspoint.com/mysql/mysql-select-query.htm

Query failed in php

$query = "SELECT * FROM Student WHERE student_id = $child AND school_id = $college";
$result = mysqli_query($conic, $query);
if(!$result){
die(" query failed");
echo 'error here';
} else {
i am trying to retrieve some information from the SQL database but the query seems to fail any ideas?
You must learn more : how to retrieve data from database using select in php
so try out following example to display all the records from Student table
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = "SELECT * FROM student WHERE student_id = '"$child"' AND school_id = '"$college"' ";
mysql_select_db('test_db');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
// some code here
}
echo "Fetched data successfully\n";
mysql_close($conn);
?>
NOTE
The mysql extension is deprecated and will be removed in the future:
use mysqli or PDO

php mysql data rerival from table

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = "";
$name=$_POST['user'];
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT * FROM `usertable` WHERE `username`='$name'' ;
mysql_select_db('myxiv');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "Name :{$row['username']} .<br> ";
}
mysql_close($conn);
?>
Here is my code and i'm getting this error.
Syntax error, unexpected '$name' (T_VARIABLE)
I m trying to retrive data from a table to display in the profile page
$sql = 'SELECT * FROM usertable WHERE username='$name'' ;
Replace Your above query by bellow code and check it.
$sql = "SELECT * FROM usertable WHERE username=' ".$name." ' ";
Try this
Change
$sql = 'SELECT * FROM usertable WHERE username`='$name'' ;
TO
$sql = "SELECT * FROM usertable WHERE username='".$name."';
You make a few mistakes. mysql (which is depracted (take a look at PDO or mysqli)) doesn't need a connection.
You could have a double single quote error but the following should fix some of it:
if($_POST) {
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$name = mysql_real_escape_string(strip_tags($_POST['user']));
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('myxiv');
$retval = mysql_query("SELECT * FROM `usertable` WHERE `username` = '$name'");
if(! $retval ) {
die('Could not get data: ' . mysql_error());
} else {
while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
echo "Name :{$row['username']} .<br> ";
}
}
}

How to update many fields with single query?

I am working with a MLM company where com has to register the member, if it detects any mistake in data, it has to update the data of member again.
I have the right code which works well when all fields has to update but problem is it does not work for individual input boxes. Please give me solution of this problem.
<?php
if(isset($_POST['update'])) {
$dbhost = 'localhost';
$dbuser = 'vvvv';
$dbpass = 'xxxx';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
$usrid = $_POST['uid'];
$pwrd = $_POST['pwd'];
$nm = $_POST['noe'];
$fnm = $_POST['fn'];
$addrs = $_POST['adrs'];
$cntn = $_POST['cnt_no'];
$cty = $_POST['ct'];
$sql = "UPDATE office_user ".
"SET
password = '$pwrd',
name='$nm',
father_name='$fnm',
address='$addrs',
contact_no='$cntn',
city='$cty' ".
"WHERE user_id = '$usrid'" ;
mysql_select_db('my_db');
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
mysql_close($conn);
}
?>

Categories