I have the following php code:
<?php
$hostname="db.";
$database="db..";
$username="db...";
$password="pw...";
$mysql = mysql_connect($hostname, $username, $password);
if(!$mysql)
echo 'Not conneted to the database...' . mysql_error() ;
else
echo 'Connected to the database...' ;
// select the appropriate database
$mysqldb = mysql_select_db( $database );
if(!$mysqldb)
die('Could not select the database...' . mysql_error());
else
echo '<br /><br />Connected to database...<br /><br />' ;
$chk_table_access = 'select * from table_name where 1';
$chk_access = mysql_query($chk_table_access);
echo $chk_table_access . '<br /><br />';
if (!$chk_access)
echo 'Could not access table: ' . mysql_error() ;
else
echo 'Table was accessed..<br /><br />' ;
$backup = 'SELECT * INTO OUTFILE "result.csv" FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY ';
$backup = $backup . "'" . '"' . "'" ;
$backup = $backup . ' FROM cities WHERE 1';
echo $backup . '<br /><br />';
$bk_success = mysql_query($backup);
if (!$bk_success)
echo 'The backup was not successful. Reason: ' . mysql_error();
?>
The program runs fine on localhost, but when I run it from my web site I get the following error: Access denied for user 'dbo????????'#'%' (using password: YES). The web help is not much help.
I do get the "Table was accessed.." message so I'm accessing the table okay. I do frwite to the directory every day without errors. I understand why it doesn't work in phpMyAdmin. I'm a little stumped. It's also the first question I've asked on this forum although I come here often for help. You guys (ladies included) rock.
Thanks in advance,
Steve
Your MySQL user apparently lacks the FILE privilege.
Does the MySQL server run on the same machine as the web server? If both servers run on different machines, you can stop right here: Even with FILE privilege, the output file would be created on the database server (and would probably inaccessible from the web server).
Related
I'm trying to learn how to make mysql queries, using MAMP, MariaDB, and 2 PHP scripts.
I used MYSQL Workbench to make a schema (helloWorld), added a table (Paintings), and put 2 entries in the table. I created a 'shuwoo'#'localhost' user, and granted all permissions to it, using the terminal. I successfully logged into MYSQL Workbench, but when I looked at shuwoo's "Administrative Roles", "DBA", "SecurityAdmin", and "DBManager" are unchecked. Trying to enable them in the Workbench gives me an error.
The real issue is that my php script gives me an error, "Could not connect: Access denied for user 'shuwoo'#'localhost' (using password: YES)" when I run the script. The code is very simple, here it is:
Note: I've noticed it doesn't matter what I enter for mysql_select_db. I think that's because I'm not even getting access to this level?
Your help is greatly appreciated!
<?php
$dbhost = 'localhost:3306';
$dbuser = 'shuwoo';
$dbpass = 'Poptart5';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT * FROM Paintings';
mysql_select_db('helloWorld');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "ID :{$row['Id']} <br> ".
"Title: {$row['Title']} <br> ".
"Description: {$row['Description']} <br> ".
"Medium: {$row['Medium']} <br> ".
"--------------------------------<br>";
}
echo "Fetched data successfully";
mysql_close($conn);
?>
New to php and mysql,
I am trying to insert a exported sql file into a remote DB.
I am try this code from 1and1 but it is not working.
$create_install_db_server = 'testdb.example.com';
$create_install_db_username = 'test123';
$create_install_db_password = 'test789';
$create_install_db_name = 'test';
$sqlfile = '/home/path/to/localsql.sql';
$command='mysql -h' . $create_install_db_server .' -u' . $create_install_db_username .' -p' . $create_install_db_password .' ' . $create_install_db_name .' < ' . $sqlfile;
exec($command,$output=array(),$worked);
switch($worked){
case 0:
echo 'Import file <b>' .$mysqlImportFilename .'</b> successfully imported to database <b>' .$mysqlDatabaseName .'</b>';
break;
case 1:
echo 'There was an error during import.';
break;
}
#srikanth, I have an another way to do this, try this one
<?php
// Name of the file
$filename = 'churc.sql';
// MySQL host
$mysql_host = 'testdb.example.com';
// MySQL username
$mysql_username = 'test123';
// MySQL password
$mysql_password = 'test789';
// Database name
$mysql_database = 'Test';
// Connect to MySQL server
mysql_connect($mysql_host, $mysql_username, $mysql_password) or die('Error connecting to MySQL server: ' . mysql_error());
// Select database
mysql_select_db($mysql_database) or die('Error selecting MySQL database: ' . mysql_error());
// Temporary variable, used to store current query
$templine = '';
// Read in entire file
$lines = file($filename);
// Loop through each line
foreach ($lines as $line)
{
// Skip it if it's a comment
if (substr($line, 0, 2) == '--' || $line == '')
continue;
// Add this line to the current segment
$templine .= $line;
// If it has a semicolon at the end, it's the end of the query
if (substr(trim($line), -1, 1) == ';')
{
// Perform the query
mysql_query($templine) or print('Error performing query \'<strong>' . $templine . '\': ' . mysql_error() . '<br /><br />');
// Reset temp variable to empty
$templine = '';
}
}
echo "Tables imported successfully";
?>
Hope this helps you. :)
Note: mysql usage is deprecated, instead you can make use of Mysqli,PDO like below.
$db = new PDO($mysql_host, $mysql_username, $mysql_password);
$sql = file_get_contents('churc.sql');
$qr = $db->exec($sql);
for executing big sql files my above answer is not enough so i suggest you to take a look at #Abu sadat answer here
Try the following things
Do an echo of the command before you try to execute it
Try running the command on the terminal - check if this is successful
If it is successful, replace your exec with a simple command (like 'touch /tmp/abc.txt') and check if a file is getting created.
By doing this, you are trying to find out if there is a problem with your mysql command or there is an issue with the exec function in php
I am using function based database connection and select query also plz help me.
FIRST CODE:
function connectDb(){
//my postgresql db connection
$link=pg_pconnect('host=' . __DB_HOST__ . ' port=' . __DB_PORT__ . ' dbname=' . __DB_NAME__ . ' user=' . __DB_USER__ . ' password=' . __DB_PWD__) or die('connection failed');
AllDelete();// Audit Logs Auto Deletion
}
SECOND CODE:
function AllDelete(){
$MysqlForLogs=pg_query("select audit.*,users.username from audit,users where audit.user_id=users.id and DATE_PART('day',now()-audit.date) > 7") or die(pg_last_error());
$CountRows=pg_num_rows($MysqlForLogs);
}
I am executing the browser it will show on error like 'No database selected' but really i am giving the database name.
What is the problem?
I am reading the head first book on PHP and MySQL. Just started chapter 2 where they connect to database. But my script doesn't do anything. I get no error from the "or die" even when I purposefully enter the incorrect connection string, and the query doesn't insert. I am running Ubuntu server 14 with PHP 5.6.4 and MySQL 5.6.24 on a VM on my Mac.
I can connect remotely with MySQL workbench and execute the query which is successful.
Here is the my code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Aliens Abducted Me - Report an abduction</title>
</head>
<body>
<h2>Aliens Abducted Me - Report an abduction</h2>
<?php
$name = $_POST['firstname'] . ' ' . $_POST['lastname'];
$when_it_happened = $_POST['whenithappened'];
$how_long = $_POST['howlong'];
$how_many = $_POST['howmany'];
$alien_description = $_POST['aliendescription'];
$what_they_did = $_POST['whattheydid'];
$fang_spotted = $_POST['fangspotted'];
$email = $_POST['email'];
$other = $_POST['other'];
echo 'Thanks for submitting the form ' . $name . '<br />';
echo 'You were abducted ' . $when_it_happened;
echo ' and were gone for ' . $how_long . '<br />';
echo 'You saw ' . $how_many . ' of them' . '<br />';
echo 'Describe them: ' . $alien_description . '<br />';
echo 'What the did: ' . $what_they_did . '<br />';
echo 'Was Fang there? ' . $fang_spotted . '<br />';
echo 'Your email address is ' . $email . '<br />';
echo 'Additional information: ' . $other;
$dbc = mysqli_connect('127.0.0.1', 'root', 'password', 'aliendatabase')
or die('Error connecting to server');
$query = "INSERT INTO aliens_abduction (first_name, last_name, " .
"when_it_happened, how_long, how_many, alien_description, " .
"what_they_did, fang_spotted, other, email)" .
"VALUES ('Sally', 'Jones', '3 days ago', '1 day', 'four', " .
"'green with 6 tenticles', 'We talked and played with a dog', " .
"'yes', 'I may have seen your dog', 'sally#gregs-list.net')";
$result = mysqli_query($dbc, $query)
or die ('Error querying DB');
mysqli_close($dbc);
?>
</body>
</html>
I have tried changing the host to localhost, ip address, loopback address all with and without the port number. But what the most frustrating thing is that I get no errors. Even when I should be.
I have even read up on different ways to create the connect/error commands using "if" i.e.
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
no difference. What is going wrong?
try this one
$dbc = mysqli_connect("localhost","root","password",'aliendatabase');
if(!$dbc)
{
echo "cannot connet to the server";
}
$q = "Your insert query here";
$query=mysqli_query($dbc, $q)
or die("Error: ".mysqli_error($dbc));
mysqli_close($dbc);
Thanks for getting back to me, i appreciate it. I discovered what the issue is. The errors not displaying was thanks to the errors not being setup in my php.ini (display_errors=off, needs to be changed to on). then i got this error "Fatal error: Call to undefined function mysqli_connect()" and this was because mysqli extension was not installed. Can be checked with php -m | grep on linux, if returns nothing then mysqli is not installed. install php5-mysqlnd and restart apache. Then fixed thanks for all the help.
I am trying to connect to multilple ftp servers using ftp_connect and ftp_login. My code is very simple as given
foreach ($ftp_accounts as $ftp_account) {
$connect_id = $ftp_account['server'];
$user = $ftp_account['user'];
$pass = $ftp_account['password'];
echo 'Ftp Server: '.$connect_id.'<br> Username: '.$user. '<br> Password: '.$pass.'<br>';
if (ftp_login($connect_id, $user, $pass)){
echo "| Connected to server " . $connect_id . " as " . $user . '<br>';
} else {
echo "Couldn't connect to $connect_id as $user \n";
}
}
I have a table in my database with multiple servers and their credentials. but the code i wrote give me the following error (warning) message
ftp_login() expects parameter 1 to be resource, string given
and it goes to the else statement for every server.
Any idea what I am doing wrong ?
Thanks in advance
You need to use ftp_connect() to connect to the server before trying to login. Additionally, you need to check the result of ftp_connect() to ensure it was able to connect before you try to login. If it wasn't then it returns FALSE and you should not proceed to try and login.
foreach ($ftp_accounts as $ftp_account) {
$connect_id = ftp_connect($ftp_account['server']);
if($connect_id !== false)
{
$user = $ftp_account['user'];
$pass = $ftp_account['password'];
echo 'Ftp Server: '.$connect_id.'<br> Username: '.$user. '<br> Password: '.$pass.'<br>';
if (ftp_login($connect_id, $user, $pass)){
echo "| Connected to server " . $connect_id . " as " . $user . '<br>';
} else {
echo "Couldn't connect to $connect_id as $user \n";
}
} else {
echo 'failed to connect to server';
}
}
Replace
<?php
$connect_id = $ftp_account['server'];
?>
with
<?php
$connect_id = ftp_connect($ftp_account['server']);
?>
Hope this helps.
Let me answer my question. First of all the replies I get was all corect options to be checked for such problems I highly appriciate the replies but one thing you also need to check is that wether your server firelwall allows to connect to any outgoing ftp server or not ?
If it does not allow you to connect then even with everything written correct in your code you will not be able to connect to the server. So before testing your code check your server firewall settings.
It worked for me when i allowed all outgoing connections.
Once again Thanks for the replies
You forgot to make connection to server using ftp_connect function, assuming that in variable $ftp_account['server'] you have valid FTP server host address.
foreach ($ftp_accounts as $ftp_account) {
$connect_id = ftp_connect($ftp_account['server']);
if ($connect_id === false) {
echo "Failed to connect to FTP server";
continue;
}
$user = $ftp_account['user'];
$pass = $ftp_account['password'];
echo 'Ftp Server: ' . $ftp_account['server'] . '<br> Username: ' . $user . '<br> Password: ' . $pass . '<br>';
if (ftp_login($connect_id, $user, $pass)){
echo "| Connected to server " . $ftp_account['server'] . " as " . $user . '<br>';
} else {
echo "Couldn't connect to " . $ftp_account['server'] . " as " . $user . "\n";
}
}