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);
}
Related
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.
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.
Here is the exception that I am getting:
ErrorException in DocketController.php line 65: odbc_connect(): SQL
error: [Microsoft][ODBC Driver Manager] The specified DSN contains an
architecture mismatch between the Driver and Application,
And here is the Docketcontroller.php line number 65:
// Localhost
$dsn = "test2";
$conn=odbc_connect($dsn, "QA", "QA");
if (!$conn) {
exit("Connection Failed: " . $conn);
}
$rs=odbc_exec($conn,$query);
//odbc_result_all($rs);exit;
$res=array();
$fields=array();
$i=0;
while($myRow = odbc_fetch_row( $rs ))
{
//echo"<pre>";print_r(odbc_result($rs,1));echo"</pre>";
$columns= odbc_num_fields ( $rs );
PDOException: SQLSTATE[42000]: Syntax error or access violation: 1226
User 'bb99ddb719cd2f' has exceeded the 'max_questions' resource
(current value: 3600) in execute().
Earlier code was working file but suddenly it has started giving errors at every execute().
Here is my code:
Actual file:
<?php
$sql="SELECT count(job_status.is_approved) as is_approved
from job_status
WHERE job_status.job_id=:job_id and job_status.is_approved=1";
$sth=$conn->prepare($sql);
$sth->bindValue("job_id",$job_id);
try {
$sth->execute();
}
catch(Exception $e) {
Rollbar::report_exception($e);
}
$res=$sth->fetchAll();
$stats=$res[0]['is_approved'];
$status=$stats?'1':'0';
return $status;
Db connection file:
<?php
require_once('config.php');
try {
$dsn = "mysql:host=$DB_HOST;dbname=$DB_DATABASE";
$conn = new PDO($dsn, $DB_USER, $DB_PASSWORD);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->exec("SET NAMES 'utf8'");
}
catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
?>
Some servers have query limit same is the case with heroku in cleardb i.e., 3600 QPH. So to resolve this issue we bought the paid version of cleardb to extend the query limit.
Hi guys i want to connect USERNAME,PASSWORD protected MS ACCESS 2000 (.*mdb) database using PHP, ODBC or any other database connection. But given below code show WARNING and doesn't connect to the database. please anyone help.
CODE 1
$mdbFilename = "C:\Documents and Settings\Hp\Desktop\Door\DB\IMSDB.mdb";
$user = 'test';
$password ='123';
$connection = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};DBQ=$mdbFilename;",$user,$password);
if($connection)
{
echo "Success";
}
else
{
exit("Connection Failed: ".$connection);
}
WARNING MESSAGE
Warning: odbc_connect() [function.odbc-connect]: SQL error: [Microsoft][ODBC Microsoft Access Driver] You do not have the necessary permissions to use the '(unknown)' object. Have your system administrator or the person who created this object establish the appropriate permissions for you., SQL state 42000 in SQLConnect in C:\xampp\htdocs\punch\test.php on line 6
Output -> Connection Failed
CODE 2
I tried given below code also. there are no any ERRORS or WARNING MESSAGE but not connect to the access database.
$mdbFilename = "C:\Documents and Settings\Hp\Desktop\Door\DB\IMSDB.mdb";
$systemDbFilename = "C:\Documents and Settings\Hp\Desktop\Door\DB\SYSTEM.MDW";
$user = 'test';
$password ='123';
$adoCon = new COM("ADODB.Connection") or die("Cannot start ADO");
$connection= $adoCon->Open("Driver={Microsoft Access Driver (*.mdb)};DBQ=$mdbFilename; SystemDB=$systemDbFilename;Uid=$user; Pwd=$password;");
if(($connection->State) > 0)
{
echo "Success";
}
else
{
exit("Connection Failed: ".$connection);
}
Output -> Connection Failed
CORRECT ANSWER
Finally I found the answer. I tried PDO to connect ODBC.
WORKING CODE
try {
$dbh = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=D:\Door\DB\IMSDB.mdb;SystemDB=D:\Door\DB\SYSTEM.MDW;Uid=test;Pwd=123;");
if($dbh)
{
echo "Connection Success";
}
}
catch (PDOException $e)
{
echo $e->getMessage();
}