Home  |  Linux  | Mysql  | PHP  | XML
From:Nathan Wallace Date:Tue Sep 21 22:52:09 1999
Subject:PHP Knowledge Base Update -- September 21st, 1999


------------------------------------------------------------
How can I store a hierarchical structure in a database?
How are folder systems stored in a relational database?
http://www.e-gineer.com/e-gineer/phpkb/view.phtml/qid/648
------------------------------------------------------------
Nathan Wallace

This is one of the more interesting problems to solve using a relational
database...<g>

If you can get a hold of "SQL for Smarties" by Joe Celko it has a great
chapter on solving this problem.  The problem with the common solution
of just keeping a pointer to the parent is that you can end up needing
recursive queries.

The solution is once of those very simple but I would never have thought
of it approaches.  Basically you have to think of the folders as sets. 
So, Linux would be a big set that contained smaller sets like Red Hat
and Mandrake.  Each of those sets can also contain smaller sets and so
on down your hierarchy.

You then lay these sets out flat and give each one a left and right
index.

     3--RPM--4 5--GIMP-6

   2------RedHat---------7  8-----------Mandrake----------9

 1---------------------Linux--------------------------------10

Notice how the indexes for linux completely contain all the other
indices.

You can do really cool queries with this structure like show me all
parents for the folder called $id:

    select *
    from   folders as parents, folders as children
    where  parents.leftindex < children.leftindex
    and    parents.rightindex > children.rightindex
    and    children.id = $id

or show me all the leaves of the tree:

    select *
    from   folders
    where  leftindex = rightindex - 1;

and so on...

The inserts get pretty hairy though as you have to update all the
indices...

BTW, databases like Oracle have a heap of SQL extensions to let you do
hierarchical stuff much more easily...

Don't you wish you were using an object database...<g>


------------------------------------------------------------
What are the steps for adding Oracle support to PHP?
http://www.e-gineer.com/e-gineer/phpkb/view.phtml/qid/650
------------------------------------------------------------
Pedro Fradique da Silva

First, you must have at least the Oracle client installed on your 
machine.

Second, you must have ORACLE_HOME and ORACLE_SID set.

Third, you have to compile php with-oracle.

Then, create a test php file with only:

    <? phpinfo(); ?>

When you use that page, you must have reference to Oracle and Oci with 
the pathnames, etc.

That's it.


------------------------------------------------------------
Will parsing all html files as PHP slow down my server?
http://www.e-gineer.com/e-gineer/phpkb/view.phtml/qid/651
------------------------------------------------------------
Nathan Wallace

Yes because each html file will go through the PHP parser.  The speed 
drop won't really be noticable unless you get a *lot* of traffic though.


------------------------------------------------------------
What does MySQL Connection Failed: Protocol mismatch mean?
http://www.e-gineer.com/e-gineer/phpkb/view.phtml/qid/652
------------------------------------------------------------
Rasmus Lerdorf, Richard Lynch

The MySQL server you are connecting to is running a newer version of 
MySQL than the client library compiled into your Apache/PHP setup.  You 
need to recompile PHP against the client library that server is using.

This usually occurs when somebody upgrades MySQL, but doesn't recompile 
PHP with the new MySQL headers, or vice versa...

It's also very easy to have two copies of either package and thoroughly
confuse yourself as to which is really getting compiled into PHP, and 
which is actually running.

You're probably going to need to recompile Apache/MySQL/PHP.


------------------------------------------------------------
How can I create an email autoresponder using PHP?
http://www.e-gineer.com/e-gineer/phpkb/view.phtml/qid/653
------------------------------------------------------------
Nathan Wallace

You'd probably do best to use a program called procmail.

    http://www.procmail.org

Using procmail you can set up filters to run when mail is received. 
That filter might be the execution of a perl or php script (the mail is
just sent to stdin).

In fact, procmail can do autoresponding anyway, see:

    man 5 procmailex

for some examples.

You may well find that procmail is already installed on your system...


------------------------------------------------------------
What are the steps for compiling pdflib with PHP?
http://www.e-gineer.com/e-gineer/phpkb/view.phtml/qid/654
------------------------------------------------------------
Luca Perugini

