php-general Digest 16 Mar 2009 07:14:11 -0000 Issue 6014
Topics (messages 290091 through 290105):
Re: Problem with passing and Area in a $_POST
290091 by: tedd
290094 by: Richard Kurth
290097 by: Jason Pruim
290098 by: Jan G.B.
Re: GeoIP?
290092 by: zerof
290096 by: Nitsan Bin-Nun
Re: left join does not work, why?
290093 by: PJ
290095 by: Jan G.B.
Which hashing algorithm is best to check file duplicity?
290099 by: Martin Zvarík
290100 by: Jan G.B.
290101 by: Paul M Foster
290102 by: Chris
290103 by: Martin Zvarík
Re: non-auto increment question
290104 by: Jim Lucas
Fork and zombies
290105 by: Waynn Lue
Administrivia:
To subscribe to the digest, e-mail:
[email protected]
To unsubscribe from the digest, e-mail:
[email protected]
To post to the list, e-mail:
[email protected]
----------------------------------------------------------------------
--- Begin Message ---
At 8:56 AM -0700 3/15/09, Richard Kurth wrote:
This is the form that is being sent as a post and the fieldorder[]
is the part that is not sending properly. But it has worked for
about a year in the script. It just stopped working. and I did not
change anything in this script
-snip- code
If it was my problem, I see the script is riddled with
inconsistencies. As such, I would pass it through validation to
discover all the errors. Then I would fix those errors and then see
if the problem continues.
Granted, some people don't think validation is important, but I'm
positive that I could get your script to work by simply cleaning up
the code.
Cheers,
tedd
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
--- End Message ---
--- Begin Message ---
tedd wrote:
At 8:56 AM -0700 3/15/09, Richard Kurth wrote:
This is the form that is being sent as a post and the fieldorder[] is
the part that is not sending properly. But it has worked for about a
year in the script. It just stopped working. and I did not change
anything in this script
-snip- code
If it was my problem, I see the script is riddled with
inconsistencies. As such, I would pass it through validation to
discover all the errors. Then I would fix those errors and then see if
the problem continues.
Granted, some people don't think validation is important, but I'm
positive that I could get your script to work by simply cleaning up
the code.
Cheers,
tedd
You are probly right. But I got this script from Rasmus Lerdor about a
year ago and I changed very little. It has worked just fine sence I
started using it. Who am I to question Rasmus's coding. I am now using
$_Request to capture the array for fieldorder[] and $_POST to get the
rest of the data.
--- End Message ---
--- Begin Message ---
Richard Kurth wrote:
tedd wrote:
At 8:56 AM -0700 3/15/09, Richard Kurth wrote:
This is the form that is being sent as a post and the fieldorder[]
is the part that is not sending properly. But it has worked for
about a year in the script. It just stopped working. and I did not
change anything in this script
-snip- code
If it was my problem, I see the script is riddled with
inconsistencies. As such, I would pass it through validation to
discover all the errors. Then I would fix those errors and then see
if the problem continues.
Granted, some people don't think validation is important, but I'm
positive that I could get your script to work by simply cleaning up
the code.
Cheers,
tedd
You are probly right. But I got this script from Rasmus Lerdor about a
year ago and I changed very little. It has worked just fine sence I
started using it. Who am I to question Rasmus's coding. I am now
using $_Request to capture the array for fieldorder[] and $_POST to
get the rest of the data.
Personally... I don't care who's code it is, if it was me, I'd still go
out and validate it... If someone typed something out real quick and
missed a period, it wouldn't be their fault :)
As has been said by some big higher up's in this very mailing list...
"Treat all code as pseudo code, don't copy and paste it without
understanding what it could do to your system" or something along those
lines... :)
--- End Message ---
--- Begin Message ---
2009/3/15 Jason Pruim <[email protected]>:
> Personally... I don't care who's code it is, if it was me, I'd still go
> out and validate it... If someone typed something out real quick and
> missed a period, it wouldn't be their fault :)
>
> As has been said by some big higher up's in this very mailing list... "Treat
> all code as pseudo code, don't copy and paste it without understanding what
> it could do to your system" or something along those lines... :)
I second that.
Maybe you should start with removing "</td></tr>" after the first
table has been opened, escape the html special chars used as values
(submit buttons) and escape all request data (e.g. $_POST) before
echoing.
well - it might not solve your problem (where's the textarea anyways),
but it wouldn't disturb the functionality. :)
--- End Message ---
--- Begin Message ---
Michelle Konzack escreveu:
Hello Nitsan,
Am 2009-03-15 15:39:26, schrieb Nitsan Bin-Nun:
Take a look at this:
http://www.maxmind.com/app/geolitecountry
Use indexes, I think the checkup won't take long.
...................
For a long time I'm using the geolitecountry without no problems.
The procedures that I use, can be seen in the tutorial located at:
http://www.educar.pro.br/i_en/GeoIP/
( An alternative procedure to the one proposed, is to use phpMyAdmin to
upload the CSV file to MySQL.)
--
zerof
http://www.educar.pro.br/
http://www.geoticks.com/
----------------------------------------------------------
Você deve, sempre, consultar uma segunda opinião!
----------------------------------------------------------
Deixe todos saberem se esta informação foi-lhe útil.
----------------------------------------------------------
You must hear, always, one second opinion! In all cases.
----------------------------------------------------------
Let the people know if this info was useful for you!
----------------------------------------------------------
--- End Message ---
--- Begin Message ---
Hi Michelle,
Try to use sqlite instead of any other database. It will be better I guess.
Regards,
Nitsan
On Sun, Mar 15, 2009 at 9:08 PM, zerof <[email protected]> wrote:
> Michelle Konzack escreveu:
>
>> Hello Nitsan,
>>
>> Am 2009-03-15 15:39:26, schrieb Nitsan Bin-Nun:
>>
>>> Take a look at this:
>>> http://www.maxmind.com/app/geolitecountry
>>>
>>> Use indexes, I think the checkup won't take long.
>>>
>>
>> ...................
> For a long time I'm using the geolitecountry without no problems.
> The procedures that I use, can be seen in the tutorial located at:
>
> http://www.educar.pro.br/i_en/GeoIP/
>
> ( An alternative procedure to the one proposed, is to use phpMyAdmin to
> upload the CSV file to MySQL.)
>
>
> --
> zerof
> http://www.educar.pro.br/
> http://www.geoticks.com/
> ----------------------------------------------------------
> Você deve, sempre, consultar uma segunda opinião!
> ----------------------------------------------------------
> Deixe todos saberem se esta informação foi-lhe útil.
> ----------------------------------------------------------
> You must hear, always, one second opinion! In all cases.
> ----------------------------------------------------------
> Let the people know if this info was useful for you!
> ----------------------------------------------------------
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--- End Message ---
--- Begin Message ---
Marc Christopher Hall wrote:
> Not to mention if this is written for v 5 then you need to wrap your joins
> in ()
>
> -----Original Message-----
> From: Jan G.B. [mailto:[email protected]]
> Sent: Sunday, March 15, 2009 9:47 AM
> To: PJ
> Cc: Daniel Brown; [email protected]
> Subject: Re: [PHP] left join does not work, why?
>
> 2009/3/15 PJ <[email protected]>:
>
>> Daniel Brown wrote:
>>
>>> On Sat, Mar 14, 2009 at 19:51, PJ <[email protected]> wrote:
>>>
>>>
>>>> $sql1 = "SELECT b.id, b.title, b.sub_title, b.descr, b.comment,
>>>> b.bk_cover, b.copyright, b.ISBN, b.language, b.sellers, c.publisher,
>>>>
> <---
>
>>>> CONCAT_WS(' ', first_name, last_name) AS Author
>>>> FROM book AS b
>>>> LEFT JOIN book_author AS ab ON b.id = ab.bookID
>>>> LEFT JOIN author AS a ON ab.authID=a.id
>>>> LEFT JOIN book_publisher as abc ON b.id = abc.bookID // <---
>>>> LEFT JOIN publishers AS c ON abc.publishers_id = c.id // <---
>>>> ORDER BY title ASC ";
>>>> $result1 = mysql_query($sql1, $db);
>>>> $bookCount = mysql_num_rows($result1);
>>>>
>> The error reported now is to check the syntax near the commented out
>>
> lines.
>
>> So, what is going on here? I understood that when the lines are
>> commented out they are not lprocessed.
>> When I deleted the commented out lines the errors went away.
>> This does not make sense and obviously could be very misleading in
>> debugging.
>>
>>
>
> *How* are you commenting out? Mysql accepts /* such comments */, so if
> you want to transform a line in your SQL query string into a comment,
> you gotta do it that way.
> so this is just wrong:
> $x = " select *
> // from foo
> from bar";
> and that query would be valid
> "SELECT this/*, that*/ from x"
>
>
Quite right...
This has been "mulling" (?) in my mind for a few hours now and I thought
I'd just see what response I would get on the list. O:-)
It took ma a bit of thinking, I must say, but the answer is simple:
You cannot do comment - out's (for that is what this was really about)
within astatement or declaration! I was commenting out two JOINs within
a $sql = "SELECT ...... " declaration that was split into several lines
and it didn't occur to me that you cannot just comment out a part of a
declaration or statement that spans several lines. So, if you comment
out one part the rest will still be parsed. There's a reason, then why
the HTML specification requires that " delimited statements cannot be
split over several lines. Hope I said all that correctly. :-)
--
unheralded genius: "A clean desk is the sign of a dull mind. "
-------------------------------------------------------------
Phil Jourdan --- [email protected]
http://www.ptahhotep.com
http://www.chiccantine.com/andypantry.php
--- End Message ---
--- Begin Message ---
2009/3/15 PJ <[email protected]>:
>> -----Original Message-----
>> From: Jan G.B. [mailto:[email protected]]
>> so this is just wrong:
>> $x = " select *
>> // from foo
>> from bar";
>> and that query would be valid
>> "SELECT this/*, that*/ from x"
>>
> It took ma a bit of thinking, I must say, but the answer is simple:
> You cannot do comment - out's (for that is what this was really about)
> within astatement or declaration! I was commenting out two JOINs within
> a $sql = "SELECT ...... " declaration that was split into several lines
> and it didn't occur to me that you cannot just comment out a part of a
> declaration or statement that spans several lines. So, if you comment
> out one part the rest will still be parsed.
well, actually you could write my example with as much "newlines"
(whitespace) as you want.
$x = "SELECT
/* JOIN x ON a=b
*/
this FROM
that";
... is valid and mysql will parse it correct as "select this from that".
> There's a reason, then why
> the HTML specification requires that " delimited statements cannot be
> split over several lines. Hope I said all that correctly. :-)
>
not really correct. the above sql statement is correct, because PHP
has no problem with newlines in a string encapsulated in ", just like
mysql.
but I think it's not a goog coding style to have newlines, tabs and
alike in a mysql-query; it messes up the logs.
I would do it that way:
$x = "foo"
// . " unused"
. " bar";
byebye
--- End Message ---
--- Begin Message ---
I want to store the file's hash to the database, so I can check next
time to see if that file was already uploaded (even if it was renamed).
What would be the best (= fastest + small chance of collision) algorithm
in this case?
Is crc32 a good choice?
Thank you in advance,
Martin
--- End Message ---
--- Begin Message ---
2009/3/15 Martin Zvarík <[email protected]>:
> I want to store the file's hash to the database, so I can check next time to
> see if that file was already uploaded (even if it was renamed).
>
> What would be the best (= fastest + small chance of collision) algorithm in
> this case?
>
> Is crc32 a good choice?
guess not.
maybe unhex(md5()) into a binary(16) field?
What I'm trying to say is, that crc32 is more likely to have
collisions as a better algorithm like sha1, md5, ..
and that the datatype for the db should be considered.
byebye
--- End Message ---
--- Begin Message ---
On Sun, Mar 15, 2009 at 10:25:11PM +0100, Martin Zvarík wrote:
> I want to store the file's hash to the database, so I can check next
> time to see if that file was already uploaded (even if it was renamed).
>
> What would be the best (= fastest + small chance of collision) algorithm
> in this case?
>
> Is crc32 a good choice?
>
> Thank you in advance,
> Martin
According to wikipedia, a CRC is not sufficient to detect intentional
alteration of a file/message, since it's relatively easy to design a
message/file which will have the same CRC. On the other hand, CRC, by
design will reliably detect subtle changes to a message/file.
Paul
--
Paul M. Foster
--- End Message ---
--- Begin Message ---
Martin Zvarík wrote:
I want to store the file's hash to the database, so I can check next
time to see if that file was already uploaded (even if it was renamed).
What would be the best (= fastest + small chance of collision) algorithm
in this case?
"Fastest" depends mostly on the size of the file, not the algorithm
used. A 2gig file will take a while using md5 as it will using sha1.
Using md5 will be slightly quicker than sha1 because generates a shorter
hash so the trade-off is up to you.
$ ls -lh file.gz
724M 2008-07-28 10:02 file.gz
$ time sha1sum file.gz
4ae7bd1e79088a3e3849e17c7be989d4a7c97450 file.gz
real 0m3.398s
user 0m3.056s
sys 0m0.336s
$ time md5sum file.gz
16cff7b95bcb5971daf1cabee6ca4edd file.gz
real 0m2.091s
user 0m1.744s
sys 0m0.328s
$ time sha1sum file.gz
4ae7bd1e79088a3e3849e17c7be989d4a7c97450 file.gz
real 0m3.332s
user 0m2.988s
sys 0m0.344s
$ time md5sum file.gz
16cff7b95bcb5971daf1cabee6ca4edd file.gz
real 0m2.136s
user 0m1.776s
sys 0m0.348s
--
Postgresql & php tutorials
http://www.designmagick.com/
--- End Message ---
--- Begin Message ---
"Fastest" depends mostly on the size of the file, not the algorithm
used. A 2gig file will take a while using md5 as it will using sha1.
Using md5 will be slightly quicker than sha1 because generates a
shorter hash so the trade-off is up to you.
$ ls -lh file.gz
724M 2008-07-28 10:02 file.gz
$ time sha1sum file.gz
4ae7bd1e79088a3e3849e17c7be989d4a7c97450 file.gz
real 0m3.398s
user 0m3.056s
sys 0m0.336s
$ time md5sum file.gz
16cff7b95bcb5971daf1cabee6ca4edd file.gz
real 0m2.091s
user 0m1.744s
sys 0m0.328s
$ time sha1sum file.gz
4ae7bd1e79088a3e3849e17c7be989d4a7c97450 file.gz
real 0m3.332s
user 0m2.988s
sys 0m0.344s
$ time md5sum file.gz
16cff7b95bcb5971daf1cabee6ca4edd file.gz
real 0m2.136s
user 0m1.776s
sys 0m0.348s
Aha, thanks for sharing the benchmark. I'll go with MD5()
--- End Message ---
--- Begin Message ---
PJ wrote:
haliphax wrote:
On Fri, Mar 13, 2009 at 1:24 PM, PJ <[email protected]> wrote:
Jim Lucas wrote:
PJ wrote:
am resending as this was erroneously ccd to mysql. sorry!
Ashley Sheridan wrote:
On Thu, 2009-02-26 at 11:27 -0500, PJ wrote:
Jerry Schwartz wrote:
Being rather new to all this, I understood from the MySql manual
that
the auto_increment is to b e used immediately after an
insertion not
intermittently. My application is for administrators (the site
owner &
designates) to update the database from and administration
directory,
accessed by user/password login... so there's really very little
possibility of 2 people accessing at the same time.
By using MAX + 1 I keep the id number in the $idIn and can reuse
it in
other INSERTS
[JS] Are you looking for something like LAST_INSERT_ID()? If you
INSERT a
record that has an auto-increment field, you can retrieve the value
that got
inserted with "SELECT LAST_INSERT_ID()". It is
connection-specific, so
you'll always have your "own" value. You can then save it to reuse,
either
as a session variable or (more easily) as a hidden field on your
form.
Thanks, Jerry,
You hit the nail on the head.:)
To refine my problem (and reduce my ignorance),here's what is
happening
on the form page:
There is a series of INSERTs. The first inserts all the columns of
"book" table except for the id, which I do not specify as it if
auto-insert.
In subsequent tables I have to reference the book.id (for
transitional
tables like "book_author"(refers authors to book) etc.
If I understand it correctly, I must retrieve ("SELECT
LAST_INSERT_ID()") after the first INSERT and before the following
insert; and save the id as a string ($id)...e.g. $sql = "SELECT
LAST_INSERT_ID() AS $id"
I need clarification on the "AS $id" - should this be simply id(does
this have to be turned into a value into $id or does $id contain the
value? And how do I retrieve it to use the returned value for the
next
$sql = "INSERT ... - in other words, is the id or $id available for
the
next directive or do I have to do something like $id = id?
I'm trying to figure this out with some trials but my insert does not
work from a php file - but it works from command-line... that's
another
post.
Here's how I mostly do it (albeit simplified):
$query = "INSERT INTO `sometable`(`title`,`content`)
VALUES('$title','$content')";
$result = mysql_query($query);
$autoId = mysql_insert_id($result);
$query = "INSERT INTO `another_table`(`link_id`,`value`)
VALUES($autoId,'$value');
$result = mysql_query($query);
No need to call another query to retrieve the last inserted id, as
it is
tied to the last query executed within this session.
Ash
www.ashleysheridan.co.uk
For some reason or other $autoId = mysql_insert_id($result); just does
not work for me... Yet some of the data is inserted correctly...
I did find that it does not work on tables that are empty... so you
can't start with an empty table. I entered data & it still did not
work.
I tried on another duplicate database... doesn't work.
I have checked & double checked the database, I have added checks
to see
what is returned and the returns are 0 or null - as I get different
responses for slightly different functions.
sessions is on
mysql is 5.1.28
php5
here's what is parsed:
else { $sql1 = "INSERT INTO book
          ( title, sub_title, descr, comment,
bk_cover,
copyright, ISBN, language, sellers )
        VALUES
          ('$titleIN', '$sub_titleIN', '$descrIN',
          '$commentIN', '$bk_coverIN',
'$copyrightIN',
'$ISBNIN', '$languageIN', '$sellersIN')";
    $result1 = mysql_query($sql1, $db);
  $autoid = mysql_insert_id($result1);
    $sql2 = "INSERT INTO author (first_name, last_name) VALUES
('$first_nameIN', '$last_nameIN')";
      $result2 = mysql_query($sql2, $db);
  $authorID = mysql_insert_id($result2);
    $sql2a = "INSERT INTO book_author (authID, bookID, ordinal)
VALUES ( '$authorID', '$autoid', '1')";
      $result2a = mysql_query($sql2a, $db);
    $sql2b = "INSERT INTO author (first_name, last_name) VALUES
('$first_name2IN', '$last_name2IN')";
      $result2b = mysql_query($sql2b, $db);
  $author2ID = mysql_insert_id($result2b);
    $sql2c = "INSERT INTO book_author (authID, bookID, ordinal)
VALUES ( '$author2ID', '$autoid', '2')";
      $result2c = mysql_query($sql2c, $db);
    $sql3 = "INSERT INTO publishers (publisher) VALUES
('$publisherIN')";
      $result3 = mysql_query($sql3, $db);
  $publisherID = mysql_insert_id($result3);
    $sql3a = "INSERT INTO book_publisher (bookID,
publishers_id)
VALUES ( '$autoid', '$publisherID' )";
      $result3a = mysql_query($sql3a, $db);
    foreach($_POST['categoriesIN'] as $category){
      $sql4 = "INSERT INTO book_categories (book_id,
categories_id)
        VALUES ($autoid, $category)";
      $result4 = mysql_query($sql4,$db);
      }
    echo $autoid; // shows: "blank"
    echo $authorID; // shows: "blank"
    echo $author2ID; // shows: "blank"
    echo $publisherID; // shows: "blank"
    echo "<br>autoid = $autoid<br>";// shows: autoid = "blank"
    echo "authorID = $authorID<br>";// shows: authorID =
"blank"
    echo "author2ID = $author2ID<br>";// shows: author2ID =
"blank"
    echo "publisherID = $publisherID<br>";// shows:
publisherID =
"blank"
here is what is inserted:
book is correctly inserted
author is correctly inserted
book_author is correctly inserted
book_categories is correctly inserted
book_publisher bookID = 0 publishers_id = 0 (not correct)
publishers is correctly inserted
You need to take a closer look at this page:
http://php.net/manual/en/function.mysql-insert-id.php
Under the parameters section, look at the description of the
link_identifier. Â It talks about the link_identifier being the
database connection handler, not the resulting resource that is
returned from a call to mysql_query().
Change the value you are passing to mysql_insert_id() to be the $db
variable instead of the $results variable and you will probably get
the results you are looking for.
Also, reading further tells me that since you did not pass
mysql_insert_id() a valid connection handler, you should have gotten a
E_WARNING notice. Â Did you by chance check your logs for this error?
Honestly, sounds like you need to turn on full error reporting to the
screen.
Place the following at the top of your script and you might see the
problem a little clearer.
error_reporting(E_ALL);
ini_set('display_errors', 1);
This will make it so that every error is reported to the screen and
not just logged.
Thanks much for your input. This is great. Now I can seeeeeee!
It would have solved a lot of headaches earlier in the process. :-[
I was able to catch several "Notice:"s and correct them.
Now things work pretty well...
Only problem now is to understand the use of subqueries...
and how to display several authors and categories for books.
I'm not trying to reinvent the wheel; just trying to make round and not
ssquare. :-) O:-)
This might get you started...
SELECT * FROM authors WHERE author_id IN (SELECT author_id FROM books
WHERE book_id = ?)
HTH,
It does, thanks.
But here's an interesting question -- in my original select sequence
with joins I have a CONCAT_WS(" ", first_name, last_name) as Author :
$sql1 = "SELECT b.id, b.title, b.sub_title, b.descr, b.comment,
b.bk_cover, b.copyright, b.ISBN, b.sellers, b.language, c.publisher,
CONCAT_WS(' ', first_name, last_name) AS Author
FROM book AS b
LEFT JOIN book_author AS ab ON b.id = ab.bookID
LEFT JOIN author AS a ON ab.authID=a.id
LEFT JOIN book_publisher as abc ON b.id = abc.bookID
LEFT JOIN publishers AS c ON abc.publishers_id = c.id
ORDER BY title ASC ";
$result1 = mysql_query($sql1, $db);
$bookCount = mysql_num_rows($result1);
for ($i=0; $i < $bookCount; $i++) {
$row = mysql_fetch_array($result1);
$bookid[$i] = $row["id"];
$booktitle[$i] = $row["title"];
$booksub_title[$i] = $row["sub_title"];
$bookdescr[$i] = $row["descr"];
$bookcomment[$i] = $row["comment"];
$bookbk_cover[$i] = $row["bk_cover"];
$bookcopyright[$i] = $row["copyright"];
$bookISBN[$i] = $row["ISBN"];
$booklanguage[$i] = $row["language"];
$booksellers[$i] = $row["sellers"];
$Author[$i] = $row["Author"];
$publisherspublisher[$i] = $row["publisher"];
}
Works fine; but it does not give me 2 authors when there are 2.
I wonder if it is possible to do a second
CONCAT_WS(' ', first_name, last_name) AS Author2 WHERE
(book_author.bookID = '$bookid' && ordinal = '2') --- I probably do not
have the right syntax, but you get the idea, I'm sure.
The whole point of my exercise is to try to keep things as simple as
possible. I am desperately trying to avoid more and more complicated
stuff. :-)
And I would think that it should be possible to work out some way of
integrating another table (book_categories) to retrieve an array of up
to, say 4 fields, and store them in a $string (with conditions to show
only that which exists) for echoing in HTML.
I just see a big problem because the code is parsing all books
......
for ($i=0;$i<$bookCount;$i++) {
if (empty($bookbk_cover[$i]))....
and unless there's a way to include querys within the "for...)
execution... actually, this is what I thought could be done. between
echos I would do a query to see if there is another Author, for example,
or categories and, if so, echo them at this point and then go on to the
next line of code, an echo... does that sound reasonalbe?
I'm probably trying to do something that is way over my head, if not
impossible, but hell, that's life. :-D and I'm having one hell-of-a time
(good, though) 8-)
Ok... Well, that code is something to behold...
Think of your SQL queries like this.
Each time you do a join in a SQL select, (if you don't involve a where clause) you are taking all the records from one
table and joining them with the other table. So in the above Select with the four JOIN()'s, you are creating this
monster of a table (in memory) that is finally handed back to PHP.
something to the effect of this.
Table A Record count * Table B Record count * Table C Record count *
Table D Record count * etc...
See how that could grow rather quickly?
What you should do is more like
Table A Record count + Table B Record count + Table C Record count +
Table D Record count + etc...
That seems a little more manageable.
<?php
$books = array();
$SQL = "SELECT * FROM book ORDER BY title ASC";
if ( ( $results = mysql_query($SQL, $db) ) !== false ) {
while ( $row = mysql_fetch_assoc($results) ) {
$books[$row['id']] = $row;
}
}
$book_author = array();
$SQL = "SELECT id, bookID, authID FROM book_author";
if ( ( $results = mysql_query($SQL, $db) ) !== false ) {
while ( $row = mysql_fetch_assoc($results) ) {
$book_author[$row['bookID']][] = $row['authID'];
}
}
$authors = array();
$SQL = "SELECT id, first_name, last_name FROM author";
if ( ( $results = mysql_query($SQL, $db) ) !== false ) {
while ( $row = mysql_fetch_assoc($results) ) {
$authors[$row['id']] = $row;
}
}
$book_publisher = array();
$SQL = "SELECT id, bookID, publisher_id FROM book_publisher";
if ( ( $results = mysql_query($SQL, $db) ) !== false ) {
while ( $row = mysql_fetch_assoc($results) ) {
$book_publisher[$row['bookID']][] = $row['publisher_id'];
}
}
$publishers = array();
$SQL = "SELECT id, publisher FROM publishers";
if ( ( $results = mysql_query($SQL, $db) ) !== false ) {
while ( $row = mysql_fetch_assoc($results) ) {
$publishers[$row['id']] = $row;
}
}
?>
Now you can do something like this:
<?php
if ( $books ) {
echo "<table>";
echo "<tr><th>Book Information</th><th>Book Authors</th><th>Book
Publishers</th></tr>";
foreach ( $books AS $bookID => $book ) {
echo "<tr>";
echo "<td>";
print_r($book, 1);
echo "</td>";
echo "<td>";
if ( isset( $book_author[$bookID] ) ) {
foreach ( $book_author[$bookID] AS $authorID ) {
if ( isset( $authors[$authorID] ) ) {
print_r($authors[$authorID], 1);
}
}
}
echo "</td>";
echo "<td>";
if ( isset( $book_publisher[$bookID] ) ) {
foreach ( $book_publisher[$bookID] AS $authorID ) {
if ( isset( $publishers[$authorID] ) ) {
print_r($publishers[$authorID], 1);
}
}
}
echo "</td>";
echo "</tr>";
}
echo "</table>";
}
--- End Message ---
--- Begin Message ---
I periodically run a script that makes a call against a remote API, which
takes some time to return. In an attempt to increase thoroughput, I decided
to investigate using pnctl_fork to spawn off multiple processes to make the
call, since the slowest part is the network part of it (and waiting for the
server response). I ended up writing a script that did this:
$pid = pnctl_fork();
if ($pid == -1) {
die('could not fork');
} else if ($pid) {
echo "parent $pid\n";
return;
} else {
// make API call
}
While this works, it unfortunately leaves behind a zombie process every
single time. I could rewrite this to instead call fork multiple times, then
wait on all of them to return, but as my system is currently architected,
the easiest way would be to fire and forget for this script. Does anyone
have any ideas on the best way to do it? The other way I've done this is to
use exec("php foo.php") and redirecting stdout and stderr to /dev/null.
Waynn
--- End Message ---