Jump to content

fractil

Members
  • Posts

    10
  • Joined

  • Last visited

    Never

Everything posted by fractil

  1. This code worked great for querying a MSSQL database and writing to a .xls file. I just made the following changes to use an odbc connection. [code]<?php // Connect to database $sqlconnect=odbc_connect("<MSSQL SERVER>","<USER>","<PASSWORD>"); $query="SELECT * FROM <table>;"; // Get data records from table. $result=odbc_exec($sqlconnect,$query); $count = odbc_num_fields($result)+1; for ($i = 1; $i < $count; $i++){     $header .= odbc_field_name($result, $i)."\t"; } $value=""; $data=""; $line=""; while($row = odbc_fetch_array($result)){   $line = '';   foreach($row as $value){     if(!isset($value) || $value == ""){       $value = "\t";     }else{ # important to escape any quotes to preserve them in the data.       $value = str_replace('"', '""', $value); # needed to encapsulate data in quotes because some data might be multi line. # the good news is that numbers remain numbers in Excel even though quoted.       $value = '"' . $value . '"' . "\t";     }     $line .= $value;   }   $data .= trim($line)."\n"; } # this line is needed because returns embedded in the data have "\r" # and this looks like a "box character" in Excel   $data = str_replace("\r", "", $data); # Nice to let someone know that the search came up empty. # Otherwise only the column name headers will be output to Excel. if ($data == "") {   $data = "\nno matching records found\n"; } # This line will stream the file to the user rather than spray it across the screen header("Content-type: application/octet-stream"); # replace excelfile.xls with whatever you want the filename to default to header("Content-Disposition: attachment; filename=excelfile.xls"); header("Pragma: no-cache"); header("Expires: 0"); echo $header."\n".$data; ?>[/code] Hope this helps those who are forced to deal with a MSSQL connection. Cheers!
  2. www.freesqlhost.com Cheers!
  3. In the "Error handling and logging" section of your php.ini file you can change a couple of settings to meet your needs. First make sure that the error reporting setting is set to the following: error_reporting = E_ALL If you want errors to echo to your browser page you should change the following setting to on: display_errors = On If you want to log errors to a file turn off the display_errors setting and set the path of the error log: error_log = "C:\php\error_log.txt" Cheers!
  4. Are you attempting to search the entire database for the "keyword", or a specific table in the database? Cheers!
  5. I believe you can just use NOW(). See: [a href=\"http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html\" target=\"_blank\"]http://dev.mysql.com/doc/refman/5.0/en/dat...-functions.html[/a] Cheers!
  6. It seems you are storing your "logged in" status variables locally and not writing to your database. If you want to maintain the "logged in" status while exploring different links within your site you will need to store that "logged in" variable in your database and not locally to your index.php Cheers!
  7. I figured it out. You can use UPDATE in a While loop. I was updating my $Width and $Height variables, but I was not updating my $sqlquery variable which had $Width and $Height embedded in it. Once I started updating my $sqlquery variable every time the $Width and $Height variables changed every executed as expected. Cheers! [code]$x="'21-2000SL'"; $y="'11-1000SL'"; $Width=24; $Height=12; $dsn="sandman"; $username="sa"; $password="linford"; $base_query = "UPDATE [Price Grid] SET ListPrice = (SELECT ListPrice FROM [Price Grid] WHERE (PartNo = ".$x.") AND (Width = [Width]) AND (Height = [Height])) WHERE (PartNo = ".$y.") AND (Width = [Width]) AND (Height = [Height])"; while($Width <= 72) {     while($Height <= 72)     {         echo "$Width X $Height\n<br>";         $sqlquery = str_replace( "[Width]", $Width, $base_query );         $sqlquery = str_replace( "[Height]", $Height, $sqlquery );         $sqlconnect=odbc_connect($dsn,$username,$password);         $process=odbc_exec ($sqlconnect, $sqlquery);         odbc_close($sqlconnect);         $Height = $Height + 6;     }     $Height = 17.9375;         while($Height <= 72)         {             echo "$Width X $Height\n<br>";             $sqlquery = str_replace( "[Width]", $Width, $base_query );             $sqlquery = str_replace( "[Height]", $Height, $sqlquery );             $sqlconnect=odbc_connect($dsn,$username,$password);             $process=odbc_exec ($sqlconnect, $sqlquery);             odbc_close($sqlconnect);             $Height = $Height + 6;         }     $Height = 12;     $Width = $Width + 6; }[/code]
  8. Continuing with GeoffOs post, I find it helpful to include the database name in the FROM clause. just do a select on the information_schema.COLUMNS table: select column_name from <dbname>.information_schema.columns where table_name = '<tablename>' see: [a href=\"http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_ia-iz_4pbn.asp\" target=\"_blank\"]http://msdn.microsoft.com/library/default...._ia-iz_4pbn.asp[/a] Cheers!
  9. [quote name='fractil' date='Jun 22 2006, 06:08 PM' post='387023'] I am using a odbc connection to query a ms sql database. *bump*
  10. I am using a odbc connection to query a ms sql database. I can successfully connect to and query the database from a php script. I need to UPDATE multiple entries and have written a While loop that successfully advances my variables which represent the table columns used in the WHERE clause. I then inserted the query statement into the While loop and ran the script. The script runs with no errors. All expected variable results are echoed (so I can watch/test it's progress). But the query is only executed once, the first time through the loop. I am perplexed. My code is included below. <html> <head><title>priceupdate.php</title></head> <body> <?php $x="'21-2000SL'"; $y="'11-1000SL'"; $Width=24; $Height=12; $dsn="sandman"; $username="sa"; $password="linford"; $sqlconnect=odbc_pconnect($dsn,$username,$password); $sqlquery="UPDATE [Price Grid] SET ListPrice = (SELECT ListPrice FROM [Price Grid] WHERE (PartNo = $x) AND (Width = $Width) AND (Height = $Height)) WHERE (PartNo = $y) AND (Width = $Width) AND (Height = $Height)"; while($Width <= 72) { while($Height <= 72) { echo "$Width X $Height\n<br>"; $process=odbc_exec ($sqlconnect, $sqlquery); $Height = $Height + 6; } $Height = 17.9375; while($Height <= 72) { echo "$Width X $Height\n<br>"; $process=odbc_exec ($sqlconnect, $sqlquery); $Height = $Height + 6; } $Height = 12; $Width = $Width + 6; } odbc_close($sqlconnect); ?> </body> </html>
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.