JQGrid and Microsoft SQL Server - php

I am trying to get this awesome JQuery plugin working with SQL Server. I have setup PDO as I use it elsewhere (with MS Access) - I think I have it setup with sql server (2008) now. Here is how I connect:
define('DB_DSN',"odbc:Driver={SQL Server};Server=MyInstance;Database=table1;");
define('DB_USER', '');
define('DB_PASSWORD', '');
// include the jqGrid Class
require_once "php/jqGrid.php";
// include the PDO driver class
require_once "php/jqGridPdo.php";
// Connection to the server
$conn = new PDO(DB_DSN,DB_USER,DB_PASSWORD);
// Tell the db that we use utf-8
$conn->query("SET NAMES utf8");
// Create the jqGrid instance
$grid = new jqGridRender($conn);
$tsql = 'SELECT * FROM Trt';
// Write the SQL Query
$grid->SelectCommand = $tsql;
// set the ouput format to json
$grid->dataType = 'json';
// Let the grid create the model
$grid->setColModel();
// Set the url from where we obtain the data
$grid->setUrl('report-creator.php');
// Set grid caption using the option caption
$grid->setGridOptions(array(
"caption"=>"Report",
"rowNum"=>10,
"sortname"=>"OrderID",
"hoverrows"=>true,
"rowList"=>array(10,20,50),
));
$grid->toolbarfilter = true;
$grid->setFilterOptions(array("stringResult"=>true));
$grid->renderGrid('#grid','#pager',true, null, null, true,true);
$conn = null;
I get this error:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[IM001]: Driver
does not support this function: driver doesn't support meta data' in
C:\wamp\www\webs\tgd\edh\php\jqGridPdo.php(1) : eval()'d code:1 Stack trace: #0
C:\wamp\www\webs\tgd\edh\php\jqGridPdo.php(1) : eval()'d code(1): PDOStatement-
>getColumnMeta(0) #1 C:\wamp\www\webs\tgd\edh\php\jqGrid.php(1) : eval()'d code(7):
jqGridDB->getColumnMeta(0, Object(PDOStatement)) #2 C:\wamp\www\webs\tgd\Front- End\report-
creator.php(31): jqGridRender->setColModel() #3 C:\wamp\www\webs\tgd\Front- End\view-
report.php(123): require_once('C:\wamp\www\web...') #4 {main} thrown in
C:\wamp\www\webs\tgd\edh\php\jqGridPdo.php(1) : eval()'d code on line 1
I appreciate any help - I just can't understand the problem. I have narrowed it down to this function: $grid->SelectCommand - if I remove this no error occurs but I can not do without this as this is where my t-sql to query the database goes.
I am able to query SQL server mysql using PDQ->Query('SELECT * FROM table1').
Thanks all
Please note: I have edited table name, file paths for privacy. :)

Looks like jqGrid uses getColumnMeta function to enumerate columns, but this function may be unsupported by several drivers - in this case, your MSSQL driver.
See examples in the link above to test if getColumnMeta works.

I work for Trirand (the company that developers jqGrid - ASP.NET team) and unfortunately cannot help with PHP much, but I can suggest posting the very same question in our own forums as well:
http://www.trirand.net/forum/
there you will find a PHP jqGrid forum and you will get a timely response from our PHP team. Meanwhile I will write them an email and ask them to help here as well, but just in case posting there will guarantee you an answer.
Cheers,
Rumen Stankov
Trirand Inc

I couldn't solve this so I switched over to another class which was written by the JQGrid guys that makes use of the PHP driver from Microsoft. This solved my problem, hope it helps someone.

Related

