kleenhead Posted February 28, 2010 Share Posted February 28, 2010 there's one suggestion for reading an access database, it says to create a custom function to connect, then because there are issues specific to my setup, avoid using odbc_fetch_array and create another custom function for that too. what I have so far reads the db row by row during a loop and puts it into a variable, but after the loop is finished the variable goes kapoof and I can't figure out how to read it. here's the whole thing, quite messy with all the comments and diagnostics using print statements and farm animals. --- begin code -------------------------------------------------------------------------------------------------------- <?php $db = "itemsdb"; function odbc_connect_custom($db) { // $dbdir is the location of the database. $dbdir = "D:/box/b523945/xmu/"; $cfg_dsn = "DRIVER=Microsoft Access Driver (*.mdb); DBQ=".$dbdir.$db.".mdb;UserCommitSync=Yes; Threads=3; SafeTransactions=0; PageTimeout=5; MaxScanRows=8; MaxBufferSize=2048; DriverId=281; DefaultDir=C:/ProgramFiles/CommonFiles/ODBC/DataSources"; // The DefaultDir setting will probably be ok if you have gone for // a typical installation $cfg_dsn_login = ""; $cfg_dsn_mdp = ""; return odbc_connect($cfg_dsn,$cfg_dsn_login,$cfg_dsn_mdp); } //diagnostic print "1"; ?> <?php //fixed vairable $num = "51"; // set ODBC connection identifier $connection_id = odbc_connect_custom($db); //diagnostic print "2"; // set the query string $query = "SELECT * FROM tblItems"; //diagnostic print "3"; // execute query, put in a variable $result_id = odbc_exec($connection_id,$query); //diagnostic print "<br>"; print 4; print "<br>"; print "result_id= ".$result_id; print "<br>"; print "odbc_num_fields= ".odbc_num_fields($result_id); print "<br>"; print "now for the custom fetch array..."; print "<br>"; ?> <?php ////////////////////////// // custom fetch array ////////////////////////// function odbc_fetch_array_custom($result_id) { // Clear our return variable unset($ar); // Get the number of fields in the returned result $colnum = odbc_num_fields($result_id); //diagnostic print "CFL-colnum= ".$colnum; print "<br>"; // Fill an array with the column headers // We do this here to save API calls. for($i=1; $i<=$colnum;$i++)$field_name[$i]=odbc_field_name($result_id,$i); // If we have a result if (odbc_fetch_row($result_id)) { // Fill our return array with the column data for ($i=1;$i<=$colnum;$i++) { $ar[$field_name[$i]]=odbc_result($result_id, $field_name[$i]); //diagnostic print "i=".$i; print "<br>"; print "ar[".$field_name."[".$i."]]=".$ar[$field_name[$i]]."<br>"; print "field_name= ".$field_name."<br>"; print "oink= ".$ar[Array[$i]]."<br>"; print "<br>"; } return $ar; //return $i; unneccessary //diagnostic // print "returned ar"; } else {return false;} } //diagnostic print "<br>"; print "after custom function loop"; print "<br>"; print "x2-colnum= ".$colnum; print "<br>"; ?> <? ////////////////// // run it ////////////////// // odbc_connect_custom again $connection_id = odbc_connect_custom($db); //diagnostic print "after odbc_connect<br>"; print "<br>"; $var1="Dishes"; $var2="1"; print "quack<br>"; print $ar[$var1[$var2]]; print "moo<br>"; //alternate instead of while loop //odbc_fetch_array_custom($result_id) //print odbc_fetch_array_custom($result_id); while(odbc_fetch_array_custom($result_id)) { //diagnostic print "while loop<br>"; print "<br>"; // Do whatever we want with the row print "result=".$ar[$var1[$var2]]."<br>"; //print($var2[$var2]."<br>"); //print($row["Plates"]."<br>"); } //////////////////// //close database //////////////////// odbc_close($connection_id) //close out variables //dummy vars //$bbb="3265"; //$aaa = "1554"; //unset($aaa,$bbb); ?> --- end code ----------------------------------------------------------------------------------------- I've tried changing it all around and it still doesn't work, what do I do? Link to comment https://forums.phpfreaks.com/topic/193625-cant-read-access-database-array-into-a-variable/ Share on other sites More sharing options...
jcanker Posted February 28, 2010 Share Posted February 28, 2010 before I start tracing all the loops, exactly which variable is it that's going kapoof? The whole $ar array? If these are intended as multi-dimensional arrays, don't nest the brackets, but rather put them inline: $ar[0][3] to get the value of the 3rd key in the first key of the $ar array. Link to comment https://forums.phpfreaks.com/topic/193625-cant-read-access-database-array-into-a-variable/#findComment-1019241 Share on other sites More sharing options...
kleenhead Posted February 28, 2010 Author Share Posted February 28, 2010 I tried un-nesting the array brackets, that didn't work. I cleaned it up to make it easier to read. the last part that prints "result"'s doesn't put out anything except "result1= " not the $field_name either. I thought that would be left over and still available to print out. --- begin code -------------------------------------------------- <?php //////////////////////////// // custom odbc connection //////////////////////////// function odbc_connect_custom($db) { $db = "itemsdb"; $dbdir = "D:/box/b523945/xmu/"; $cfg_dsn = "DRIVER=Microsoft Access Driver (*.mdb); DBQ=".$dbdir.$db.".mdb;UserCommitSync=Yes; Threads=3; SafeTransactions=0; PageTimeout=5; MaxScanRows=8; MaxBufferSize=2048; DriverId=281; DefaultDir=C:/ProgramFiles/CommonFiles/ODBC/DataSources"; $cfg_dsn_login = ""; $cfg_dsn_mdp = ""; return odbc_connect($cfg_dsn,$cfg_dsn_login,$cfg_dsn_mdp); } ////////////////////////////////////////////////////////////////////// ////////////////////////// // get result id ////////////////////////// // set ODBC connection identifier $connection_id = odbc_connect_custom($db); // set the query string $query = "SELECT * FROM tblItems"; // execute query, put in a variable $result_id = odbc_exec($connection_id,$query); ////////////////////////////////////////////////////////////////////// ////////////////////////// // custom fetch array ////////////////////////// function odbc_fetch_array_custom($result_id) { // Clear out return variable unset($ar); // Get the number of fields in the returned result $colnum = odbc_num_fields($result_id); // Fill an array with the column headers for($i=1; $i<=$colnum;$i++) $field_name[$i]=odbc_field_name($result_id,$i); // If we have a result if (odbc_fetch_row($result_id)) { // Fill the return array with the column data for ($i=1;$i<=$colnum;$i++) $ar[$field_name[$i]]=odbc_result($result_id, $field_name[$i]); return $ar; } else {return false;} } ////////////////////////////////////////////////////////////////////// ////////////////// // run it ////////////////// // odbc_connect_custom again odbc_connect_custom($db); $connection_id = odbc_connect_custom($db); while(odbc_fetch_array_custom($result_id)) { //diagnostic print "while loop<br><br>"; // testing output print "xx-field_name= ".$field_name."<br>"; print "result1= ".$ar[$field_name[$i]]."<br>"; print "result2= ".$ar[1[2]]."<br>"; print "result3= ".$ar[1][2]."<br>"; //diagnostic print "<br>while returned<br>"; } // testing output again after while loop print "xx-field_name= ".$field_name."<br>"; print "result1= ".$ar[$field_name[$i]]."<br>"; print "result2= ".$ar[1[2]]."<br>"; print "result3= ".$ar[1][2]."<br>"; //////////////////// //close database //////////////////// odbc_close($connection_id) ?> --- end code ---------------------------------------------------------- Link to comment https://forums.phpfreaks.com/topic/193625-cant-read-access-database-array-into-a-variable/#findComment-1019566 Share on other sites More sharing options...
kleenhead Posted March 2, 2010 Author Share Posted March 2, 2010 anybody? Link to comment https://forums.phpfreaks.com/topic/193625-cant-read-access-database-array-into-a-variable/#findComment-1020241 Share on other sites More sharing options...
teamatomic Posted March 3, 2010 Share Posted March 3, 2010 You forgot: $you='kleenhead'; Link to comment https://forums.phpfreaks.com/topic/193625-cant-read-access-database-array-into-a-variable/#findComment-1020733 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.