All posts by elliott954

Test Your Configuration

  1. Open SQL Server Management Studio
    1. Create a SQL user with a password
    2. Create a database named “MyDatabase”
    3. Create a table in MyDatabase named TestTable
    4. Add two columns to TestTable
    5. Create login name “TestUser”
    6. Create a password “Tpass_PHPConnect”
    7. Add a user to the database you created above
    8. Assign read/write permissions to the user for the database you assigned the user to.
  2. Create a PHP script with the following lines and save it in your document root directory under the file name TestConnection.php
  3. Open a browser, and enter “http://localhost/TestConnection.php” as the URL

If everything works, congratulations!  If not, go to the trouble shooting section.

 

<?php
 // edit the lines between here and the next comment 
 // using values from your installation 
$DBName = "MyDatabase"; 
$user = "TestUser";
$pass = "Tpass_PHPConnect";
$host = "localhost"; 

$stmt = "select * from TestTable";
 // edit the lines between here and the previous comment 
$conn = DBConnect($DBName, $user, $pass,$host); 
echo "$conn <br>"; 
DBError(); 
$userData = sqlsrv_query($conn,$stmt, array(), array("Scrollable"=>"buffered")); DBError(); 
$gotrows = sqlsrv_has_rows($userData); if (!$gotrows) { 
$e = "WARNING No Rows found for SQL statement $stmt"; 
echo "$e <br>"; 
} 

While( $row = sqlsrv_fetch_array( $userData, SQLSRV_FETCH_ASSOC) ) { 
  $t = $row['returnoptions']; 
  echo "Selected Value => $t<br>";
 } 
$d = DBDisconnect($conn); DBError(); 


function DBDisconnect($conn) { 
$rtn = sqlsrv_close( $conn ); 
if ($rtn) { 
  $result = "SUCCESS $conn"; } else { 
  $result = "ERROR $conn"; } 
  echo $result; 
return $result; 
} 


function DBError() { 
//echo "entering dberror"."<br />"; 
$errmessage = "";
$rtn = false; 
// returns true if no errors are found, else false 
if( ($errors = sqlsrv_errors() ) !== null) { 
  $rtn = true; //there was an error 
  foreach( $errors as $error ) { 
    $errmessage = "SQLSTATE ERROR: ".$error[ 'SQLSTATE']; 
    echo $errmessage;
    $errmessage = "code: ".$error[ 'code'];
    echo $errmessage; 
    $errmessage = "message: ".$error[ 'message']; 
    echo $errmessage;
   } 
} 
return $rtn;
} 

function DBConnect($DBName, $user, $pass, $host) { 
$connectinfo = array( "Database"=>$DBName, "UID"=>$user, "PWD"=>$pass); 
$r = sqlsrv_connect( $host, $connectinfo); 
if( ($errors = sqlsrv_errors() ) !== null) { 
  $rtn = true; 
  //there was an error 
  foreach( $errors as $error ) { 
    $errmessage = "SQLSTATE ERROR: ".$error[ 'SQLSTATE']; 
    echo $errmessage."<br>";
    $errmessage = "code: ".$error[ 'code'];
    echo $errmessage."<br>"; 
    $errmessage = "message: ".$error[ 'message']; 
    echo $errmessage."<br>"; 
  }  
} 
return $r; 
} 

?>

 

Calculate Fletcher Checksum from Serial Port

Calculate the Fletcher-16 checksum as data streams into the serial port….

johngfletcher-image

John G. Fletcher

 

nt dex0=-1;
boolean comComplete0 = false;
char buffer0[200];
byte sum01, sum02;

void setup() {
  // initialize both serial ports:
  Serial.begin(9600);
  //Serial1.begin(9600);
}

void loop() { 
    // if the SerialEvent() function detected a string 
    // that ended in a line feed ('/n'), then 
    // print the fletcher-16 checksum and the serial data
    // to port 0
    if (comComplete0) {   
      Serial.print(“sum1=");
      Serial.print(sum01);
      Serial.print(",");
      Serial.print(“sum2=");
      Serial.print(sum02);
      Serial.print(“:  “);
      Serial.println(buffer0);
      dex0 = -1;
      //reset the comComplete flag
      comComplete0 = false;
      //reset the checksum
      sum01 = 0;
      sum02 = 0; 
    }

void serialEvent() {
  while (Serial.available()) {
    dex0 ++;
    byte inChar = Serial.read();
    //The next lines calculate the fletcher checksum and
    //as each character is read from the com port.
    //put it into the global variables sum01 and sum01
    //
    sum01 = (sum01 + inChar) % 255;
    sum02 = (sum01 + sum02) % 255;   
    if (inChar == '\n') {           //line feed
      buffer0[dex0] = inChar;
      dex0++;
      buffer0[dex0] = '\0';
      dex0--;
      comComplete0 = true;
      return;
    }
    buffer0[dex0] = inChar;
  }
  return;
}

	

Joe Cocker

In October, 1996 I had a chance to interview Joe Cocker at the Mad Dog Ranch in Crawford, Colorado.

Joe started out speaking about Ed Sullivan, finding Ray Charles’ music in Sheffield, England, and the influences that shaped his early days.  He finished up speaking about his current music just before the time his album “Organic” came out.

Downloading Files with PHP

These Things Will Help You Avoid Issues When Using PHP to Download Files

  • Make sure all your PHP files do not have empty lines after the closing PHP tag  ( ?>)
    • This rule includes the case where JAVASCRIPT or JQuery is placed inline with PHP code.  There should not be an empty line between the closing PHP tag (?>) and the opening JAVASCRIPT tag (<SCRIPT…> )
  • Don’t issue any echos other than the ones necessary to handle the bitstream generated by fread
    • This is because echo statements writes to the same buffer as PHP statements such as the following:
      • echo “Content-Disposition: filename=\””.$path_parts[“basename”].”\””;:
  • If you call SQL Server stored procedures, make sure there are not Print statements in the stored procedure.   Unconsumed print statements can cause writes to the same buffers that are used by the data streams for file downloads.
  • If your download script causes SQL Server triggers to fire, the same rule about Print statements applies:   Make sure no triggers have print statements.