php-general Digest 27 Dec 2004 13:02:47 -0000 Issue 3193
Topics (messages 205187 through 205201):
Re: A serious bug? "or" operator gives out diffferent results depending on
order of operands
205187 by: Rory Browne
205191 by: Richard Lynch
Re: handling large files w/readfile
205188 by: Heilig \(Cece\) Szabolcs
205189 by: Raditha Dissanayake
Re: MP3s
205190 by: Richard Lynch
Re: How do I underline a string using php and fonts?
205192 by: Richard Lynch
Re: Firefox/IE (sometimes) wants to download .php files from my Apache2 server
205193 by: Richard Lynch
205200 by: Rinke Hoekstra
Re: Any bright solution for my problem?
205194 by: Richard Lynch
Re: negative numbers
205195 by: Richard Lynch
205201 by: Jason Wong
Re: Help with code
205196 by: Burhan Khalid
Re: [PHP-DB] what is wrong with my code.. please help
205197 by: Burhan Khalid
emailing files...
205198 by: Gregory Machin
205199 by: Jason Wong
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 ---
I think what Jose is trying to say, is that because 'or' has a lower
precidence than =, you are interpreting the expression wrong.
$b1 = is_null($x) or ($y > 5);
is the same as
($b1 = is_null($x)) or ($y > 5)
This assigns the value of is_null($x) to $b1, regardless of the result
of ($y > 5). It's shorthand for
if( !($b1 = is_null($x)) ){
($y > 5);
}
similarly
$b2 = ($y > 5) or is_null($x);
assigns $b2 to ($y > 5), or in this case true(since $y == 10). If ($y
> 5) returned false, ie if $i <= 5, then it would call the function
is_null($x). In this case calling the function is_null() is pointless,
since you aren't using the result.
What I think you are looking for is either
$b2 = (($y > 5) or is_null($x)); // whole thing right of = enclosed in brackets
or
$b2 = ($y > 5) || is_null($x); // '||' has higher precidence than 'or'
On Fri, 24 Dec 2004 16:47:42 -0300, Jose M.Herrera
<[EMAIL PROTECTED]> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Bogdan Ribic wrote:
> | Here's a little test script:
> |
> | --------------------
> | $x = 2;
> | $y = 10;
> |
> | $b1 = is_null($x) or ($y > 5);
> | $b2 = ($y > 5) or is_null($x);
>
> Yes, of course.
> Your code or example, is just like:
>
> ( $b1 = is_null($x) ) or ( $y > 5 ) ;
> ( $b2 = ($y > 5) ) or is_null($x) ;
>
> The ">" has more precedence than "=", "or" it has a very low precedence.
> Then, $b1 = false and $b2 = True... that's ok! :P
>
> You example, must have been:
>
> $b1 = ( ( is_null($x) ) or ( $y > 5 ) );
> $b2 = ( ( $y > 5 ) or ( is_null($x) ) );
>
> I this example the value of $b1 is all between the parenthesis (explicitly).
>
> Bye!
>
> - --
> Jose Miguel Herrera M. - User #246070 counter.li.org
> Est.Ing.Civil Informatica - UTFSM
> Valparaiso, Chile - http://www.inf.utfsm.cl/~jherrera
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.2.4 (GNU/Linux)
> Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org
>
> iD8DBQFBzHJdl/j2VHFHn8wRArZdAKCJbv8W54vlpeinK1hMF3xEttjuiACeIIUs
> 63OX2bn+h9zLUDHhSvSTr/M=
> =3vfi
> -----END PGP SIGNATURE-----
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--- End Message ---
--- Begin Message ---
Bogdan Ribic wrote:
> $b1 = is_null($x) or ($y > 5);
> $b2 = ($y > 5) or is_null($x);
To add to what has been posted already:
If 'or' isn't what you want '||' probably *IS* what you want.
Watch out though -- Sometimes even the precedence of '||' isn't as
high/low as I hoped, and if in doubt, use parentheses to force things to
happen in the order you want.
--
Like Music?
http://l-i-e.com/artists.htm
--- End Message ---
--- Begin Message ---
Hello!
> i'm working on a app which output files with readfile() and some headers..
> i read a comment in the manual that says if your outputting a file php
> will
> use the same amount of memory as the size of the file. so, if the file is
> 100MB php will use 100MB of memory.. is this true?
>
> if it is, how can i work around this?
Do not use readfile() or file_get_contents() to read
the full file in one step. Read file in smaller chunks
to a buffer variable whith fread() and output that
immediately in the cycle. And again and again...
You can read terrabytes of data and utput them, the
amount of memory needed is based on your buffer size.
--
=============================================================
Heilig (Cece) Szabolcs - [EMAIL PROTECTED] - http://www.phphost.hu
=============================================================
--- End Message ---
--- Begin Message ---
Sebastian wrote:
i'm working on a app which output files with readfile() and some headers..
i read a comment in the manual that says if your outputting a file php will
use the same amount of memory as the size of the file. so, if the file is
100MB php will use 100MB of memory.. is this true?
I did a comparision study of readfile() and related functions you will
see that results at http://www.raditha.com/wiki/Readfile_vs_include
What I discovered was that require,include() and file_get_contents()
would consume more memory than the file size. Not so with readfile or
fpasstru
if it is, how can i work around this?
go ahead and use readfile.
--
Raditha Dissanayake.
------------------------------------------------------------------
http://www.radinks.com/print/card-designer/ | Card Designer Applet
http://www.radinks.com/upload/ | Drag and Drop Upload
--- End Message ---
--- Begin Message ---
GH wrote:
> I am working on a PHP based website and would like to offer media to
> my visitors... I have the Audio in WMA and MP3 formats... I would like
> to know how I could get them to "Stream"? inline... using PHP.... any
> advice would be greatfully appreciated
Having done this (a lot) I'll add some notes to the fine answers already
provided.
#1. It's .m3u not .m3a :-)
#2. I have NO IDEA if putting WMA files in .m3u playlists works, and don't
care, because .wma files suck acoustically. :-) You're on your own for the
whole WMA thing, though most of what was said probably applies to the WMA
files and their corresponding streaming mechanism, because MS probably
just stole the ideas from MP3/M3U anyway.
#3. You can also put MULTIPLE .mp3 URLs in a single .m3u file to generate
a "playlist". Way cool.
#4. As always, some browsers (IE) suck worse than others with rich media,
and you will want to force your URL to end in .m3u in the link you provide
so they will do the right thing with your PHP-generated M3U playlist.
Something like http://example.com/playlist.php/bogus.m3u should do the
trick. I go even farther, and use .htaccess with ForceType to get Apache
to treat 'playlist.m3u' as a PHP file, so my URL looks like
http://example.com/playlist.m3u so there is NO CHANCE of allowing the
stupid browser to screw up.
#5. Your web server must be configured to know the correct mime-type of
.m3u files. In the old days, if you (or your host) had altered their
mime-type list in any way, the fresh install of Apache wouldn't update it,
and then they'd maybe have this really old icky list of mime-types which
did not include .m3u. Fortunately, you could use http://php.net/header to
force the Content-type to be audio/mpeg-url which most browsers "know" is
MP3 playlist.
#6. You can also include #EXTM3U entries with the playlist for some MP3
players to know the artist/album from which the music came. See URLs
below for example output.
#7. If you want to, you can also use the pretty cool PHP ID3 library to
dynamically pre-pend ID3v2 tags to your MP3s to include copyright, artist
info, and even thumbnail images to go with your MP3s. I've done that at
http://uncommonground.com with mixed results -- Most players ignore the
extra data, a few display it. At least none of them seem to choke on
it... Google for "ID3v2 PHP" and it should turn up.
If anybody has read this far and wants to give me feedback OFF-LIST on the
efficacy of the ID3v2 parts working or not in your player/platform, I'd
sure appreciate it:
http://uncommonground.com
has Hi-Fi and Lo-Fi for your listening pleasure.
--
Like Music?
http://l-i-e.com/artists.htm
--- End Message ---
--- Begin Message ---
Brent Clements wrote:
> I have always wondered this and maybe you guys can help.
>
> How do I underline a bit of text using True Type Fonts?
>
> Right now, I am creating imagelines but I don't think that's the most
> efficient way of doing it.
>
> Anybody know how to do "underlines" but using the font itself?
I believe the User Contributed Notes in the on-line manual include some
references to additional arguments in the various font/text drawing
functions that will do things of this nature.
I don't promise that underlining is there, or that it will work.
You're going to have to end up reading the documentation not only of PHP,
but also of the underlying TrueType library.
NOTE: It may be that TrueType won't do it, but FreeType or PostScript
will, and that switching is an option for you. Or not.
NOTE: If you are happy with the "look" and code readability you have, I
seriously doubt that imageline is that much slower than some 'underline'
feature of the TrueType library, unless you are calling imageline a *LOT*
of times... So you may want to focus your efforts elsewhere unless you're
just bored or the code is unreadable.
--
Like Music?
http://l-i-e.com/artists.htm
--- End Message ---
--- Begin Message ---
Rinke Hoekstra wrote:
> I have a rather odd problem. Since a few days, my otherwise perfect SuSe
> 9.0 Apache2 server started to do some funny stuff.
> In general, php works fine, and I can do anything I want... after a
> little while Firefox/IE starts asking me whether I want to download/run
> the php file instead of rendering it in the browser.
When this does happen, do you get your PHP source if you download it, or
does PHP actually get invoked, and you get the HTML output?
Try it several times, as there may be timing issues, and you may get
different results based on weird browser behaviour with respect to asking
for a URL *twice* when prompting for a download.
EG: browser asks for URL, triggers bug, asks you if you want to download,
asks for URL again but doesn't trigger bug and gets output you expect.
> Sometimes even this doesn't happen, and I just get a regular Apache2
> page stating 'Object not found' (with accompanying lines in my server's
> error_log). A few minutes later, everything works fine again.
> (restarting apache also tends to help)
Sounds like something is killing the PHP Module (or is it CGI?) in your
server...
> I think the biggest problem is that it is intermittent: sometimes it
> works, and sometimes it doesn't... no way for me to diagnose it.
> weird.
Try throwing Apache Benchmark (ab) at it, or some other load-inducing
software.
My hypotheses is that it's happening under load because...
Your OS has a limit on the number of processes that can run, and Apache is
going okay, but PHP as CGI is hitting that wall. If you are running PHP
as Module, and not CGI, this hypothesis is obviously bogus.
Some module in PHP is not thread-safe, and your PHP Module is dying out,
but Apache is managing to sort of stagger along... Seems unlikely,
however...
It's also possible that some third piece of software is somehow killing
PHP threads or something, but that would be tough to do...
You might also be hitting some other sort of limit and some bad coding.
For example, if your Apache/MySQL/PHP setup limits the number of MySQL
connections (see /etc/my.cnf) to *FEWER* than the number of Apache
children (see httpd.conf) and if you PHP code just sort of dies with no
output when MySQL can't be connected to (find your mysql_connect code in
PHP) then you could maybe get this kind of behaviour from some browsers
when they get nothing back from the web server because your code sent
nothing out. MySQL should have *more* connections available than you have
Apache children. Just a few more, so you can use the 'mysql' monitor
program even if every Apache child is using a MySQL connection. This is
most obvious when you use mysql_pconnect, since at that point, each Apache
child ends up with its own MySQL connection, for all intents and purposes.
You may also want to be more specific in your versions of Apache and PHP
if you re-post after more testing.
Hopefully you've got a Development Server you can test this all on...
--
Like Music?
http://l-i-e.com/artists.htm
--- End Message ---
--- Begin Message ---
Dear Richard,
Thank you for your most extensive an excellent suggestions! I will have
a look at it once my holidays are over... (or when I start itching too
badly)
Thanks again.
-Rinke
Richard Lynch wrote:
Rinke Hoekstra wrote:
I have a rather odd problem. Since a few days, my otherwise perfect SuSe
9.0 Apache2 server started to do some funny stuff.
In general, php works fine, and I can do anything I want... after a
little while Firefox/IE starts asking me whether I want to download/run
the php file instead of rendering it in the browser.
When this does happen, do you get your PHP source if you download it, or
does PHP actually get invoked, and you get the HTML output?
Try it several times, as there may be timing issues, and you may get
different results based on weird browser behaviour with respect to asking
for a URL *twice* when prompting for a download.
EG: browser asks for URL, triggers bug, asks you if you want to download,
asks for URL again but doesn't trigger bug and gets output you expect.
Sometimes even this doesn't happen, and I just get a regular Apache2
page stating 'Object not found' (with accompanying lines in my server's
error_log). A few minutes later, everything works fine again.
(restarting apache also tends to help)
Sounds like something is killing the PHP Module (or is it CGI?) in your
server...
I think the biggest problem is that it is intermittent: sometimes it
works, and sometimes it doesn't... no way for me to diagnose it.
weird.
Try throwing Apache Benchmark (ab) at it, or some other load-inducing
software.
My hypotheses is that it's happening under load because...
Your OS has a limit on the number of processes that can run, and Apache is
going okay, but PHP as CGI is hitting that wall. If you are running PHP
as Module, and not CGI, this hypothesis is obviously bogus.
Some module in PHP is not thread-safe, and your PHP Module is dying out,
but Apache is managing to sort of stagger along... Seems unlikely,
however...
It's also possible that some third piece of software is somehow killing
PHP threads or something, but that would be tough to do...
You might also be hitting some other sort of limit and some bad coding.
For example, if your Apache/MySQL/PHP setup limits the number of MySQL
connections (see /etc/my.cnf) to *FEWER* than the number of Apache
children (see httpd.conf) and if you PHP code just sort of dies with no
output when MySQL can't be connected to (find your mysql_connect code in
PHP) then you could maybe get this kind of behaviour from some browsers
when they get nothing back from the web server because your code sent
nothing out. MySQL should have *more* connections available than you have
Apache children. Just a few more, so you can use the 'mysql' monitor
program even if every Apache child is using a MySQL connection. This is
most obvious when you use mysql_pconnect, since at that point, each Apache
child ends up with its own MySQL connection, for all intents and purposes.
You may also want to be more specific in your versions of Apache and PHP
if you re-post after more testing.
Hopefully you've got a Development Server you can test this all on...
--
--------------------------------------
Rinke Hoekstra [EMAIL PROTECTED]
T: +31-20-5253499 F: +31-20-5253495
Leibniz Center for Law, Law Faculty
University of Amsterdam, PO Box 1030
1000 BA Amsterdam, The Netherlands
--------------------------------------
--- End Message ---
--- Begin Message ---
QT wrote:
> I have a database with thousands records. And every minutes I am selecting
> 50 rows from that database and send some data to another web url according
> sellected 50 rows.
>
> Whenever, my script finish job in one minute, I have no problem. Because
> after each transaction, I am uptading one of the rows field as a "uesd".
If you actually have MySQL transactions, you should be able to wrap the
entire process in a single transaction, and it should work -- assuming
MySQL transactions pass the whole ACID test... You'll have to read the
MySQL manual and understand ACID, which I only grok as I read it, not at
all times, or I'd just tell you what you need to know. Sorry.
> BUT! if my script can not finish sending data to another url due to low
> speed connection or such a problem. On the next minute, same script runs
> automaticlay and sellecting another 50 rows which are not marked by
> "used".
>
> In this case, some times these two running acripts are sellecting same
> rows
> due to, old one haven't marked as a "used" for sellected rows.
>
> Is there any way, to put flag or something like that, when sellecting rows
> with php and mysql?
Ah.
Given the problem you are facing, of a slow connection, you probably don't
want to tie up the whole table while running your script with a
transaction in the first place -- MySQL still does a table-level lock,
right?... Maybe not. Again, you'd have to read the MySQL docs carefully,
and be sure your version of MySQL matches, to see if they do row-level
locking.
Without row-level locking, your other scripts will wait for the slow
connection to release the whole table from its locked state.
That would probably be "bad" for most applications.
Here is what I would try if I were you:
//Possibly START a MySQL transaction HERE:
$query = "select table_id from your_table where not status = 'used' limit
50";
$selected = @mysql_query($query, $connection) or
trigger_error(@mysql_error($connection) . " $query", E_USER_ERROR);
$selected_ids = array();
while (list($id) = @mysql_fetch_row($selected)){
$selected_ids[] = $id;
}
$selected_ids_sql = implode(', ', $selected_ids);
$query = "update your_table set status = 'used' where table_id in
($selected_ids_sql)";
$used = @mysql_query($query, $connection) or
trigger_error(@mysql_error($connection) . " $query", E_USER_ERROR);
//END the transaction started above here.
//*NOW* go ahead and get the stuff you want from your selected records
//and send them off to that slow connection:
$query = "select fields, you, really, want from your_table where table_id
in ($selected_ids_sql) ";
$data = @mysql_query($query, $connection) or
trigger_error(@mysql_error($connection) . " $query", E_USER_ERROR);
while (list($fields, $you, $really, $want) = @mysql_fetch_row($data)){
//Send this row to the slow connection
}
By dealing with *ALL* the rows at once when you first SELECT them, and
then doing an UPDATE on *ALL* the rows at once right away, you can keep
the other scripts from getting confused about which records are 'used'.
If you wrap just that part of the code in a MySQL transaction, you make it
virtually 100% "safe" [*] for the other scripts.
Most importantly, by pulling the SELECT and UPDATE out, you can handle the
'used' attribute in a transaction *quickly* without bogging down your
application waiting for that slow connection or even just getting all the
data from MySQL to PHP. You really only care about the ID field (assuming
you have one) and the 'used' status in terms of speed and in terms of
tracking which records were 'sent' probably.
This all assumes that you don't care if the slow connection gets its
records *AFTER some other fast connection gets the next 50 records... If
that's not what your application needs, DON'T DO THIS. :-)
[*] NOTE:
The transaction is 100% safe, but if your slow connection dies, you've got
a bunch of records marked 'used' that were *NOT* really 'used' because the
script died after you marked them 'used'
Hopefully, that's a fairly rare occurence, right?
Since it is rare, if you can write your code to DETECT that the slow
connection died, and the records you tried to send didn't really get sent,
in that last loop, you can update a single record's 'used' setting to put
it back to 'unused' (or whatever you call it) if it fails to be sent.
So the basic algorithm is:
BEGIN TRANSACTION
SELECT 50 ids
UPDATE those 50 ids as 'used' in ONE query
END TRANSACTION
[the above should happen about as fast as PHP/MySQL can go]
SELECT actual data for selected 50 ids
send data to client
if a record fails to send, re-set status to not be 'used' so it can get
sent in a later batch.
The down-side is that faster connections will get records "out of
sequence" while the slower connection is still chugging away trying to
process the records that are "earlier" in your sequence. In most
applications of this nature, that's okay.
If that's not okay, this is entirely the wrong algorithm to use, and you
need to wrap the whole thing in a TRANSACTION and accept the fact that
your slow connection is going to lock up your entire database until it
finishes all 50 records... At which point you can try some combination of
things like:
Send less than 50 records, at least to connections that have been proven
to be "slow" in the past.
Buy more hardware or acceleration software to make up for the slowness
of some connections by making all the rest go faster, and hope it
averages out.
Build in a system of detecting slow connections and roll back your
transactions when something is too slow, and hope the next time it's
faster.
What you do among all these choices depends more on your application's
needs than on what's "fastest" or "best" really.
--
Like Music?
http://l-i-e.com/artists.htm
--- End Message ---
--- Begin Message ---
I've managed to delete the original post, so apologies in advance if I got
the Subject wrong and/or am missing the threading headers.
Plus, this post isn't even strictly necessary, and could be construed as
pedantic. Oh well.
To change -40 to 40, http://php.net/abs is probably the best answer from a
code maintenance point of view.
If, however, you need to do this in a very tight loop, and performance is
an issue, and if you are certain that the number *IS* negative, you could
also do:
$x = - $x;
which would be faster than the abs() function.
This performance difference would be moot in *MOST* cases, but could be
important to the original poster, I suppose.
If you want to mimic the behaviour of abs (allowing for positive numbers)
and performance was an issue, that:
$x = ($x < 0) ? - $x : $x;
is most likely faster than abs()
Don't do this just to be "faster" because "faster is better" though. Code
maintenance and abs() win over "faster" except in really dire straits on
this one.
YMMV. NAIAA. IANAL. TANSTAAFL.
--
Like Music?
http://l-i-e.com/artists.htm
--- End Message ---
--- Begin Message ---
On Monday 27 December 2004 12:40, Richard Lynch wrote:
> If you want to mimic the behaviour of abs (allowing for positive numbers)
> and performance was an issue, that:
> $x = ($x < 0) ? - $x : $x;
>
> is most likely faster than abs()
Having nothing better to do I decided to benchmark this:
ternary:
$doo = -20;
for ($i = 1; $i < 10000000; $i++) {
$dah = ($doo < 0) ? - $doo : $doo;
}
abs():
$doo = -20;
for ($i = 1; $i < 10000000; $i++) {
$dah = abs($doo);
}
It turns out that abs() is slightly faster - as you might have guessed,
otherwise I wouldn't be posting this ;-)
ternary = 14.67 secs
abs() = 14.10 secs
The moral of the story is: if speed is important to you always do your *own*
benchmarking.
--
Jason Wong -> Gremlins Associates -> www.gremlins.biz
Open Source Software Systems Integrators
* Web Design & Hosting * Internet & Intranet Applications Development *
------------------------------------------
Search the list archives before you post
http://marc.theaimsgroup.com/?l=php-general
------------------------------------------
/*
A lot of people I know believe in positive thinking, and so do I.
I believe everything positively stinks.
-- Lew Col
*/
--- End Message ---
--- Begin Message ---
karl james wrote:
I have since updated it.
And I am having issues with a function I suppose.
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result
resource in
/home/virtual/site38/fst/var/www/html/php/wrox_php/movie_details.php on line
119
http://www.theufl.com/php/wrox_php/movie_details.php
This error (obviously) means that there is a problem with your query,
which is why mysql_query() did not return a result resource.
http://www.php.net/mysql_error is your friend.
--- End Message ---
--- Begin Message ---
S Kumar wrote:
Dear group,
I am trying to query my db and print the results.
when i execute my script, i am not getting any
result
except a blank page. can any one please help me what
is going wrong with my script.
Script:
$conn = pg_connect("user = $user dbname = $dbname
host
= $host password = $pass");
if (!$conn)
{
echo('could not establish connection with
database <br>');
exit;
}
$sql = "select * from experiment;"
$sth = pg_exec($connect,$sql)
You are missing ; after the above two lines.
Please use a php syntax aware editor to avoid these problems.
--- End Message ---
--- Begin Message ---
Hi
I would like to email some log files as attachments, all the examples
use an html page with a file input box, how can i do it without the
file input box.
Many Thanks
--
Gregory Machin
[EMAIL PROTECTED]
[EMAIL PROTECTED]
www.linuxpro.co.za
Web Hosting Solutions
Scalable Linux Solutions
+27 72 524 8096
--- End Message ---
--- Begin Message ---
On Monday 27 December 2004 15:50, Gregory Machin wrote:
> I would like to email some log files as attachments, all the examples
> use an html page with a file input box, how can i do it without the
> file input box.
It sounds like the examples are a superset of your problem, ie that does what
you need and more, which means you just need to remove the "more" part, in
other words remove the part dealing with the file uploads, more specifically
just use the code which deals with reading and attaching the uploaded files,
obviously changing it so that it reads the files that you want to attach.
It also sounds like you haven't bothered to look very hard as
google > email attachments php
give you more than enough links to solve your problem.
--
Jason Wong -> Gremlins Associates -> www.gremlins.biz
Open Source Software Systems Integrators
* Web Design & Hosting * Internet & Intranet Applications Development *
------------------------------------------
Search the list archives before you post
http://marc.theaimsgroup.com/?l=php-general
------------------------------------------
/*
The linuX Files -- The Source is Out There.
-- Sent in by Craig S. Bell, [EMAIL PROTECTED]
*/
--- End Message ---