Can't get PHP PDO and OOP classes to work [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am trying to learn how to use PHP PDO as Object Oriented Programming.
I have tried following these two tutorials:
http://culttt.com/2012/10/01/roll-your-own-pdo-php-class/
http://culttt.com/2012/09/24/prevent-php-sql-injection-with-pdo-prepared-statements/
but I can't get anything on either of them to work.
The second one gives a download link to a pre-written wrapper class class.db.php from http://www.imavex.com/php-pdo-wrapper-class/
using this pre-written wrapper class and trying something as simple as this tutorial.php (credentials changed):
// Include the database class
include("class.db.php");
// Connect to database
$db = new db("mysql:host=localhost;dbname=my-db-name", "my-username", "my-password");
$results = $db->select("ad_publication");
print_r($results);
The above shows a blank page.
I know there is nothing wrong with the pre-written class and the text of the above example as it was copied directly out of the tutorial and the comments are full of thanks and praise.
I know there is nothign wrong with my credentials as this works fine:
try
{
$pdo = new PDO('mysql:host=localhost;dbname=my-db-name', 'my-username', 'my-password');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec('SET NAMES "utf8"');
$output = 'Connection Successful';
echo $output;
}
catch (PDOException $e)
{
$output = 'Unable to connect to the database server.' . $e->getMessage();
echo $output;
exit();
}
And outputs Connection Successful
My Server is running PHP 5.5 and the table used in the example above is an InnoDB table.
When I run the example select statement my error logs show:
PHP Notice: Undefined variable: GhG678 in /var/www/vhosts/mywebsite.com.au/httpdocs/booking/tutorial.php on line 7
line 7:
$db = new db("mysql:host=localhost;dbname=my-db-name", "my-username", "my-password");
PHP Warning: Creating default object from empty value in /var/www/vhosts/mywebsite.com.au/httpdocs/booking/class.db.php on line 18
line 18:
$this->error = $e->getMessage(); // (from public function __construct)`
PHP Fatal error: Call to a member function select() on a non-object in /var/www/vhosts/mywebsite.com.au/httpdocs/booking/tutorial.php on line 9
line 9:
$results = $db->select("ad_publication"); // (an existing table with data in it)
I just can't see what I could be doing wrong especially as the wrapper class was not written by me and no-one else is complaining about it (heaps of praise) and the contents of tutorial.php were copied directly from the page with only the table name changed.
Like I say, using the PDO connection and doing normal PDO queries without the wrapper class, work fine.
Can anyone here see anything that could be going wrong or know of anything that I should look at?
I'm not sure why I copped a -1 for that question??
I thought it was very complete.
Anyway, thanks to #Sean for providing the clue to the answer.
My password does in fact have a $ character in it.
The connection code of mine that does work (as shown above) is:
$pdo = new PDO('mysql:host=localhost;dbname=my-db-name', 'my-username', 'my-password');
Their code that I was using is:
$pdo = new PDO("mysql:host=localhost;dbname=my-db-name", "my-username", "my-password");
changing the " characters to ' worked straight away.
The password was auto-generated by my hosts (Plesk) and I don't know enough about PHP to know that there is a difference between ' and ". I've never known why some people use one and some use the other. Seems I still have a lot to learn.
#Sean, because you didn't put it as a reply, I couldn't choose your suggestions as the answer, so I don't know how to give you the points, but thank you very much for steering me in the right direction.

Detecting type of database connection in drupal

How can I detect what type of database I'm connecting to in drupal (using php code)? I am trying to write a module which exposes some database functionality which only works on postgres or sql server. Currently, I'm doing it by trying to detect the database version, since the syntax appears to be different for each database but it doesn't seem very robust. Is there a php function which will report this?
You should use the global variable: $databases and check the driver value.
global $databases;
dpm($databases);
https://drupal.stackexchange.com/questions/48882/how-to-get-database-credentials
I don't fully understand what you're trying at the moment but
db_driver()
returns he currently connected database in Drupal as a string (e.g. "pgsql").
This may be helpfull to you. Use db_set_active() API to set your database before executing the query.
This will help you to keep away from errors
<?php
//set your configurations
$db_url['default'] = 'mysql://drupal:drupal#localhost/drupal';
$db_url['mydb'] = 'mysql://user:pwd#localhost/anotherdb';
$db_url['db3'] = 'mysql://user:pwd#localhost/yetanotherdb';
//set the active database and then process your queries in this case you can always knows which database is connected now.
db_set_active('mydb');
db_query('SELECT * FROM table_in_anotherdb');
Setting multiple databases
<?php
// ... header of the settings.php file
$db_url = array (
"default" => "mysql://user:pass#host/db",
"second" => "pgsql://user:pass#host/db"
);
db_set_active('second');
Ref: https://drupal.org/node/18429

How make database connection using aura sql?

I am using mysql. And i want to connect with database using aura sql.
<?php
$connection_factory = include '/Aura/scripts/instance.php';
$connection = $connection_factory->newInstance(
// adapter name
'mysql',
// DSN elements for PDO; this can also be
// an array of key-value pairs
'host=localhost;dbname=db_aura',
// username for the connection
'root',
// password for the connection
''
);
$result = $connection->fetchAll('SELECT * FROM tbl_test');
?>
The above code shows the error
Parse error: parse error in C:\wamp\www\1\Aura\src\Aura\Sql\Connection\AbstractConnection.php on line 65
Finally I got it...
The code given above in my question is correct. Error occured due the version of my php i used. PHP version above 5.4 only support this. Now am using php 5.4.12 and aura sql is able to connect with mysql and working well.. Thanks.