this is my recipe:
 
 1) untar pdflib-2.01.tar.gz
     ./configure
     make
     make install

 2) set a symlink 
      ln -s /usr/local/lib/libpdf2.01.so  /usr/local/lib/libpdf.so
    You need to do this 'cause the PHP configure check for pdflib with
    a little C-program compiling it and linking with library named
    libpdf (!)

 2.bis)  ldconfig
    this update the info about dynamic library on you system 
    (think about the magic phrase  "Reboot  Windows "  )   ;-)  
    Linux it's COOL !!!!!  

 3) go to cvs.php.net and checkout the source tree 
     You can find How-To on http://cvs.php.net

 4) configure your php: 
      this is my configure script:
        ./configure --with-apxs=/usr/bin/apxs --with-ldap --with-mysql \
                        --with-pgsql=/usr --with-dbase \
                        --with-pdflib=/usr/local --with-zlib \
                        --with-jpeg-dir=/usr/lib \
                        --with-tiff-dir=/usr/lib \
                        --with-config-file-path=/etc/httpd \
                        --with-system-regex=yes \
                        --enable-debug=no
     make
 
     Stop your Apache server  with  apachectl stop (don't forget it!)

     make install

     apachectl start

     and 

      .......... have happy time hacking with pdflib/php !  ;->

Some about:
 - with pdflib 2.01 there's a lot of change on API
 - php-3.0.12 is patched for pdflib 2.0
 - some strange happens installing pdflib (name mismatch..)


** OTHER RANDOMLY SELECTED PHPKB ENTRIES **


------------------------------------------------------------
How do I run PHP from the command line or as a shell script?
http://www.e-gineer.com/e-gineer/phpkb/view.phtml/qid/12
------------------------------------------------------------

Install PHP in CGI mode.  You can of course have both (CGI and module)
on the same system.

When you get there, php -h gives you the command line parameters you may
find useful. And you can use the !# construction to cause the script to
load php.

The syntax is:

    #!/path/to/php
    <?php
        // your php code
    ?>

and if you want to stop it from sending the CGI-style headers, do:

    #!/path/to/php -q
    <?php
        // your php code
    ?>

On a win32 system, just cd to the dir that the script is in and type the
script name:

    C:\>myscript.php

Note, however, that if the script is a form that posts back to itself,
the only thing you will get is the form elements echoed to the console.
You will have to hardcode the form field values first if you want to see
the post in action.

Also, the .php extension should be registered to be executed with
php.exe


------------------------------------------------------------
Where can I use a hyphen (-) inside a [] set in a regular expression?
http://www.e-gineer.com/e-gineer/phpkb/view.phtml/qid/588
------------------------------------------------------------

Inside of [] in a regex, if you want a literal "-" it must be at the
beginning or end of the characters; otherwise, it specifies a range of
characters, as in a-z.


------------------------------------------------------------
Do I need to put CR and LF after HTTP protocol elements?
Do HEAD and GET HTTP requests require a CR and an LF?
http://www.e-gineer.com/e-gineer/phpkb/view.phtml/qid/292
------------------------------------------------------------

HTTP/1.1 defines the sequence CR LF as the end-of-line marker for all
   protocol elements except the entity-body (see appendix 19.3 for
   tolerant applications).

(19.3 recommends that applications recognize a single LF.)

So the answer should be "always use \r\n". Any HTTP/1.1 server that
doesn't accept it violates the standard.

(Essentially the same text appears in the HTTP/1.0 spec.)

So the following may not work as expected:

    fputs($sock,"HEAD / HTTP/1.0\n");

You should use \r\n instead:

    fputs($sock,"HEAD / HTTP/1.0\r\n");

If you do not use \r\n then the HTTP server may not recognise the
request and your script will wait for the request to the server to
timeout.  This may give the appearance of hanging if the HTTP server
does not handle timeouts properly.


------------------------------------------------------------
How can I sort results by a certain column?
http://www.e-gineer.com/e-gineer/phpkb/view.phtml/qid/260
------------------------------------------------------------

Let's say the column you want to sort by is "user_num".

    SELECT * 
    FROM   table_name
    WHERE  whatever='whathaveyou' 
    ORDER BY user_num ASC

This sorts the rows from smallest to highest, change the ASC to DESC and
you get from highest to lowest...


------------------------------------------------------------
How are strings constructed in PHP?
What quotes can I use to make strings in PHP?
Can I include a variable reference in a string?
Can I reference array variables inside a quoted string?
Why won't multi-dimensional array references work in a quoted string?
Why does '$foo' not print the value of the variable $foo?
http://www.e-gineer.com/e-gineer/phpkb/view.phtml/qid/267
------------------------------------------------------------

Basically a string in PHP is anything between matching quotes:

    'I am a string in single quotes'

    "I am a string in double quotes"

You can include quotes inside the string that do not match the quote
that started the string.  So you can include single quotes in a string
that was started (and finishes) with double quotes.  So, these will
work:

    "I am a 'single quote string' inside a double quote string"

    'I am a "double quote string" inside a double quote string'

PHP thinks it has reached the end of the string as soon as it sees that
matching quote.  So the example

    "Why doesn't "this" work?"

is actually seen by PHP as being

    "Why doesn't " - a string

    this           - crap letters that PHP doesn't recognize

    " work?"       - a string

It is also helpful to know that you can include a quote character of the
same type inside a string if you escape it.  The escape tells PHP that
the next character should just be used as part of the string.  For
example:

    "Why doesn't \"this\" work?"

is a single string that contains double quote characters.

Double quote strings and single quote strings are treated differently by
PHP.  Double quote strings are interpreted by PHP while single quotes
strings are treated exactly as is.  For example:

    $foo = 2;
    echo "foo is $foo";     // this prints:  foo is 2
    echo 'foo is $foo';     // this prints:  foo is $foo
    echo "foo is $foo\n";   // this prints:  foo is 2 (with a <CR>)
    echo 'foo is $foo\n';   // this prints:  foo is $foo\n

That's right, even backslashes aren't expanded inside single quotes,
either except for \\ and \'.

So, you should use double quotes when you need variable expansion inside
the string.  Otherwise, feel free to use single quotes.

Note however that PHP only supports simple constructs inside quoted
strings, so this will work:

    "$a[$i]"

But this won't:

    "$a[$i][$j]"

And as such it is usually advisable to simply pop out of the string when
you are doing anything more complex than a simple variable substitution.
ie.

    "some string ". $a[$i][$j] . " more text"

The last thing to know about strings is that you can join them together
easily using the concatenate operator (.).  Here is an example:

    $var = "crappy ending";
    echo "this " . 'is an' . ' example ' . "with a " . $variable;



Navigate in group php.kb at sever news.php.net
Previous Next




  
© No Copyright
You are free to use Anything
Site Maintained by PHP Developer
Powered By PHP Consultants