1
I cannot access the MS SQL SERVER database using PDO. I’ve done everything, the best answer I could was with the code below, but I keep getting an error reply:
SQLSTATE[] Connection Error (null) (Severity 0)
My server is Centos 5.11 (64 bits)
Any idea?
<?php
try {
$hostname = "db-gadel-campinas.lexos.com.br,1500";
$dbname = "LexManager_GadelCampinas";
$username = "gadelview";
$pw = "password";
$pdo = new PDO ('dblib:host=$hostname;dbname=$dbname', '$username', '$pw');
} catch (PDOException $e) {
echo "Erro de Conexão " . $e->getMessage() . "\n";
exit;
}
?>
I changed the code and it seems to work the connection with sqlserver . Now is to find out how to query the table and show the data :). Any hint?
The code is correct?
<?php
try {
$hostname = "db-gadel-campinas.lexos.com.br";
$port = 1500;
$dbname = "LexManager_GadelCampinas";
$username = "gadelview";
$pw = "password";
$dbh = new PDO ("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw");
echo "Conexão ok";
} catch (PDOException $e) {
echo "Failed to get DB handle: " . $e->getMessage() . "\n";
exit;
}
?>
Which error is giving? Do not use the
mysql_*
he is depreciated, I think his best option is to hire a freelance.– Leonardo
PS: 1500 should probably be the Mysql port that was changed and not part of the pro server address.
– Leonardo
@Thanks for the help in my question. We want to hire a freelance to create an app, but we need to have a template to present to the sponsor, so I’m racking my brain to create this MVB. If you can give me a few tips, I’ll research and test around here.
– Állan Fidelis Toledo
About a year ago I did a project to help beginners with database operations, using
PDO
(latest driver for database). You can access this link: https://github.com/leonardovilarinho/MinPDO in it there is a brief tutorial and how to use, I hope it helps. To make the connection is very simple, as well as consult and change records, in the index.php file of the repository has a description of how everything works.– Leonardo
I’ll take a look, thank you! But back to 1500, how should I use this information, I mean, how I declare this door in the code?
– Állan Fidelis Toledo
If it really is the door just change to
db-gadel-campinas.lexos.com.br:1500
If you continue with error try only withdb-gadel-campinas.lexos.com.br
. And in case there’s still an error, post what error is giving.– Leonardo
db-gadel-campinas.lexos.com.br:1500 Error: Connection failed: SQLSTATE[HY000] [2005] Unknown Mysql server host 'db-gadel-campinas.lexos.com.br:1500' (1) db-gadel-campinas.lexos.com.br Error: I’ve been waiting for a few seconds without server response.
– Állan Fidelis Toledo
Without the port, the error is another: Error 503 Backend fetch failed Backend fetch failed Guru Meditation: XID: 33940816 Varnish cache server
– Állan Fidelis Toledo
Strange, that error without the port, it seems that found the database host, but gave some internal error there.
– Leonardo
I already asked a question to the staff that generated the access, let’s see. Thank you.
– Állan Fidelis Toledo
I received a feedback from them, the problem is that they use SQL Server :| "The interface you are using points to a Mysql connection string... Our database is SQL Server, you will probably be able to use with the mssql_connect driver, or sqlsrv_connect..."
– Állan Fidelis Toledo
You can use
$dbh = new PDO("sqlsrv:Server={$host};Database={$db}", $user , $pass);
Now yes, in the$host
must have the ',1500'– Leonardo
Or like: http://answall.com/questions/73279/howto connect-php-com-o-sqlserver-do-pdo
– Leonardo
@lvcs At least it gave connection error :) the message that appears now is "Connection Error could not find driver" Dei a searched and it seems that my server (Locaweb) is not compatible. Any idea?
– Állan Fidelis Toledo
I switched to dblib '$Pdo = new PDO ("dblib:host=$hostname;dbname=$dbname", '$username', '$Pw');' now the error is SQLSTATE[] Connection Error (null) (Severity 0)
– Állan Fidelis Toledo
The first error is what database driver is not installed on the machine, the second you can try to fix by putting
db-gadel-campinas.lexos.com.br:1500
(two points).. If this attempt fails, then I do not know how to proceed anymore.– Leonardo
@lvcs I’m using your project on Github, I made the settings according to the guidelines, I did a BD connection test, it all worked out fine, but now, when I try to run the query(), for example, I get an error Error:SQLSTATE[HY000] [2002] Can’t connect to local Mysql server through socket '/var/lib/mysql/mysql.Sock' (2) Do you have any idea what might be going on? [http://pethood.com.br/pontestore/MinPDO-master/ex/consultar.php]
– Állan Fidelis Toledo