home
Home button Search button Dr. Hain buttonClinic website Information for Dizzy Patients Fun Various and Sundry

Dr. Hain's various/sundry has helpful information about things that Dr. Hain has discovered by trial and error.

PHP

We are fond of using PHP for simple little (ie. 1000 lines or less) projects. In fact, we have implemented a medical record system in PHP/mysql. However, it does have some major problems.

PHP scripts -- here are a collection of generally useful little routines I wrote to do stuff in PHP.

1. Recreating the command line arguments to a php page:

Sometimes you want to just call the page you are already on again, passing something new through the POST variables. There must be an easier way to do this, probably some obscure PHP $_SERVER variable, but this is how I managed. The first segment goes at the top:

// This piece of PHP goes at the top, to construct a "cloned" command line when one wants to have the same PHP program do something different after a user has clicked on a FORM submit button.

$currentPage = $_SERVER["PHP_SELF"];
$acount=0;
foreach ($_GET as $key => $value) {

if($acount++==0) $currentPage.='?';
else $currentPage.='&';
$currentPage=$currentPage.$key.'='.$value;
}
echo $currentPage;

When one then uses a form, later on, you do it like this:

<form id="form1" name="form1" method="POST" action="<?php echo $currentPage; ?>">, it will send the $_GET array arguments.

2. Stripping out numbers from the beginning of lines, as in 1. This is a line with a number in it.

MYSQL does not have a simple method of identifying lines that begin with numbers. The MYSQL regular expression code simply does not work, because it does not recognize periods. This is the workaround:

This code finds all lines in a field called "DIAGNOSIS" that begin with numbers. We start with this to reduce the work for PHP.

$query_DX = "SELECT * FROM `dx` WHERE DIAGNOSIS REGEXP '^[0-9]+'";
$DX = mysql_query($query_DX, $Mysql)or die(mysql_error());

This code goes through each record, and uses a working regular expression syntax in PHP to strip out numbers.

$row_DX = mysql_fetch_assoc($DX);

do {
$original = $replace = $row_DX['Diagnosis'];
// at start of line, match 1 or more digits, followed by period, followed by one or more white spaces, and strip out. (the regexp does not work in MYSQL)
$replaced = preg_replace('/^[0-9]+\.\s+/', "", $row_DX['Diagnosis'], 1, $count);

if(!$count) continue; // no matches

$u= sprintf("UPDATE dx set Diagnosis=%s, Comment2=%s where ID=%s", "'".$replaced."'","'".$original."'", $row_DX['ID']);
mysql_query($u, $Mysql)or die(mysql_error());
}

3. Converting to mysqli and Mysql 5.7

PHP 7+ forces one to convert to mysqli (from mysql) and also the Mysql version of 5.7 involves some "strict" changes, which break working code.

The syntax modifications between mysqli( ) and mysql() are tedious but not very serious. These could have been better thought out, but it is too late now.

The Mysql changes are very serious because they break things that cannot be fixed by recoding. The SQL_MODE problem is it now defaults to "strict". In particular,

The default SQL mode in MySQL 5.7 includes these modes: ONLY_FULL_GROUP_BY, STRICT_TRANS_TABLES, NO_ZERO_IN_DATE, NO_ZERO_DATE, ERROR_FOR_DIVISION_BY_ZERO, NO_AUTO_CREATE_USER, and NO_ENGINE_SUBSTITUTION."

There are two ways to do this: Work around, or Restructure.

© Copyright December 22, 2019 , Timothy C. Hain, M.D. All rights reserved.
Dr Hain's CV Clinic dizziness-and-hearing.com FLW Rant-Rave