php-general Digest 31 Mar 2012 11:36:52 -0000 Issue 7754
php-general Digest 31 Mar 2012 11:36:52 - Issue 7754 Topics (messages 317381 through 317395): Re: Node.PHP 317381 by: German Geek 317382 by: Michael Save Could apc_fetch return a pointer to data in shared memory ? 317383 by: Simon 317394 by: Stuart Dallas Re: securing a script that exec()s 317384 by: tamouse mailing lists 317385 by: rene7705 317390 by: tamouse mailing lists whats wrong 317386 by: saeed ahmed 317388 by: Tommy Pham 317391 by: Duken Marga 317393 by: tamouse mailing lists 317395 by: Maciek Sokolewicz some good deals on good books 317387 by: tamouse mailing lists 317389 by: Tommy Pham Re: saeed ahmed 317392 by: Karl-Arne Gjersøyen Administrivia: To subscribe to the digest, e-mail: php-general-digest-subscr...@lists.php.net To unsubscribe from the digest, e-mail: php-general-digest-unsubscr...@lists.php.net To post to the list, e-mail: php-gene...@lists.php.net -- ---BeginMessage--- Maybe stupid question, but is node.php really necessary? If you can program PHP and it performs better than node.js, why would you need to have another wrapper around things. Why not just program normal PHP? twitter: geekdenz Blog: http://www.thheuer.com On Sat, Mar 31, 2012 at 10:39 AM, Hiyarli Baba root...@gmail.com wrote: As like Micheal's said said just keep up alive the project I was preferes node.js to pho only when i needed send millions of ssl api requests. nodejs sends 1k https request in onky 2 second including parsing required elements from database , check the returned source write to file if you want develope / clone more modules for that please start from http|s.req :p and let me coninue at php http://stackoverflow.com/a/9199961 my nodejs + php thing 2012/3/22, Michael Save savetheinter...@omegasdg.com: Very nice! I'll have a proper look at this in the morning, and I'll try it out for myself. Looking forward to seeing more development on this. Michael On Thu, Mar 22, 2012 at 11:40 AM, Joseph Moniz joseph.mo...@gmail.com wrote: Hey, So i had my first Hackathon at work last week and my project was to prototype making a node.js clone using PHP instead of V8. So i snatched up libuv and joyent's HTTP parser and set off on a 24 hour coding spree to get something workable. By the time the sun was coming out the next morning the following code was working. ?php $http = new node_http(); $http-listen(8080, function($request, $response) { $response-end(yay, super awesome response); }); nodephp_run(); ? The C code that powers it was whipped together really fast and is kind of hackish as a result. The code has some memory leaks that i haven't had time to fully track down yet. Some small portions of the code were borrowed from the phode project. In a naive benchmark on this simple server VS an equally simple server in node.js this implementation already out performs node.js in throughput by being able to serve just under 200% the amount of requests per second that node.js could. Take that with a grain of salt though because node.js has much more feature and is much more hardend from production use. I do believe the PHP binary will have some major performance gains over V8 as crossing the PHP -- C barrier seems to be a much lighter operation then crossing the V8 -- C++ barrier. Any help or feedback will be greatly appreciated. The projects source code can be found here: https://github.com/JosephMoniz/node.php - Joseph Moniz -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php ---End Message--- ---BeginMessage--- Because normal PHP is not asynchronous. Also, I kind of doubt you can outperform node.js with standard PHP. On Sat, Mar 31, 2012 at 11:56 AM, German Geek geek...@gmail.com wrote: Maybe stupid question, but is node.php really necessary? If you can program PHP and it performs better than node.js, why would you need to have another wrapper around things. Why not just program normal PHP? twitter: geekdenz Blog: http://www.thheuer.com On Sat, Mar 31, 2012 at 10:39 AM, Hiyarli Baba root...@gmail.com wrote: As like Micheal's said said just keep up alive the project I was preferes node.js to pho only when i needed send millions of ssl api requests. nodejs sends 1k https request in onky 2 second including parsing required elements from database , check the returned source write to file if you want develope / clone more modules for that please start from http|s.req
Re: [PHP] securing a script that exec()s
On Fri, Mar 30, 2012 at 7:05 AM, David OBrien dgobr...@gmail.com wrote: Find a way to do it using PHP's imagemagick extensions http://php.net/manual/en/book.imagick.php On Fri, Mar 30, 2012 at 5:56 AM, rene7705 rene7...@gmail.com wrote: Hi. I have a script that uses imagemagick's convert command on the commandline to get it's work done. These calls to exec('convert [params]') take params from the end-user via a html form, so is very unsecure. The intention is that the end-user only runs this script on localhost, from localhost. So now i'm checking $_SERVER['REMOTE_ADDR']===$_SERVER['SERVER_ADDR'] to see if I can allow the script to be used. But unfortunately, $_SERVER['REMOTE_ADDR'] is my external IP, and $_SERVER['SERVER_ADDR'] is my internal IP. How would I best fix this? I, too, would suggest you use the PHP extensions rather than shell out a command for various reasons, security being possibly the highest. There is also the cost of another process on the box, and doing the translation in and out. And David, please bottom post responses. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] whats wrong
On Fri, Mar 30, 2012 at 11:45 PM, saeed ahmed mycomputerbo...@gmail.com wrote: i have made a php script with a tutorial helpi dont know where is a error.please have a look Your code below assumes that everything is perfect in your world. IE: no network connectivity issue, all firewall related are properly configured, the user account actually exists and have the right permissions, the database exists and so are the table(s), etc I suggest you revisit the official PHP manual and read about each function you're using. ?php //connect and select a database mysql_connect(localhost,root, ); for starters, http://php.net/function.mysql-connect Noticed the result returned. You didn't bother to check whether if it's successful or not. If not why not? Since the DB is on the same box, is the firewall, if any, configured properly? If there's no firewall or if it's configured properly, is the account valid and have the right permissions? Is the MySQL server running on the default port 3306? If you don't know how to configure each of the above mentioned, allow me to introduce you a new friend, Google ;) Both of those topics are beyond the scope of this list. HTH, Tommy mysql_select_db(addressbook); //Run a query $result=mysql_query(select(SELECT*FROM COLLEAGUE); ? !DOCTYPE html PUBLIC-//w3c//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd; html xmlns=http://www.w3.org/1999/xhtml; head titleAddress Book/title meta http-equiv=content-type content=text/html;charset=utf-8 /head body h1Address Bookh1 table Border=1 cellpadding=2cellspacing=3 summary=table holda colleague contacts information tr th ID/th thFirst Name/th thLast Name/th thTelephone/th thEmail Address/th /tr ?php //LOOP through all table rows while ($row=mysql_fetch_array($result)) { echotr; echotd.$row['id'] . /td; echo td . $row['firstName'] . /td; echo td . $row['lastName'] . /td; echo td . $row['telephone'] . /td; echo td . Srow['email'] . /td; echo/tr; } //free result memory and close database connection mysql_free_result($result); mysql_close(); ? /table /body /html thanks -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] some good deals on good books
On Sat, Mar 31, 2012 at 12:02 AM, tamouse mailing lists tamouse.li...@gmail.com wrote: ( I apologize if this offends anyone's sensibilities. I am not in the employ of O'Reilly, nor is this going to make me any scratch. I just think this is a good chance to pick up some pretty useful books. ) Makes a lot of sense to me for someone kind enough to share a great offer to fellow coders/hackers :) Thanks. I'll pass this along to the folks newly interested in PHP. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] securing a script that exec()s
On Sat, Mar 31, 2012 at 1:37 AM, rene7705 rene7...@gmail.com wrote: escapeshellcmd() seems simplest. It might be if all you care about are shell meta characters, and admittedly it will save you from someone entering rm -rf / in your input field. But dealing with generic user input, even escaped, can still be problematic. Say you want to let the user set the size of the output file, and the user enters a bunch of letters instead of a geometry. Do you really want to have to deal with all the possible ramifications of such GIGO stuff? Better to vet the data, untaint it, and deal with it that way. When you've gone to all that, you're almost all the way to where you need to be to use the library functions. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] whats wrong
Please check this line: $result=mysql_query(select(SELECT*FROM COLLEAGUE); I think it should be: $result=mysql_query(SELECT * FROM COLLEAGUE); On Sat, Mar 31, 2012 at 1:45 PM, saeed ahmed mycomputerbo...@gmail.comwrote: i have made a php script with a tutorial helpi dont know where is a error.please have a look ?php //connect and select a database mysql_connect(localhost,root, ); mysql_select_db(addressbook); //Run a query $result=mysql_query(select(SELECT*FROM COLLEAGUE); ? !DOCTYPE html PUBLIC-//w3c//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd; html xmlns=http://www.w3.org/1999/xhtml; head titleAddress Book/title meta http-equiv=content-type content=text/html;charset=utf-8 /head body h1Address Bookh1 table Border=1 cellpadding=2cellspacing=3 summary=table holda colleague contacts information tr th ID/th thFirst Name/th thLast Name/th thTelephone/th thEmail Address/th /tr ?php //LOOP through all table rows while ($row=mysql_fetch_array($result)) { echotr; echotd.$row['id'] . /td; echo td . $row['firstName'] . /td; echo td . $row['lastName'] . /td; echo td . $row['telephone'] . /td; echo td . Srow['email'] . /td; echo/tr; } //free result memory and close database connection mysql_free_result($result); mysql_close(); ? /table /body /html thanks -- Duken Marga
[PHP] Att: saeed ahmed
Try this: mysql CREATE DATABASE addressbook; Query OK, 1 row affected (0.00 sec) mysql USE addressbook; Database changed mysql CREATE TABLE userData(id INT(4) NOT NULL AUTO_INCREMENT PRIMARY KEY, firstName VARCHAR(50) NOT NULL, lastName VARCHAR(50) NOT NULL, telephone INT(12) NOT NULL, email VARCHAR(100) NOT NULL); Query OK, 0 rows affected (0.11 sec) Now when you have the databse created and a table to store data, you need to write a PHP page that do the trick. Here is a simpe sample. Read all comments and look at the source code... !DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.1//EN http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd; html xmlns=http://www.w3.org/1999/xhtml; xml:lang=en head meta http-equiv=content-type content=application/xhtml+xml; charset=utf-8 / titleAddressbook/title meta name=generator content=BBEdit 10.1 / /head body ?php if(!isset($_POST['updateDB'])){ ? form action=addressbook.php method=post fieldset legendContact info:/legend p label for=firstNameFirst Name:/labelbr / input type=text id=firstName name=firstName / /p p label for=lastNameLast Name:/labelbr / input type=text id=lastName name=lastName / /p p label for=telephoneTelephone:/labelbr / input type=text id=telephone name=telephone / /p p label for=emailAddressEmail Address:/labelbr / input type=text id=emailAddress name=emailAddress / /p p input type=submit name=updateDB value=Update DB / /p /fieldset /form ?php } else { // Formfields $firstName = $_POST['firstName']; $lastName = $_POST['lastName']; $telephone = $_POST['telephone']; $email = $_POST['emailAddress']; // Delete all kind of tags that may be found in form fields $firstName = strip_tags($firstName); $lastName = strip_tags($lastName); $telephone = strip_tags($telephone); $email = strip_tags($email); // Just a simple way to a little more secure query $firstName = mysql_real_escape_string($firstName); $lastName = mysql_real_escape_string($lastName); $telephone = mysql_real_escape_string($telephone); $email = mysql_real_escape_string($email); // Connect to the Database $connectDB = mysql_connect(localhost,root,br0ok2meg) or die(mysql_error()); // Select Database $selectDB = mysql_select_db(addressbook) or die(mysql_error()); // Now create the query that insert data into table userData $sql = INSERT INTO userData(firstName,lastName,telephone,email)VALUES(\$firstName\,\$lastName\,\$telephone\,\$email\); // Run the query that actually insert data into the table userData mysql_query($sql,$connectDB) or die(mysql_error()); echo pThe Contact information is registred/p; } ? pa href=addressbook.php?viewView Contact Info/a/p ?php if(isset($_GET['view'])){ // Connect to the Database $connectDB = mysql_connect(localhost,root,br0ok2meg) or die(mysql_error()); // Select Database $selectDB = mysql_select_db(addressbook) or die(mysql_error()); // Ask for information in table userData $sql = SELECT * FROM userData; $result = mysql_query($sql,$connectDB) or die(mysql_error()); // Handle received data while($row = mysql_fetch_array($result)){ $firstName = $row['firstName']; $lastName = $row['lastName']; $telephone = $row['telephone']; $email = $row['email']; // Print the result to screen echo pstrongFirst Name:/strong $firstNamebr /; echo strongLast Name:/strong $lastNamebr /; echo strongTelephone:/strong $telephonebr /; echo strongEmail:/strong $emailbr /; echo /p; } } ? /body /html Hope this can be in help for you. Karl -- Hjemmeside: http://www.karl-arne.name/ Den 08:45 31. mars 2012 skrev saeed ahmed mycomputerbo...@gmail.com følgende: i have made a php script with a tutorial helpi dont know where is a error.please have a look ?php //connect and select a database mysql_connect(localhost,root, ); mysql_select_db(addressbook); //Run a query $result=mysql_query(select(SELECT*FROM COLLEAGUE); ? !DOCTYPE html PUBLIC-//w3c//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd; html xmlns=http://www.w3.org/1999/xhtml; head titleAddress Book/title meta http-equiv=content-type content=text/html;charset=utf-8 /head body h1Address Bookh1 table Border=1 cellpadding=2cellspacing=3 summary=table holda colleague contacts information tr th ID/th thFirst Name/th thLast Name/th thTelephone/th thEmail Address/th /tr ?php //LOOP through all table rows while ($row=mysql_fetch_array($result)) { echotr; echotd.$row['id'] . /td; echo td . $row['firstName'] . /td; echo td .
Re: [PHP] whats wrong
On Sat, Mar 31, 2012 at 1:45 AM, saeed ahmed mycomputerbo...@gmail.com wrote: i have made a php script with a tutorial helpi dont know where is a error.please have a look Before you actually run code in a browser, check your syntax with php -l (for lint, the old command to check C syntax on unixy systems) on a command line. This will improve your life immeasurably since you won't have to chase down where the server logs errors just to see if your code was syntactically correct. miishka:Sites tamara$ php -l test.saeed.php PHP Parse error: syntax error, unexpected T_STRING in test.saeed.php on line 6 Errors parsing test.saeed.php miishka:Sites tamara$ Which points out the syntax error on line 6 where you have: $result=mysql_query(select(SELECT*FROM COLLEAGUE); Which as an improperly formed select sql, and an unclosed string (thus sucking up part of your php code and html document. To fix this, it should be: $result=mysql_query($db, SELECT * FROM COLLEAGUE); i.e., the sql statement is in a single string. Also, spacing around the column list * may or may not be necessary, but it's a lot easier to read if it's there. Also, you need to tell the query procedure what data base you're using the $db variable (see below for this). Ok, that fixed, run php -l on the file again, and we get: PHP Parse error: syntax error, unexpected '' in test.saeed.php on line 31 Errors parsing test.saeed.php Looking at line 31: echo td . $row['firstName'] . /td; it looks fine, you might be wondering why it's complaining about that line. The key is to look up from there for possible syntax problems, and on line 30, there is a problem in that you didn't close the last string: echotd.$row['id'] . /td; should be: echotd.$row['id'] . /td; Ok, fix that, run php -l again: PHP Parse error: syntax error, unexpected '[', expecting ',' or ';' in test.saeed.php on line 34 Errors parsing test.saeed.php So, look at line 34 now: echo td . Srow['email'] . /td; The problem is that you typoed the dollar sign in front of row. It should be: echo td . $row['email'] . /td; Also, on this line, although php -l won't catch it, is an html error. The last /td is not closed, so: echo td . $row['email'] . /td; Run php -l again, and this time, voila: No syntax errors detected in test.saeed.php That solves your syntax errors. Now you can test it from a server. Before you do that though, a few other points: When developing code, it's a really good idea to turn on run time error checking so you can see things in the browser when a problem crops up: ?php error_reporting(-1); // turns on EVERYTHING ini_set('display_errors',true); ini_set('display_startup_errors',true); // follow with your code... This won't help if your code doesn't parse correctly, but that's what the php -l is for above, but it will reveal run-time processing errors in your browser. And you have several. As another responder said, checking the return values of functions is important. Sometimes tutorials omit this for the sake of brevity, but it is something that still should be done. Line 3: mysql_connect(localhost,root, ); is incorrect on at least two levels. First, you haven't saved the data base resource that is returned from mysql_connect, and second, you haven't checked that the call was successful. Third, possibly is that you have specified the root mysql password a string of one space. This is unlikely to be the password. It should be rewritten as: $db = mysql_connect(localhost, root, ) or die(Could not connect to mysql server on local host: . mysql_error() . PHP_EOL); The next line: mysql_select_db(addressbook); is also incorrect as it does not specify the data base resource which you omitted in the previous call, and you aren't (again) checking the result to see if it works. It should be rewritten as: mysql_select_db($db, addressbook) or die (Could not connect to data base 'addressbook'. Error: . mysql_error($db) . PHP_EOL); Back to that line 6, after you've repaired it, you *again* need to check that the query actually worked. After that query, include the following line: if (FALSE === $result) die (Query failed. Error: . mysql_error($db) . PHP_EOL); Note that if the table is empty, that is not an error; your result set will simply be empty. Returning FALSE indicates that there was an actual error in the query. I find it better to put the sql statement into a string, and then use the string variable in the actual query function. This lets you also print out the query as it was sent to mysql: $sql = select * from colleague; $result = mysql_query($db, $sql); if (FALSE === $result) die (Query failed. \$sql=$sql. Error: . mysql_error($db) . PHP_EOL); A little further on, there is a problem in the html meta statement: there is no closer on that line: meta http-equiv=content-type content=text/html;charset=utf-8 It needs to be closed with / since you're using
Re: [PHP] Could apc_fetch return a pointer to data in shared memory ?
On 31 Mar 2012, at 02:33, Simon wrote: Or: Why doesn't PHP have Applications variables like ASP.NET (and node.js) ? Hi, I'm working on optimising a php application (Drupal). The best optimisation I've found so far is to use APC to store various bits of Drupal data in RAM. The problem with this is that with Drupal requiring say 50Mb of data* per request is that lots of cpu cycles are wasted de-serialising data out of apc_fetch. Also 50Mb of data per http process !! is wasted by each one re-creating it's own copy of the shared data. 50MB? WTF is it storing?? I've never used Drupal, but based purely on that it sounds like an extremely inefficient piece of software that's best avoided! If it were possible for apc_fetch (or similar function) to return a pointer to the data rather than a copy of the data this would enable incredible reduction in cpu and memory usage. Vanilla PHP adheres to a principle known as shared nothing architecture in which, shockingly, nothing is shared between processes or requests. This is primarily for scalability reasons; if you stick to the shared nothing approach your application should be easily scalable. This is essentially how ASP.NET Application variables and node.js work. Not a valid comparison. Node.js applications can only share variables within a single process, and they can do so because it's single-threaded. Once you scale your app beyond a single process you'd need to add a custom layer on to share data between them. I'm not sure about the architecture behind IIS and ASP.net but I imagine there are similar paradigms at work. I'm surprised PHP doesn't already have Application variables, given that they are so similar to Session Variables and that it's been around for a long time in ASP / ASP.NET. Just because x does it, doesn't mean y should. I've used lots of languages over the years, including classic ASP, ASP.net, Perl, Python, Ruby, PHP (obv), and more, and I'm yet to see a compelling reason to want application variables. Let go of the possibility of application variables and your thinking will shift to other ways of solving the problem. I just wondered if there was a reason for not having this functionality or if it's on a road map somewhere or I've missed something :) ? As far as I am aware, ASP and ASP.net are the only web technologies to support application variables out of the box. You think that's simply because the others just haven't gotten around to it yet? -Stuart -- Stuart Dallas 3ft9 Ltd http://3ft9.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] whats wrong
On 31-03-2012 10:29, tamouse mailing lists wrote: On Sat, Mar 31, 2012 at 1:45 AM, saeed ahmedmycomputerbo...@gmail.com wrote: i have made a php script with a tutorial helpi dont know where is a error.please have a look Before you actually run code in a browser, check your syntax with php -l (for lint, the old command to check C syntax on unixy systems) on a command line. This will improve your life immeasurably since you won't have to chase down where the server logs errors just to see if your code was syntactically correct. miishka:Sites tamara$ php -l test.saeed.php PHP Parse error: syntax error, unexpected T_STRING in test.saeed.php on line 6 Errors parsing test.saeed.php miishka:Sites tamara$ Which points out the syntax error on line 6 where you have: $result=mysql_query(select(SELECT*FROM COLLEAGUE); Which as an improperly formed select sql, and an unclosed string (thus sucking up part of your php code and html document. To fix this, it should be: $result=mysql_query($db, SELECT * FROM COLLEAGUE); Wrong, it should be the other way around. First the query, and then an OPTIONAL link-identifier (which I assume $db to be in this case). i.e., the sql statement is in a single string. Also, spacing around the column list * may or may not be necessary, but it's a lot easier to read if it's there. Also, you need to tell the query procedure what data base you're using the $db variable (see below for this). Ok, that fixed, run php -l on the file again, and we get: PHP Parse error: syntax error, unexpected '' in test.saeed.php on line 31 Errors parsing test.saeed.php Looking at line 31: echo td . $row['firstName'] ./td; it looks fine, you might be wondering why it's complaining about that line. The key is to look up from there for possible syntax problems, and on line 30, there is a problem in that you didn't close the last string: echotd.$row['id'] ./td; should be: echotd.$row['id'] ./td; Ok, fix that, run php -l again: PHP Parse error: syntax error, unexpected '[', expecting ',' or ';' in test.saeed.php on line 34 Errors parsing test.saeed.php So, look at line 34 now: echo td . Srow['email'] ./td; The problem is that you typoed the dollar sign in front of row. It should be: echo td . $row['email'] ./td; Also, on this line, although php -l won't catch it, is an html error. The last /td is not closed, so: echo td . $row['email'] ./td; Run php -l again, and this time, voila: No syntax errors detected in test.saeed.php That solves your syntax errors. Now you can test it from a server. Before you do that though, a few other points: When developing code, it's a really good idea to turn on run time error checking so you can see things in the browser when a problem crops up: ?php error_reporting(-1); // turns on EVERYTHING ini_set('display_errors',true); ini_set('display_startup_errors',true); // follow with your code... This won't help if your code doesn't parse correctly, but that's what the php -l is for above, but it will reveal run-time processing errors in your browser. And you have several. As another responder said, checking the return values of functions is important. Sometimes tutorials omit this for the sake of brevity, but it is something that still should be done. Line 3: mysql_connect(localhost,root, ); is incorrect on at least two levels. First, you haven't saved the data base resource that is returned from mysql_connect, and second, you haven't checked that the call was successful. Third, possibly is that you have specified the root mysql password a string of one space. This is unlikely to be the password. It should be rewritten as: $db = mysql_connect(localhost, root, ) or die(Could not connect to mysql server on local host: . mysql_error() . PHP_EOL); The next line: mysql_select_db(addressbook); is also incorrect as it does not specify the data base resource which you omitted in the previous call, and you aren't (again) checking the result to see if it works. It should be rewritten as: mysql_select_db($db, addressbook) or die (Could not connect to data base 'addressbook'. Error: . mysql_error($db) . PHP_EOL); Back to that line 6, after you've repaired it, you *again* need to check that the query actually worked. After that query, include the following line: if (FALSE === $result) die (Query failed. Error: . mysql_error($db) . PHP_EOL); Note that if the table is empty, that is not an error; your result set will simply be empty. Returning FALSE indicates that there was an actual error in the query. I find it better to put the sql statement into a string, and then use the string variable in the actual query function. This lets you also print out the query as it was sent to mysql: $sql = select * from colleague; $result = mysql_query($db, $sql); Again, query first, linkidentifier after that. This way you'll always get an error. if
Re: [PHP] Watch out for automatic type casting
On 30-03-2012 09:47, Arno Kuhl wrote: -Original Message- From: Simon Schick [mailto:simonsimc...@googlemail.com] Sent: 29 March 2012 07:19 PM To: a...@dotcontent.net Cc: php-general@lists.php.net Subject: Re: [PHP] Watch out for automatic type casting Hi, Arno FYI: I found a page in the php-manual that's exactly for that: http://www.php.net/manual/en/language.operators.precedence.php p.s. some of them were also new to me Thanks for getting me to read it. Bye Simon Thanks Simon and others, thought it was typecasting, but precedence makes more sense. I remember seeing that table when I first started using php, which is why I always use AND and OR rather than and || because it's lower precedence than the assignment and the ternary operators, but I couldn't remember where I'd seen it. So thanks for linking to it. Cheers Arno -- BTW interesting to note on that precedence page that ! has a higher precedence than = (which you'd expect it to be) but you can still do if (!$a = foo()) I use that form often (as I'm sure many others do) and just took it for granted that it works even though the order of precedence says it shouldn't. It could be expanded to if ($a = foo() != TRUE) But that wouldn't get the expected result due to order of precedence, though at first glance you could reasonably expect it to work because of if (!$a = foo()) being valid. I think that's why it's so easy to be caught out (at least for me) by the similar form of if ( $pos = strpos($sText, test) !== FALSE) Cheers Arno I would still suggest to explicitly supply the precedence you expect. Ie: if(($foo = $var) == true) { } or if($a + (15*$b) ) { } I know you can write it with fewer parentheses, but this at least makes it 100% clear you at all times WHAT is supposed to happen and you're 100% sure that PHP will understand what you WANT it to do aswell instead of having to guess, and fail at it because you don't know how guessing is implemented in the Zend Engine. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Websocket using php
Hello, I have the need to use websockets for the server part of an application I am developing and I found with a big surprise no websocket class or procedure is available on the net. Have I missed looking in some place? Being really interested in using this protocol with PHP, I start from the closest working thing: http://code.google.com/p/phpwebsocket/ and start adapting it to the lastest RFC describing the protocol http://tools.ietf.org/html/rfc6455, but it is really heavy to code and I feel like reinventing the wheel. Is there anyone out there who has already written a websocket server in PHP and like to share the code? Leandro
Re: [PHP] Thinking out loud - a continuation...
[snip] On 3/30/2012 1:14 PM, Robert Cummings wrote: On 12-03-27 11:11 AM, Jay Blanchard wrote: [snip]On 3/27/2012 12:21 AM, Robert Cummings wrote: [-- SNIP --] Essentially, entries at the root and entries for the children are just auto indexed array items but the actual entries in those arrays retain the associative index structure for retrieval of the specific information. let me know and I can probably whip you up something. Robert that looks correct. Here is an example of the JSON that the guy provided for me - var json = { id: node02, name: 0.2, data: {}, children: [{ id: node13, name: 1.3, data: {}, children: [{ id: node24, name: 2.4, data: {}, children: [{ id: node35, name: 3.5, data: {}, children: [{ id: node46, name: 4.6, data: {}, children: [] }] }, { id: node37, name: 3.7, data: {}, children: [{ id: node48, name: 4.8, data: {}, children: [] }, { id: node49, name: 4.9, data: {}, children: [] }, { id: node410, name: 4.10, data: {}, children: [] }, { id: node411, name: 4.11, data: {}, children: [] }] }, Of course he properly closes up the JSON. I inserted id's (just an auto-incrementing number) and the data portion where needed. The name: is the part that has been the result of what you did before. Here's the code... I did a bit of shuffling and actually tested against a test db table: ?mysql :) DROP TABLE IF EXISTS tiers; CREATE TABLE tiers ( company INT NOT NULL, tier1 VARCHAR( 32 ) NOT NULLDEFAULT '', tier2 VARCHAR( 32 ) NOT NULLDEFAULT '', tier3 VARCHAR( 32 ) NOT NULLDEFAULT '', tier4 VARCHAR( 32 ) NOT NULLDEFAULT '', tier5 VARCHAR( 32 ) NOT NULLDEFAULT '', tier6 VARCHAR( 32 ) NOT NULLDEFAULT '', tier7 VARCHAR( 32 ) NOT NULLDEFAULT '', tier8 VARCHAR( 32 ) NOT NULLDEFAULT '', tier9 VARCHAR( 32 ) NOT NULLDEFAULT '', tier10 VARCHAR( 32 ) NOT NULLDEFAULT '', tier11 VARCHAR( 32 ) NOT NULLDEFAULT '', tier12 VARCHAR( 32 ) NOT NULLDEFAULT '', tier13 VARCHAR( 32 ) NOT NULLDEFAULT '', tier14 VARCHAR( 32 ) NOT NULLDEFAULT '' ); INSERT INTO tiers (company, tier1, tier2, tier3) VALUES (1, 'exec-001','sub-exec-011','sub-sub-exec-111'), (1, 'exec-001','sub-exec-011','sub-sub-exec-112'), (1, 'exec-001','sub-exec-012','sub-sub-exec-121'), (1, 'exec-001','sub-exec-012','sub-sub-exec-122'), (1, 'exec-002','sub-exec-021','sub-sub-exec-211'), (1, 'exec-002','sub-exec-021','sub-sub-exec-212'), (1, 'exec-002','sub-exec-022','sub-sub-exec-221'), (1, 'exec-002','sub-exec-022','sub-sub-exec-222'); ? And here's the code: ?php function getTiers( $company ) { // // Establish the root. // $sDb = ijinn_getServiceRef( 'dbManager' ); $db = $sDb-getConnectionRef(); $query = SELECT DISTINCT . * .FROM . tiers .WHERE . company = {$company} ; $root = array(); if( $db-query( $query ) ) { while( ($row = $db-fetchRow()) ) { $focus = $root; for( $i = 1; $i = 14; $i++ ) { $name = trim( $row['tier'.$i] ); if( $name === '' ) { break; } if( !isset( $focus[$name] ) ) { $focus[$name] = array ( 'name' = $name, 'children' = array(), ); } $focus = $focus[$name]['children']; } } } $wrapper = array ( 'children' = $root ); postProcessTiers( $wrapper ); return $root; } function postProcessTiers( $root ) { $root['children'] = array_values(
Re: [PHP] Node.PHP
On Fri, Mar 30, 2012 at 21:33, Michael Save savetheinter...@omegasdg.com wrote: Because normal PHP is not asynchronous. Also, I kind of doubt you can outperform node.js with standard PHP. Your doubts are indeed well-grounded. Using node.js (indeed, V8-based apps in general) are compiled as native machine code, which don't require the added overhead of a parser, such as PHP. With that said, compiling PHP (such as with HopHop) would give at least comparable performance results. Still, all in all, I would never discourage someone doing a 'node.php' application. While its performance may not be quite as good speed-wise, that doesn't mean it can't become more robust, more generally-applicable, or even just find niche uses. I've written numerous socket servers in PHP for a variety of clients and uses, where they made sense (speed of deployment, ease of code-management by a number of developers who don't know C, et cetera). I can easily see where this could add value. -- /Daniel P. Brown Network Infrastructure Manager http://www.php.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] some good deals on good books
On Sat, Mar 31, 2012 at 03:02, tamouse mailing lists tamouse.li...@gmail.com wrote: I know there are quite a few people here who are just beginning to learn how to write programs as they also learn about PHP. O'Reilly has a deal on some pretty important books available electronically, save 50%. If this appeals to you, head to http://shop.oreilly.com/category/deals/essential-code.do and check them out. ( I apologize if this offends anyone's sensibilities. I am not in the employ of O'Reilly, nor is this going to make me any scratch. I just think this is a good chance to pick up some pretty useful books. ) There was no affiliate code, so it doesn't appear as though there was any ulterior motive. As such, if anyone is offended, perhaps they don't quite possess sensibilities. ;-P The link you provided will also benefit a lot who read the list via numerous external sources, such as forums that auto-populate with content from these lists, and the archives, which are slurped by Google so quickly that, on occasion, I've seen posts show up in Google before I even get the chance to read the original email. Great job on a thoughtful post. -- /Daniel P. Brown Network Infrastructure Manager http://www.php.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Websocket using php
On Sat, Mar 31, 2012 at 5:14 PM, Leandro Dardini ldard...@gmail.com wrote: Hello, I have the need to use websockets for the server part of an application I am developing and I found with a big surprise no websocket class or procedure is available on the net. Have I missed looking in some place? Being really interested in using this protocol with PHP, I start from the closest working thing: http://code.google.com/p/phpwebsocket/ and start adapting it to the lastest RFC describing the protocol http://tools.ietf.org/html/rfc6455, but it is really heavy to code and I feel like reinventing the wheel. Is there anyone out there who has already written a websocket server in PHP and like to share the code? Leandro Have you tried http://code.google.com/p/phpws/ ? It seems to work pretty simple.. - Matijn -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Could apc_fetch return a pointer to data in shared memory ?
On 31 Mar 2012, at 13:14, Simon wrote: Thanks again Stuart. On 31 March 2012 12:50, Stuart Dallas stu...@3ft9.com wrote: On 31 March 2012 11:19, Simon slg...@gmail.com wrote: Thanks for your answer. On 31 March 2012 09:50, Stuart Dallas stu...@3ft9.com wrote: On 31 Mar 2012, at 02:33, Simon wrote: Or: Why doesn't PHP have Applications variables like ASP.NET (and node.js) ? Hi, I'm working on optimising a php application (Drupal). The best optimisation I've found so far is to use APC to store various bits of Drupal data in RAM. The problem with this is that with Drupal requiring say 50Mb of data* per request is that lots of cpu cycles are wasted de-serialising data out of apc_fetch. Also 50Mb of data per http process !! is wasted by each one re-creating it's own copy of the shared data. 50MB? WTF is it storing?? I've never used Drupal, but based purely on that it sounds like an extremely inefficient piece of software that's best avoided! All sorts of stuff (taxonomies, lists of data, menu structures, configuration settings, content etc). Drupal is a sophisticated application. Besides, 50Mb of data seems like relatively tiny application state to want to access in fastest possible way. It's not hard to imagine wanting to use *much* more than this in future If it were possible for apc_fetch (or similar function) to return a pointer to the data rather than a copy of the data this would enable incredible reduction in cpu and memory usage. Vanilla PHP adheres to a principle known as shared nothing architecture in which, shockingly, nothing is shared between processes or requests. This is primarily for scalability reasons; if you stick to the shared nothing approach your application should be easily scalable. Yes, I know. I think the effect of this is that php will scale better (on average) in situations where requests don't need to share much data such as shared hosting. In an enterprise enviroment where the whole server might be dedicated to single application, shared nothing seems to be a synonym for re-load everything ? Yes, on one level that is what it means, but alternatively it could mean being a lot more conservative about what you load for each request. Um, I want to be *less* conservative. Possibly *much* less. (like Gigabyes or even eventually Petabytes of shared data !) We appear to have drifted off the point. There's a big difference between data that an application needs to access and application variables. What you're describing is a database. If you want something more performant there are ways to optimise access to that amount of data, but if not I've completely lost what the problem is that you're trying to solve. This is essentially how ASP.NET Application variables and node.js work. Not a valid comparison. Node.js applications can only share variables within a single process, and they can do so because it's single-threaded. Once you scale your app beyond a single process you'd need to add a custom layer on to share data between them. I'm not sure about the architecture behind IIS and ASP.net but I imagine there are similar paradigms at work. I totally agree although, I *think* IIS uses multiple threads running in a single process (or Application Pool). I realise that ASP.NET / node.js have their own architectural issues but I'm confident that for enterprise applications (ie Drupal) the option for shared something is capable of many orders of magnitude higher performance and scalability than shared nothing. And that's why there are so many options around that enable such functionality. The need for something doesn't in any way imply that it should be part of the core system. Consider the impact such a requirement would have on the environment in which you run PHP. By delegating that feature to third-party modules, the PHP core doesn't need to concern itself with the details of how to share data between processes on every target platform. Agreed. If you were able to point me in the direction of such a 3rd party module I'd be a very happy man. APC and memcached are two of the most common examples, other than the vast array of DBMSes out there. I'm surprised PHP doesn't already have Application variables, given that they are so similar to Session Variables and that it's been around for a long time in ASP / ASP.NET. Just because x does it, doesn't mean y should. I've used lots of languages over the years, including classic ASP, ASP.net, Perl, Python, Ruby, PHP (obv), and more, and I'm yet to see a compelling reason to want application variables. The reason that I'm suggesting this is because taking the example of Drupal, the ability to share information between requests by reference rather than by copy has the potential to be *millions* of times faster. Assuming I had say a 5Mb dataset that I wanted to re-use between