Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

odbc connect and check issue 1

Status
Not open for further replies.

svar

Programmer
Aug 12, 2001
349
GR
I'm apparently having some trouble with an odbc connection. I deliberately to debug put the connect script in the main, but still no luck. odbc sees a true $link, but thinks it's not a 'resource'.

Code:
<?php
ini_set('display_errors', true);
error_reporting(E_ALL); 
if(session_id() == '') session_start(); 
	$debugOn=0;
global $link;
	ob_start();
foreach(PDO::getAvailableDrivers() as $driver) {
  echo $driver.'<br />';}// prints mysql,odbc,sqlite, so looks like the driver is there
$sql="SELECT ISCHECKED FROM KTIMP";

$dsn="C:\users\me\Desktop\sample\Sample.accdb";

  $link = new PDO(
 "odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};
Dbq=$dsn;Uid=; Pwd=;");

/* just a small test, grab the options to use later in listbox, e.g.
<?php
for($x=0;$x<$arrlength;$x++) {$str="
  <option value=".'"'. $var[$x] .'">'.$var .'</option>'; echo "$str";}?>*/


$Result = odbc_exec( $link, $sql );//
$indxd=0;
while( odbc_fetch_row( $Result ) )  
 {  $option = odbc_result($Result, 1);
$var[$indxd]=$option; $indxd++;
}
 
$arrlength=count($var);
if($link){echo "link not null";}// this says link not null


?>
I get
Warning: odbc_exec() expects parameter 1 to be resource, object given

 
Does PHP or the site user account have permissions to "C:\users\me\Desktop\sample\Sample.accdb"?

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Really pretty much every one of these odbc issues could be solved (or at least removed from the php forum)/if people would just create the dsn in the windoze plugin. Create a system Dan for the best flexibility. Test it and then migrate to a file or user dsn to lock down as may be needed for protecting the app.

Of course it can be done with a dsn-less connection string but why make life so hard?
 
That's what I did: Created a system dsn called sysdsn in
C:\users\me\Desktop\sample\Sample.accdb
Also tried / instead of
Still get the resource error message

 
You are creating a PDO object rather than a direct ODBC resource. PDO has its own methods and cannot be used with normal ODBC methods like odbc_exec.

if you want to use a PDO connection, then you need to use PDO methods.

You can't mix and match libraries and methods.

Code:
$sql="SELECT ISCHECKED FROM KTIMP";

$dsn="C:\users\me\Desktop\sample\Sample.accdb";

  $link = new PDO(
 "odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};
Dbq=$dsn;Uid=; Pwd=;");

[b]$res = $link->query($sql);[/b]



If you want to use odbc methods,then you ned to use a direct odbc connection.




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Thanks!

So either
Code:
$sql="SELECT ISCHECKED FROM KTIMP";

$dsn="C:\users\me\Desktop\sample\Sample.accdb";

  $link = new PDO(
 "odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};
Dbq=$dsn;Uid=; Pwd=;");

$res = $link->query($sql); 
// or $res = $link->prepare("$sql");
   //   $res->execute();
and then
Code:
foreach ($res as val){
echo "$val"; // if each row is a single element
}


If one wants to go with odbc, then
Code:
$link = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$mdbFilename", $user, $password);
$obj=odbc_exec($link,$sql); 
while(odbc_fetch_row($obj))
{ 
//collect results
$tipo=odbc_result($obj,1);// 1st column 
....

    }

//disconnect from database
    odbc_close(link);

Any preferences?
 
Correct.

Any preferences

PDO offers better error handling. And more robust methods. And is easier to change drivers for other DB's.







----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
you should just be able to connect using the system dsn

Code:
$pdo = new PDO('odbc:mysystemdsn');
$sql="SELECT ISCHECKED FROM KTIMP";
$s = $pdo->prepare($sql);
if($s === false):
 print_r($pdo->errorInfo());
 die;
endif;
$result = $s->execute();
if($result === false):
 print_r($s->errorInfo());
 die;
endif;
echo '<pre>';
while($obj = $s->fetchObject()):
 print_r($obj);
endwhile;
echo '</pre>';
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top