this is my code it's working in local host but not in server
$dbtype = 'mysql'; // Normally mysql
$host = 'localhost'; // This is normally set to localhost
$db_username = 'name'; // DB username
$db_password = 'pass'; // DB password
$db_name = 'db_name'; // DB database name
$dbprefix = 'db_'; // Do not change unless you need to!
$cat_table="db_cat";
try
{
$pdo = new PDO('mysql:host='. $host .';port=2082;dbname='.$db_name, $db_username, $db_password);
}
catch (PDOException $e)
{
die($e);
}
?>
ERROR MESSAGE
exception 'PDOException' with message 'SQLSTATE[HY000] [2000] mysqlnd cannot connect to
MySQL 4.1+ using the old insecure authentication. Please use an administration tool to reset your password with the command SET PASSWORD = PASSWORD('your_existing_password').
This will store a new, and more secure, hash value in mysql.user. If this user is used in
other scripts executed by PHP 5.2 or earlier you might need to remove the old-passwords flag
from your my.cnf file' in /home/jeywigki/public_html/hotel/db/config.php:13 Stack trace:
#0 /home/jeywigki/public_html/hotel/db/config.php(13): PDO-
>__construct('mysql:host=loca...', 'table', 'pass') #1 /home/jeywigki/public_html/hotel/index.php(3): require_once('/home/jeywigki/...') #2 {main}
kindly help me.
thanks in advance
I am not sure but it could be the issue.
you can try this:
mysql:host=$host;dbname=$db_name inside in quotes.
try
{
$pdo = new PDO("mysql:host=$host;dbname=$db_name", $db_username, $db_password);
}
catch (PDOException $e)
{
exit('Error Connecting To DataBase');
}
docs
Related
I am relatively new to php and have been following a tutorial for a beginner project. This project includes a database using the DBO object with mysql. I am using MAMP to run php. When I try to run my project it gives me the error in the title. Here is my code for database setup:
<?php
$dsn = 'mysql:host=localhost;dbname=assignment_tracker';
$username = 'root';
try {
$db = new PDO($dsn, $username);
} catch (PDOException $e) {
$error = "Database Error: ";
$error .= $e->getMessage();
include('view/error.php');
exit();
}
I am able to enter the phpmyadmin page. Looking at the user table in the mysql database shows this:
My port in the php.ini file is 8889. (My apache port is 8888 but my mysql is 8889 not sure which one it should be set to.) Looking at other answers on this website have been really confusing for me so any help would be greatly appreciated.
Try adding mysql's port to the $dns and add the password (if it's not null) as part of the connection. Try this
<?php
$dsn = 'mysql:host=127.0.0.1;port=8889;dbname=assignment_tracker';
$username = 'root';
$password = '';
try{
$db = new PDO($dsn, $username, $password);
}catch (PDOException $e){
$error = "Database Error: ";
$error .= $e->getMessage();
include('view/error.php');
exit();
}
I've followed the guides at https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15 and https://learn.microsoft.com/en-us/sql/connect/php/installation-tutorial-linux-mac?view=sql-server-ver15 and PHP is finding the drivers / server but now I'm getting an authentication error.
Verified the server is configured for "SQL Server and Windows Authentication mode" and allows remote connections to the server.
In order to get this error, I believe that means the code is at least connecting and attempting to authenticate with the server (changing the server to some nonsense server name/location produces a timeout error) - fairly certain that eliminates any problems that would come before the authentication step.
Still, I'm getting an error as follows...
PDOException: SQLSTATE[28000]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user 'username'. in /var/www/html/connect.php:10
Stack trace:
#0 /var/www/html/connect.php(10): PDO->__construct('sqlsrv:Server=S...', 'username', 'password')
#1 {main}root#containerIdGoesHere:/var/www/html
<?php
$user = 'username';
$pass = 'password';
try {
$pdo = new PDO("sqlsrv:Server=SomeServer,1433;Database=SomeDb", $user, $pass);
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(Exception $e) {
echo 'See error log';
$elog = fopen('errorlog.txt', 'w') or die("Couldn't open file");
fwrite($elog, $e);
fclose($elog);
}
Note: I've removed any sensitive info from my code... so the username and pw I'm using are not 'username' and 'password' & not related to the issue.
I've got this working on my production environment:
$db = "DatabaseName";
$password = "Password";
$server = "TheSQLServer";
$user = "username";
$link = new PDO(
"sqlsrv:Server=" . $server .
";Database=" . $db,
$user,
$password
);
return $link;
I also had to manually get into the DB and allow the previously created user to have the proper rights into it (DB->Security->Users), you can check that out aswell. Let me know how it goes.
I'm using a mysql database. I would like to connect to it with the script i wrote :
<?php
function getDatabase() {
$host = 'localhost:3306';
$db = 'freya';
$login = 'root';
$pw = 'helloitsme';
try {
return new PDO('mysql:host='.$host.';dbname='.$db.';charset=utf8', $login, $pw);
} catch (Exception $e) {
die('Erreur : '.$e->getMessage());
}
}
$db = getDatabase();
I have seen that this error is recurent but none of the solutions worked for.
I checked the my.cnf, and i'm sure that i'm using the port where the mysql db is.
I'm also sure that the db name, the login and the password are correct, because i'm using them to reach the db with the shell.
What could be the problem ?
You don't need to specify the port as 3306 is default for mysql, but if you do, the correct connection string is
'mysql:host=localhost;port=3306 ...'
I am trying to create a simple mailing list webpage. Using LAMP. Here is the code I have for connecting to my database:
<?php
function insert()
{
$servername = "127.0.0.1";
$username = "root";
$password = "(my password here)";
$dbname = "(my db name here)";
try
{
// preparing database handle $dbh
$dbh = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo ("Could not connect to server.\n");
echo ("getMessage(): " . $e->getMessage () . "\n");
}
}
?>
The function continues after that but this connection attempt gets cung up on the catch, and throws the "SQLSTATE[HY000] [2002] Connection refused" error. I have tried:
-Changing the $servername to localhost, but this gives me a different error: "SQLSTATE[HY000] [2002] No such file or directory"
-trying to specify all different ports
-checked all my info, database name and password.
-I can log into phpmyadmin and see that my database is fine
-looked at all other questions on this topic, with no help found.
How would I connect to the demo phpmyadmin server in php? My code looks like this.
<?php
$host = 'http://demo.phpmyadmin.net/STABLE/';
$dbname = 'shubham';
$user = 'root';
$pass = '';
// Attempt to connect to database.
try {
$DBH = new PDO("mysql:host={$host};dbname={$dbname}", $user, $pass);
} catch(PDOException $e) {
echo $e->getMessage();
}
?>
but I get this as my error
QLSTATE[HY000] [2005] Unknown MySQL server host 'http://www.demo.phpmyadmin.net/STABLE/' (1)
You seem to be confusing two things:
the demo phpMyAdmin front-end that is backed by a db server and db/schema
the db server and schema itself
PDO needs the latter, the db server itself.
Inspecting the front-end code of the demo, I don't see anything in there that would give us the actual connection details for the db server. And that's as I would expect: I find it hard to believe that the makers/maintainers of the phpMyAdmin demo would make their actual db server available for public remote connections.
change your hostname from
$host = 'http://demo.phpmyadmin.net/STABLE/';
to your original remote hostname like eg $host = 'ukld.db.5510597.hostedresource.com';
MySQL does not work on HTTP
<?php
$host = 'demo.phpmyadmin.net';
// High chances that this is NOT your mysql hostname.
// It will not even by like /STABLE/ as you mentioned it.
$dbname = 'shubham';
$user = 'root';
$pass = '';
// Attempt to connect to database.
try {
$DBH = new PDO("mysql:host={$host};dbname={$dbname}", $user, $pass);
} catch(PDOException $e) {
echo $e->getMessage();
}
?>