Posts Tagged ‘Script’

Mysql-Test your MySQL queries inside a MySQL GUI before coding them

Thursday, July 24th, 2008

Before you start coding your application with PHP or whatever remember to test out your MySQL in your MySQL GUI such as SQLYOG or even the CLI

What are the advantages?

* You don’t have to worry about the Script/MySQL syntax clashes (quoting etc)
* You only have to think MySQL and not say PHP as well
* You can see the data produced or not produced
* You can test your concepts

Is PHP good enough for science?

Tuesday, July 15th, 2008

My ‘day job’ has nothing to do with PHP. It has nothing to do with any form of programming. I graduated in 2006 with a degree in Biochemistry and went on to do a MSc and now PhD in cardiovascular biology. The closest most of my colleagues come to programming is a formula in an Excel spreadsheet.

It was actually Excel which prompted this post. Yesterday I was analysing some data and bemoaning the poor search functionality that Excel makes available. I had already expanded the small set of experimental data I had with some values pulled from a web service using a quickly hacked together PHP script and it got me to wondering how much better things could be if I just stuck with PHP.

Where’s the science?

This train of thought led on to whether PHP has been used all that often for scientific projects. There is an accelerating trend in Biology to make data and tools available via web interfaces. In my opinion this is an environment where PHP excels and yet all the literature I’ve seen discussing the development of these services uses Perl or occasionally Java.

Searching a little harder for PHP projects yields an equally depressing outlook. In PEAR Jesus Castagnetto released the Science_Chemistry and Math_Stats packages back in 2003. For my purposes though the Chemistry package is a little too ‘chemical’ and the stats package is a little too basic. In sourceforge there is a package named BioPHP which looks promising but again there has been no activity since 2003. A lot has happened since then.

Biology is increasingly data generative. There is going to be a steadily increasing need for tools to analyse all this data. These are likely to be centralised and made available via web interfaces.

Anyone out there?

I suspect I’m going to be increasingly creating automated solutions to remove some of the repetition involved in processing the, relatively, small amounts of data that I generate. A PHP toolkit able to leverage the latest online databases and perform ‘advanced’ statistics would be immensely valuable.

So my question is this. Is anyone out there using PHP in a scientific environment? Are there resources available which I’ve missed?

Display an Alert Message on Web Page Load

Monday, July 14th, 2008

If you would like an alert box to display when your page loads, place the following code within the HTML of your web page between your <HEAD> and </HEAD> tags.

<script language=”Javascript”>
<!–
alert (”Alert Message”)
//–>
</script>

Change the text indicated in red to the message you would like to display.

Although you can use the above HTML code to display an alert box when your web page loads, please use it cautiously, as you don’t want to irritate your visitors.

Php:Connection Handling

Tuesday, July 1st, 2008

PHP maintains a connection status bitfield with 3 bits:
0 - NORMAL

1 - ABORTED

2 - TIMEOUT
By default a PHP script is terminated when the connection to the client is broken and the ABORTED
bit is turned on. This can be changed using the ignore_user_abort() function. The TIMEOUT bit is
set when the script timelimit is exceed. This timelimit can be set using set_time_limit().

<?php
set_time_limit(0);
ignore_user_abort(true);
/* code which will always run to completion */
?>
You can call connection_status() to check on the status of a connection.

<?php
ignore_user_abort(true);
echo “some output”;
if(connection_status()==0) {
// Code that only runs when the connection is still alive
} else {
// Code that only runs on an abort
}
?>
You can also register a function which will be called at the end of the script no matter how the script
was terminated.

<?php
function foo() {
if(connection_status() & 1)
error_log(”Connection Aborted”,0);
if(connection_status() & 2)
error_log(”Connection Timed Out”,0);
if(!connection_status())
error_log(”Normal Exit”,0);
}
register_shutdown_function(’foo’);
?>

Php:Adding an extension

Monday, June 30th, 2008

Problem

You need PHP’s built-in ftp functions for the ultra-cool script you are writing, but your service
provider does not have PHP compiled with the –enable-ftp option.

Solution

