BBEdit #Shebang!

January 11, 2008

Now, after getting Zend Debugger to work, trying to run a .php file in BBEdit using #!/usr/bin/php results in the following error:

dyld: NSLinkModule() error
dyld: Symbol not found: _OnUpdateInt
Referenced from:
/Applications/Zend/ZendStudio-5.5.0/lib/php4/ZendDebugger.so
Expected in: flat namespace

This is caused by those new lines in /etc/php.ini (see previous post).

But Zend Debugger needs that configuration, so the fix is to put an empty php.ini into /usr/bin. This allows BBEdit to work around the problem.


Zend Debugger

January 6, 2008

ZendDebuggerLocal.so, an extension supplied with Zend Studio, doesn’t work in server mode. Checking the environment using phpinfo shows Zend Debugger v5.2.12 is disabled. Also, the file dummy.php is empty.

A different extension and dummy.php file is found online. ZendDebugger.so goes into Zend’s lib/php4 directory, and the new and different dummy.php goes into localhost’s root directory.

Next, insert the following data into /etc/php.ini, showing the interpreter where the extension can be found:

zend_extension=/Applications/Zend/ZendStudio-5.5.0/lib/php4/ZendDebugger.so
zend_debugger.allow_hosts=127.0.0.1/32
session.save_path=/Applications/Zend/ZendStudio-5.5.0/tmp

Also, because MySQL is installed on localhost and can’t be reached internally, change the debug mode to server:

Zend Studio > Preferences > Debug > Debug Mode: Server


Control Structure Syntax

December 29, 2007

<?php
if ($a > 0) {
echo
"a is positive";
}
elseif (
$a < 0) {
echo
"a is negitive";
}
else {
echo
"a is zero";
}
?>

<?php
if ($a > 0) : echo "a is positive";
elseif (
$a < 0) : echo "a is negitive";
else : echo
"a is zero";
endif;
?>


Show Fields

December 22, 2007

#!/usr/bin/php
<?php
mysql_connect(“localhost”, “root”, “phpxphp”);
$q1 = mysql_query(“show databases;”);
$n1 = mysql_num_rows($q1);
for ($i; $i < $n1; $i++) {
$r1 = mysql_result($q1, $i);
echo $r1.”\n”;$q2 = mysql_query(“show tables from $r1;”);
$n2 = mysql_num_rows($q2);
for ($j = 0; $j < $n2; $j++) {
$r2 = mysql_result($q2, $j);
echo “\t”.$r2.”\n”;$q3 = mysql_query(“show fields from $r2 from $r1;”);
$n3 = mysql_num_rows($q3);
for ($k = 0; $k < $n3; $k++) {
$r3 = mysql_result($q3, $k);
echo “\t\t”.$r3.”\n”;
}
}
echo “\n”;
}
mysql_close();
php?>


Show Tables

December 20, 2007

#!/usr/bin/php
<?
mysql_connect(“localhost”, “root”, “phpxphp”);
$sd = mysql_query(“show databases;”);
$mnrsd = mysql_num_rows($sd);
for ($i; $i < $mnrsd; $i++) {
$mr = mysql_result($sd, $i);
echo $mr.”\n”;
$st = mysql_query(“show tables from $mr;”);
$mnrst = mysql_num_rows($st);
for ($j = 0; $j < $mnrst; $j++) {
$mr = mysql_result($st, $j);
echo “\t”.$mr.”\n”;
}
echo “\n”;
}
mysql_close();
?>


socket

December 15, 2007

Simplify connecting by creating a directory containing a symbolic link in the /var/mysql directory, where PHP looks for it by default.  MySQL installs it by default in the /tmp directory:

shell> sudo mkdir /var/mysql
shell>sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock

Updating /etc/my.cnf or /etc/php.ini are two other solutions.


The Object

December 15, 2007

#!/usr/bin/php
<?
$theConnection = mysql_connect(“localhost:/tmp/mysql.sock”, “root”, “phpxphp”);
$theResource = mysql_query(“show databases;”);
while ($theObject = mysql_fetch_object($theResource)) {
echo “$theObject->Database\r”;
}
mysql_close();
?>


Show Databases

December 15, 2007

#!/usr/bin/php
<?
$theConnection = mysql_connect(“localhost:/tmp/mysql.sock”, “root”, “phpxphp”);
$theResource = mysql_query(“show databases;”);
$numRows = mysql_num_rows($theResource);
for ($i; $i < $numRows; $i++) {
echo mysql_result($theResource, $i).”\n”;
}
mysql_close();
?>


Key Value Usage

December 14, 2007

#!/usr/bin/php
<?
$theArray = getrusage();
foreach ($theArray as $key => $value) {
print(“$key\t$value\n”);
}
?>

or

<HTML>
<HEAD>
<TITLE>PHP – Key Value Usage</title>
</HEAD>
<BODY>
<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=5>
<?
$theArray = getrusage();
foreach ($theArray as $key => $value) {
print(“<TR><TD>$key</TD><TD>$value</TD></TR>”);
}
?>
</TABLE>
</BODY>
</HTML>


For Each Array Item.php

December 14, 2007

<html>
<head>
<title>PHP – For Each Array Item</title>
</head>
<body>
<?
$theArray = array(‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’);
foreach ($theArray as $theItem) {
echo $theItem.”<BR>”;
}
?>
</body>
</html>

When foreach first starts executing, the internal array pointer is automatically reset to the first element of the array; there’s no need to call reset() before a foreach loop.