<?php
$host = 'localhost';
$db='mysql';
$user = 'root';
$pass = 123;
$conn = new PDO("mysql:host=$host;dbname=$db",$user,$pass);
if($conn){
echo 'ok';
}else {
echo 'error';
}
?>
why this code can't connect the database? my php.ini file setting is right,
extension=php_pdo.dll
extension=php_pdo_mysql.dll
the database user is root, the password is 123. thank you
If you are at the beginning of a project and are unable to make PDO work, you might be able to have more luck with mySQLi.
http://php.net/manual/en/book.mysqli.php
Related
I have a table on PHPmyamdmin, and when i try to select it with the following code:
*<?php
//connect.php
session_start();
$server = 'localhost';
$username = 'zimmer';
$password = '';
$database = 'csgopit';
if(!mysql_connect($server, $username))
{
exit('Error: could not establish database connection');
}
if(!mysql_select_db($database))
{
exit('Error: could not select the database');
}
?>*
i get this error when i open the page in localhost:
Error: could not select the database
In the database i have 4 tables, one of them is users and i have manually added the user "zimmer" to it, i'm using this to create and experiment forum in my learning experiences, also, using this same code as a "connect.php" if i use it as for example on another file simply by using this line
using connect.php or include connect.php
would it work in a login or would it load only the user zimmer?
I started with PHP couple of days ago so sorry for the rookie question.
Try PDO.
$handler = '';
$server = 'localhost';
$username = 'zimmer';
$password = '';
$database = 'csgopit';
try {
$this->handler = new PDO('mysql:host='. $server .';dbname='. $database . ';charset=utf8', $username, $password);
} catch(PDOException $e) {
echo $e->getMessage();
die();
}
But you also mentioned you added the user "zimmer" to the users table. This is different than your $username you pass to connect to the database.
Did you set up a username and password to a database?
I am fairly new to using PHP. I downloaded XAMPP, and installed everything. PHP 5.5.27 is the version. I ran a test php program which was jsut echo "Hello World". It worked fine. I also was able to connect to MYSQL database using PHP.
$link = mysqli_connect("localhost", "u/n", "pass", "databasename";
Problem i am having and need help is with connecting to sql server. How do i do that? I saw an example online and tried it:
$serverName = "servername";
$connectionInfo = array("Database"="name", "UID"=>"U/N", "PWD"=>"pass";>
$conn = sqlsrv_connect($serverName, $connectionInfo);
But everytime i run this it tells me:
Call to undefined function sqlsrv_connect()
Can someone help me understand what is going on?
Consider using PHP's Data Objects (PDO) to connect to SQL Server (in fact you can use it to connect to MySQL or any other database).
Using the MSSQL sqlsrv API (various dlls must be set):
<?php
$server = 'servername';
$database = 'databasename';
$username = 'username';
$password = 'pass';
try {
$conn = new PDO("sqlsrv:Server=$server;Database=$database",
$user, $password);
}
catch(PDOException $e) {
echo $e->getMessage()."\n";
exit;
}
?>
Using the ODBC Driver or DSN API (requiring MSSQL ODBC Driver installed which usually ships with database or Windows in general):
<?php
$server = 'servername';
$database = 'databasename';
$username = 'username';
$password = 'pass';
try {
$dbh = new PDO("odbc:Driver={SQL Server};Server=$server;
database=$database",$username,$password);
}
catch(PDOException $e) {
echo $e->getMessage()."\n";
exit;
}
?>
i'm having issues connecting to my database on a web host that I have, i'm using the following:
$dsn = 'mysql:host=mysql1.hosting.digiweb.ie;dbname=mydbname';
$user = 'myusername';
$password = 'mypassword';
According to the website: Host Name mysql1.hosting.digiweb.ie (ip address)
as the title says i'm getting a could not find driver error, am i entering the host incorrectly i tried entering whats above and also the ip address - Thanks!
Edit:
Here's all my code
<?php
$dsn = 'mysql:host=localhost;dbname=';
$user = '';
$password = '';
try {
// Connect and create the PDO object
$dbh = new PDO($dsn, $user, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
echo 'Database connection failed - ';
echo $e->getMessage();
exit;
}
echo 'works';
?>
go to your php.ini file and uncomment this line
extension=php_pdo_mysql.dll
and then restart your apache
Change your extension dir to be absolute in php.ini. I changed it from
extension_dir = "ext"
to
extension_dir = "C:/{PATH TO PHP DIRECTORY}/ext"
and it worked.
i'm having issues connecting to my database on a web host that I have, i'm using the following:
$dsn = 'mysql:host=mysql1.hosting.digiweb.ie;dbname=mydbname';
$user = 'myusername';
$password = 'mypassword';
According to the website: Host Name mysql1.hosting.digiweb.ie (ip address)
as the title says i'm getting a could not find driver error, am i entering the host incorrectly i tried entering whats above and also the ip address - Thanks!
Edit:
Here's all my code
<?php
$dsn = 'mysql:host=localhost;dbname=';
$user = '';
$password = '';
try {
// Connect and create the PDO object
$dbh = new PDO($dsn, $user, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
echo 'Database connection failed - ';
echo $e->getMessage();
exit;
}
echo 'works';
?>
go to your php.ini file and uncomment this line
extension=php_pdo_mysql.dll
and then restart your apache
Change your extension dir to be absolute in php.ini. I changed it from
extension_dir = "ext"
to
extension_dir = "C:/{PATH TO PHP DIRECTORY}/ext"
and it worked.
I keep getting this whenever I run the below code:
Fatal error: Call to undefined function mysql_connect() in C:\wamp\www\php-academy\113-connect-db.php
<?php
$conn_error ='could not connect';
$mysql_host = 'localhost';
$mysql_user = 'root';
$mysql_pass = '';
mysql_connect($mysql_host, $mysql_user, $mysql_pass) or die($conn_error);
$mysql_db = 'a_database';
mysql_select_db($mysql_db) or die($conn_error);
echo 'Connected!';
?>
That probably means you don't have the MySQL extension loaded. You can test whether the extension is loaded with extension_loaded("mysql");.
I suspect that this is because the extension is not loaded. I would check your php_info(); to find out.
Once you do your code should work, although I prefer to assign the mysql_connect() to a variable and then call that variable in mysql_select_db() as I have below:
$defHost = 'localhost';
$defUsername = 'username';
$defPassword = 'password';
$defDatabase = 'database_name';
$connect = mysql_connect($defHost, $defUsername, $defPassword) or die();
mysql_select_db($defDatabase, $connect) or die();
The reasons for the error may be the following:
Incorrect access details to server database are specified in configuration file.
There are no read permissions for configuration file. Provide read permissions for the file.