If you have a shell account on a system with the same operating system as your web server, grab the
PHP source tarball and build using:

–with-apxs –enable-ftp=shared
You can check which flags your provider used by putting a phpinfo() call in a script on your server.

<?phpinfo()?>
Once compiled, you will find a “modules/ftp.so” file which you can copy to your web server and
enable either by putting:

extension=ftp.so
in your php.ini file or by adding this to the top of your script:

<?php dl(”ftp.so”) ?>

HTML:Highlight select html textarea contents

Monday, June 30th, 2008

<script type=”text/javascript”>
if (document.forms["form_id"] && document.forms["form_id"].data) {
document.forms["seofilter"].data.select();
}
</script>
this should be put after the form or near </body> because form may not be accessible.
In this example the textarea has name ‘data’.

Javascript:IE (Internet Explorer) red X or X-box picture placeholder workaround

Monday, June 30th, 2008

<div id=”problem_image1″>
<img border=”0″ id=”img1″ ONERROR=’problem_image_handler();’ src=” alt=” />
</div>
<script>
function problem_image_handler() {
document.getElementById(”problem_image1″).style.visibility = “hidden”;
}
</script>

PHP:Detect Firefox browser with php script/function

Monday, June 30th, 2008

function is_firefox() {
$agent = ”;
// old php user agent can be found here
if (!empty($HTTP_USER_AGENT))
$agent = $HTTP_USER_AGENT;
// newer versions of php do have useragent here.
if (empty($agent) && !empty($_SERVER["HTTP_USER_AGENT"]))
$agent = $_SERVER["HTTP_USER_AGENT"];
if (!empty($agent) && preg_match(”/firefox/si”, $agent))
return true;
return false;
}

PHP:Detect MySQL server version with simple php script on your web hosting

Monday, June 30th, 2008

<form>
<p>
This program lets to find out what mysql version your current hosting is running on.
You need to enter correct databaes parameters in order to get desired info.
</p>
<b>db host: <b/>
<input type=”text” name=”db_host” value=”localhost”>
<hr size=1 width=”25%” align=left>
<b>db username: <b/>
<input type=”text” name=”db_user” value=”">
<hr size=1 width=”25%” align=left>
<b>db password: <b/>
<input type=”text” name=”db_pass” value=”">
<hr size=1 width=”25%” align=left>
<hr size=1 width=”25%” align=left>
<input type=”submit” name=”submit” value=”submit”>
</form>
<?php
/*
1) enter db user/pass: host = localhost
2) view results
scenario:
1) show form
2) try to login
3) if OK -> show version
4) otherwise tell incorrect db user/pass combination
Freeware for comercial or non-commercial use.
no warranties!
*/
$submit = empty($_REQUEST['submit']) ? 0:1;
if ($submit) {
$db_host = empty($_REQUEST['db_host']) ? “localhost” : $_REQUEST['db_host'];
$db_user = empty($_REQUEST['db_user']) ? “” : $_REQUEST['db_user'];
$db_pass = empty($_REQUEST['db_pass']) ? “” : $_REQUEST['db_pass'];;
if (         empty($db_host)
|| empty($db_host)
|| empty($db_host) ) {
die(”<font color=’red’>Missing fields.</font>”);
}
$link = @mysql_connect($db_host, $db_user, $db_pass);
if (!$link)
die(”<font color=’red’>Can’t connect.
Please check your data.</font>”);
$qry = “SELECT VERSION()”;
$result = @mysql_query($qry);
if (!$result) {
die(”<font color=’red’>Query error: ” . mysql_error() . “</font>”);
}
$ver = @mysql_result($result, 0);
print “<font color=’green’>MySQL version: <b>$ver</b></font>”;
@mysql_free_result($result);
}
?>

Enable/Disable command line buffering of php shell script

Monday, June 30th, 2008

// enable command line buffering
php -doutput_buffering=1 console_app.php
// disable command line buffering
php -doutput_buffering=0 console_app.php
// console_app.php code
<?php
for ( $i=1; $i<=100;$i++)
{
print “$i]hin”;
sleep( 1 );
}
?>