error when update table with odbc in php

I have database with DBF format, and use the ODBC to access the database.
When i do update on the table, it returns error:
Warning: odbc_execute() [function.odbc-execute]: SQL error: [Microsoft][ODBC dBase Driver] Index not found., SQL state S0012 in SQLExecute in C:\xampp\htdocs\payroll\index.php on line 16
According to the error msg, it seems like the index not found. what index is that? how to fix?
Below is the PHP script that i use to do the query.
$odbc = odbc_connect ('payroll', '', '') or die('Error connecting to server. Server says: '.odbc_errormsg());
$upd_q = "UPDATE paytran SET empno = 22 WHERE empno = 888";
$update = odbc_prepare($odbc, $upd_q);
$result = odbc_execute($update);
odbc_close($odbc);
Same method was used to insert new record. Insert query works successfully, but not for DELETE and UPDATE query.
According to the last post in this thread,
If you have a .dbf file nowadays, more often than not, it is from a
FoxPro application. And, since you have a .cdx file, that tells you
it is a FoxPro compound index file and dBASE can't use it and
certainly can't take advantage of it to get any speed advantage that
you would like in your queries.
Do you have a .cdx file? If so, you may want to check which version you have and then find the FoxPro driver or OLE DB provider, and use that instead of ODBC. There's information on how to do it here and here; the driver and DB provider are here.

How can i import SQLite db3 file

Sorry about this question but i never worked with SQLite, and i gone through the SQLite site , downloaded "Precompiled Binaries For Windows" files tried to make them usable for my purpose but i couldnot, i saw with working tutorials with databases,tables.
My tasks is to get data from SQLite and put them into magento mysql DB. So for that i have got the SQLite DB dump file as .db3 file, txt file which has size of db dump file(20110125_SIZE).
So how can i import it into the SQLite. Please if anyone of you worked with SQLite3 help me to understand the .db3 file and how can i see the records from them.
I have got sqlite3 db dump as dbname.db3,
So then i tried to connect this sqllite from php. Here is my sample code which i got it from forum.
$db = openDatabase();
unset($db);
function openDatabase() {
$dbFile = realpath('dbname.db3');
echo $dbFile . "\n";
$needDbCreate = !file_exists($dbFile);
$db = new SQLiteDatabase($dbFile) or die((file_exists($dbFile)) ? "Unable to open" : "Unable to create");
if($needDbCreate) {
}
return $db;
}
But i am getting fatal exception.
Fatal error: Uncaught exception
'SQLiteException' with message
'SQLiteDatabase::_construct() [sqlitedatabase.--construct]:
file is encrypted or is not a
database' in
C:\wamp\www\SQLITE\index.php:23 Stack
trace: #0
C:\wamp\www\SQLITE\index.php(23):
SQLiteDatabase->_construct('C:\wamp\www\SQL...')
1 C:\wamp\www\SQLITE\index.php(15): openDatabase() #2 {main} thrown in
C:\wamp\www\SQLITE\index.php on line
23
But when i tried the same sqldump with the PDO , i got connected.
try {
$dbh = new PDO("sqlite:C:\wamp\www\SQLITE\dbname.db3");
echo 'Db Connected <br/>';
}
catch(PDOException $e)
{
echo $e->getMessage();
}
here i do not know the list of tables available in this dump so how can i query them to list and then getting records from them.
Please help me to solve this issue ti get connected and browse the tables from php.
Sorry for posting here in answer
section, i wanted to highlight the
code.
Thanks
On a system where SQLite is installed, you will usually also have the sqlite3 command line program. It can be used both from the command line or in interactive mode to dump data or load it into a (binary) database file.
sqlite3 ./database.file
This will give you the interactive prompt, where you can issue SQL commands or special commands such as .help or .dump.
There are also more graphical tools, but they are probably overkill for what you want to do.
Edit: seeing your reply (currently in the answer section), it seems that your .db3 file simply is not in the binary SQLite3 format, but instead perhaps a dump. That would be a problem. If it is a dump, you'd have to load it into a proper database file first.
cat yourdump.sql|sqlite3 ./realdb.db3
call three times
$rr=exec("sqlite3 database.db \".separator '|'\" ");
$rr=exec("sqlite3 database.db \".import myfile.txt tablename\" ");
$rr= exec ("sqlite3 database.db \"pragma encoding = 'utf-8'\" ");
or
single line
$rr=exec("sqlite3 database.db \".separator '|'\" && sqlite3 database.db \"pragma encoding = 'utf-8'\" && sqlite3 database.db \".import myfile.txt tablename\" ");

Categories