PHP Knowledge Base Update -- July 2nd, 1999

[email protected] (Nathan Wallace) Fri, 2 Jul 1999 21:20:54 -0500
Newsgroups php.kb
Message-ID <[email protected]>
Thanks to all those people who have made contributions to the knowledge
base.  Keep them coming!

    http://www.e-gineer.com/phpkb

To help us get a feel for how the knowledge base is growing I have just
added a statistics page:

    http://www.e-gineer.com/e-gineer/phpkb/stats.phtml

Cheers,

Nathan


------------------------------------------------------------
Where can I get PHPLIB?
Does PHPLIB run on Windows NT?
http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/341
------------------------------------------------------------
Vivek Awasthi

PHPLIB can be found at:

    http://phplib.shonline.de

You can download the tar.gz version, it works on all PHP platforms. 

On Windows NT you'll probably need WinZip to install it.

Furthur instructions are well documented in it.


------------------------------------------------------------
My query succeeds, so why doesn't $db->f(field) in PHPLIB return
anything?
What is $db->next_record() used for in PHPLIB?
http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/343
------------------------------------------------------------
Youngbong Choe

In PHPLIB, next_record() gets the next row returned by the query.  It
basically just calls *_fetch_array().  If you do not call next_record()
the fields are not available for accessing using f() and thus will
return nothing.

    $q->next_record(); // use this method before $q->f()
    $id = $q->f("id");

You can get more information in the manual at:

    http://phplib.shonline.de


------------------------------------------------------------
When does Internet Explorer try to retrieve favicon.ico?
Why do I get favicon.ico errors in my logs?
http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/345
------------------------------------------------------------
Steven Champeon, Richard Davey

When an Internet Explorer user bookmarks your site their browser
automatically makes a request for favicon.ico.  If it exists then this
file is downloaded and placed as the icon next to your site in the users
bookmarks list.  IE4.x and 5.x have this "feature".

Note that the favicon.ico action is /not/ limited to the root directory
of a site - if the user bookmarks a page in a subdirectory, IE will try
to fetch the icon from that directory as well.  This may result in two
file not found errors in your log files.

If you want to reduce the number of errors or use a single favicon.ico
file for your whole site then you might like to use a redirect in
Apache:

RedirectMatch permanent .*/favicon\.ico$ http://foo.com/your/file.ico

This will sends all requests for favicon.ico to that location,
regardless of what subdirectory they are made in.

It should also be noted that Internet Explorer will download the file
regardless of size.  Don't name really large file favicon.ico!


------------------------------------------------------------
How can I fetch the field name and data for every row of a database
query result?
How can I get the column (field) name from a database query result row
in MySQL?
http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/347
------------------------------------------------------------
Fred Isler

To work with fields in MySQL you need to know about the following
functions:

    http://www.php.net/manual/function.mysql-num-fields.php3
    http://www.php.net/manual/function.mysql-field-name.php3

Here is an example that will print the field names and their values from
each row in a MySQL query result:

    <?php
        $dbh = mysql_connect("host", "foo1", "foo2");
        mysql_select_db("foo3");
        $query = "select * from foo";

        $result = mysql_query($query, $dbh);

        $fields = mysql_num_fields($result);

        while ($row=mysql_fetch_array($result)) {
                for ($i=0; $i<$fields; $i++)  {
                        $name=mysql_field_name($result, $i);
                        echo "$name = $row[$name]<BR>\n";
                }
                echo "<br><br>\n";
        }
    <?

Should give you output like this:

field1 = id1
field2 = blahblah
field3 = done

field1 = id2
field2 = blah
field3 = doneagain

(etc)


------------------------------------------------------------
Does MySQL support subselects?
Can I do a subquery in a MySQL query?
http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/349
------------------------------------------------------------
SANIsoft, Sander Pilon, Aaron Leon Kaplan

Subselects will not be available in MySQL until version 3.23.

To work around this you'll have to make multiple queries and create
temporary tables to simulate subselect behaviour, or you'd have to come
up with a 'flat' query that does the same.

The general workaround (for all DBs where there are no subqueries) is
temporary tables. First make a temp table for the subquery and then
select from that.

Below is an example that shows the original subselect query and the work
around.

Here is the query with subselect:

    SELECT bizname, contactphone, city, website, 
           logo, description, discountid
    FROM business, discount
    WHERE business.bizid=discount.bizid AND discount.bizid
    IN (SELECT bizid FROM business WHERE catid=$categoryID)
    ORDER BY bizname

Here is the work around:

    $subresult = mysql_query("SELECT bizid
                              FROM business WHERE catid=$categoryID");

    $num =mysql_Num_Rows($subresult);

    $i=0;

    $subcriteria="";

    while ($i < m)  { 
        $subcriteria .= mysql_result($subresult,$i,"bizid");
        $subcriteria .=",";
        $i++; 
    }      

    $subcriteria=substr($subcriteria,0,-1); / / Remove last comma
  
    $result = mysql_Query("
         SELECT bizname, contactphone, city, website,                   
                logo, description, discountid
         FROM   business, discount
         WHERE  business.bizid=discount.bizid AND discount.bizid 
         IN     ($subcriteria)
         ORDER BY bizname");


------------------------------------------------------------
How can I get today's date and time in PHP?
How can I determine the current time in PHP?
http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/351
------------------------------------------------------------
Sander Pilon

Check the Date() and MkTime() functions. 

    http://www.php.net/manual/function.date.php3

    http://www.php.net/manual/function.mktime.php3

Leave the second argument in the Date() call empty to use today's date
and time.

mktime() returns the current time if you give no arguements at all.

See the manual chapter 'Date and time functions' for more info:

    http://www.php.net/manual/ref.datetime.php3


------------------------------------------------------------
How can I use a tab character as the delimiter in split() or explode()?
How can I include a tab character in a string?
http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/355
------------------------------------------------------------
Chuck Hagenbuch

The tab character is available using the \t sequence.  Remember that PHP
will not interpret strings that are in single quotes so you must use
double quotes for any string that contains a tab character.

Here is an example of the use of tab as a delimiter for explode():

    $foo = explode("\t", $array);


------------------------------------------------------------
How can I get the error message for a failed database operation?
http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/357
------------------------------------------------------------
Chad Cunningham

You can use the *_error() function to get the error message as reported
by the database.

You can use this directly in calls to die() to report the error easily. 
For example:

    mysql_select_db($dbname) or die(mysql_error())

For more information see the *_error() message for your database.

    http://www.php.net/manual/function.mysql-error.php3


------------------------------------------------------------
Can I use PHP to interface with an AS/400 database?
http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/358
------------------------------------------------------------
Rasmus Lerdorf

AS/400 uses the DB2 database.

You should be able to interface PHP to AS/400 using the DB2 ODBC
driver.