I have some code that connects to a localhost MySQL server and displays if that database contains any matches of the search box. But I am now trying to connect to a SQL Server database and I just can't get it to work. Am I doing something wrong?
try {
$pdo = new PDO("sqlsrv:Server=xxx.xx.xx.xx,1433;Database=Live", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
// $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
echo "Connected successfully";
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
$stmt = $pdo->prepare("
SELECT *
FROM `dbo.Home`
WHERE `item_number` LIKE ? OR `stock_available` LIKE ?
");
$stmt->execute(["%" . $_POST['search'] . "%", "%" . $_POST['search'] . "%"]); <---LINE 54
$results = $stmt->fetchAll();
if (isset($_POST['ajax'])) { echo json_encode($results); }
I get the following error:
Connected successfully Fatal error: Uncaught PDOException:
SQLSTATE[42000]: [Microsoft][ODBC Driver 17 for SQL Server][SQL
Server]Incorrect syntax near 'dbo.Home'. in
/usr/local/var/www/2-search.php:54 Stack trace: #0
/usr/local/var/www/2-search.php(54): PDOStatement->execute(Array) #1
/usr/local/var/www/1-form.php(14): require('/usr/local/var/...') #2
{main} thrown in /usr/local/var/www/2-search.php on line 54
Any help would be greatly appreciated.
Related
I want to run this on Ubuntu22.04(AzureVM).
<?php
$dsn = 'sqlsrv:server=hogehoge';
$user = 'user';
$password = 'password';
$dbh = new PDO($dsn, $user, $password);
$sql = "select * from user";
foreach ($dbh->query($sql) as $row) {
echo $row['id'] . ' ' . $row['name'] . "<br>";
}
$dbh = null;
?>
But I got this error message.
PHP Fatal error: Uncaught PDOException: SQLSTATE[08001]: [Microsoft][ODBC Driver 18 for SQL Server]SSL Provider: [error:0A000086:SSL routines::certificate verify failed:unable to get local issuer certificate] in test.php:6
Stack trace:
#0 /var/www/test/test.php(6): PDO->__construct()
#1 {main}
thrown in test.php on line 6
What's happened? I searched for that error message but can't understand how to fix.
My local Windows does have not this problem. Maybe Ubuntu server is bad.
Thank you for your viewing.
I am working on a new project and I am getting an error when I connect with my DB I don't know why it is happening my DB name and user name is all correct but why I am getting this error
Fatal error: Uncaught Error: Call to undefined function mysql_query() in C:\xampp\htdocs\treeview\treeview.php:9 Stack trace: #0 {main} thrown in C:\xampp\htdocs\treeview\treeview.php on line 9
Code:
<?php
mysql_connect('localhost', 'root');
mysql_select_db('test');
$qry = "SELECT * FROM treeview_items";
$result = mysql_query($qry);
$arrayCategories = array();
while ($row = mysql_fetch_assoc($result)) {
$arrayCategories[$row['id']] = array("parent_id" => $row['parent_id'], "name" => $row['name']);
}
I'll assume you are on PHP 7+, which means that mysql_query is deprecated. Please use PDO or mysqli.
More info on that function's man page: https://www.php.net/manual/en/function.mysql-query.php
You can obtain the connection to your database through:
$connection = new mysqli($hostname, $db_username, $password, $db_name);
if ($connection->connect_error){
$error = "Failed to connect to db: " . $connection->connect_error;
// Do whatever you want with $error
}
$connection->query("SET NAMES utf8mb4"); // mostly, people use utf8 instead
I have this little snippet that connects to sql server using PDO, and it fails because my server is down (which is correct, it's the scenario I want to test). However, the exception is not caught!
$dsn = "sqlsrv:Server=$host,$port;Database=$database";
$conn = new PDO($dsn, $user, $pass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$works = false;
try {
$works = ($conn->exec('SELECT * FROM INFORMATION_SCHEMA.TABLES') !== false);
}
catch (\PDOException $e) {
}
echo "works? " . ($works ? 'yes' : 'no') . "\n";
The output I get in console:
➜ backend git:(dev) ✗ php mssql-pdo-test.php
PHP Fatal error: Uncaught PDOException: SQLSTATE[HYT00]: [Microsoft][ODBC Driver 17 for SQL Server]Login
timeout expired in /path/mssql-pdo-test.php:10
Stack trace:
#0 /path/mssql-pdo-test.php(10): PDO->__construct()
#1 {main}
thrown in /path/mssql-pdo-test.php on line 10
I guess the exception is thrown outside the try / catch block here:
$conn = new PDO($dsn, $user, $pass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
I am getting following fatal error while accessing data from SQL Server 2014 through PHP Script.
Fatal error: Uncaught PDOException: SQLSTATE[42000]: [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]The SELECT permission was denied on the object 'test_table', database 'test_db', schema 'dbo'.
My PHP Script is
$server = 'KESAVATDL\SQLEXPRESS'; //KESAVATDL\SQLEXPRESS
$dbname = "acharya_db";
$usname="";
$passwd="";
try
{
$con=new PDO("sqlsrv:Server=$server;Database=$dbname",$usname,$passwd);
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected Successfully.";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
$sql = "SELECT * FROM test";
foreach ($con->query($sql) as $row) {
print_r($row);
}
I'm using a free hosting solution for my MySQL database. However, when I try to connect to it through my PHP code it throws me a uncaught PDOException error and my access is denied. I can't seem to find why the connection is denied as my credentials are correct.
I'm using Ionic 2 framework, if that helps.
Error:
<b>Fatal error</b>: Uncaught PDOException: SQLSTATE[HY000] [1045] Access denied for user 'sql10181809'#'153.92.0.10' (using password: YES) in /storage/ssd5/403/2053403/public_html/escanerproducto.php:33
Stack trace:
#0 /storage/ssd5/403/2053403/public_html/escanerproducto.php(33): PDO->__construct('mysql:host=sql1...', 'sql10181809', '\xE2\x81\xA0\xE2\x81\xA0\xE2\x81\xA0dfFf6l...')
#1 {main}
thrown in <b>/storage/ssd5/403/2053403/public_html/escanerproducto.php</b> on line <b>33</b><br />
PHP connection code:
<?php
#$db = new PDO("mysql:host=sql10.freesqldatabase.com;dbname=sql10181809", "sql10181809", "PASS");
if($db){
$sql = "select * from producto WHERE
codigo_barra='" . $codigodebarra . "'";
$query = $db->prepare($sql);
$query->execute();
$query->setFetchMode(PDO::FETCH_NUM);
if($fila = $query->fetch()){
$nombre = $fila[2];
$resultados_finales = array("nombre"=>$nombre);
echo json_encode($resultados_finales);
}
else{
$resultados_finales = array("mensage"=>"error");
echo json_encode($resultados_finales);
}
}
else {
$resultados_finales = array('mensage' => "ERROR.");
echo json_encode($resultados_finales);
};
?>
The best way to catch the exception is to put your code in a try-catch block. But in your case I think you are putting the password in incorrect. The code which causes error (exception) should be in try block and then catch it like so:
try{
$db = new PDO("mysql:host=sql10.freesqldatabase.com;dbname=sql10181809",
"sql10181809", "PASS");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e){
echo $e->getMessage() . ' ' . ' ' . getCode() . ' ' . getLine() . '
' . getFile();
}
You can read more about exception handling here.