Re: [PHP] question about best practice for coding sql statements
On Sat, Nov 12, 2011 at 7:01 AM, Stephen wrote: > While I am not a big fan of OOP, I do like PDO, and recommend its use. Right -- I wasn't actually inquiring about how to access a data base, merely the pactice of using a variable for the SQL -- In your example, you are doing this as well, which fits in fine with what I was enquiring about. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] mysqli question
On 2011-11-13 1:42 AM, Tommy Pham wrote: >>or db_error ($dbh, $__FILE__, $__LINE__); > > __FILE__ are reserved keywords __LINE__. If you intended to use > variables represent the similar meaning, the suggested approach would Yes, sorry, was a bit quick there - I'm using __FILE__ __LINE__ to indicate the file/line where the query failed to the error-handling function. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] mysqli question
On Sat, Nov 12, 2011 at 6:15 AM, Peet Grobler wrote: > Not sure if this is the correct mailing list to be asking this question > but here goes: > > I've got a prepared statement. > > $stmt = $dbh->prepare ("insert into test values (?, ?)") > or die ("Error: " . $dbh->error); > $stmt->bind_param ('ii', $var1, $var2) > or die ("Error: " . $dbh->error); > $stmt->execute() > or die ("Error: " . $dbh->error); > > Okay I've taken the "or die" statements and put it into a function > called 'db_error'. So it looks kinda like this: > > or db_error ($dbh, $__FILE__, $__LINE__); > > My db error then prints out "$dbh->error" and FILE and LINE variables so > I know exactly where the error is. > > I would, however, love to print out the actual SQL text that failed. > There's no way to determine it, the closest way I can think of is using > a $sql variable holding the 'prepare' sql - but that's not what's going > to the database. > > On a mysqli error - I can only see the actual sql that caused the > problem on the development system, as mysql logging is turned on on > that. It's not possible to turn on on the live system the overhead would > probably kill us. > > Anyone found a way to do this? > > Peet > Depends on your overall application design. Meaning if you got database abstraction layer in place to handle all your DB access or the code snippet you've provided is used directly in a file. In either case, the same concept applies but details of approach are different. * store the SQL statement string into a variable * in your error handler, access that variable and send it to where you need to Thus, this: > $stmt = $dbh->prepare ("insert into test values (?, ?)") >or die ("Error: " . $dbh->error); becomes $sql = "insert into test values (?, ?)"; $stmt = $dbh->prepare ($sql) or db_error ($dbh, __FILE__, __LINE__, $sql); function db_error(object $dbh = null, $file, $line, $sql) { /* do what you need with it here */ } >or db_error ($dbh, $__FILE__, $__LINE__); __FILE__ are reserved keywords __LINE__. If you intended to use variables represent the similar meaning, the suggested approach would be $file and $line respectively. It would make things easier to read and less confusing when you're troubleshooting, IMO. If you have everything done in classes [1] and put all your DB access in an abstraction layer class, you can use Exception [2] and get stack traces [3] to better handle and troubleshoot any errors. [4] is the non OOP way of error handling, IMO less elegant approach. Best regards, Tommy [1] http://php.net/language.oop5 [2] http://php.net/language.exceptions [3] http://php.net/exception.gettrace [4] http://php.net/ref.errorfunc -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] question about best practice for coding sql statements
On Sat, 12 Nov 2011, tamouse mailing lists wrote: I'm curious how others feel about this. When I'm creating an SQL string, either for a non-prepared or prepared execution, I build it in a variable and then pass the variable to the query or prepare function. This lets me easily add something to view the SQL statement, and also if there's an error, emit the SQL statement. I do this. This means that when a user encounters an error, they can actually give you a meaningful error report which should reduce the time it takes to fix it by a considerable amount. Geoff. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] problem with sending AT command in php
dio_write($handle, 'AT') & dio_write($handle, "AT") make firefox times out on Waiting for localhost ... . But dio_write($handle, "AT\n") makes it prints AT exactly the same command or A > A , .. On Sat, Nov 12, 2011 at 10:02 PM, Negin Nickparsa wrote: > are you sure about ATD03518726535\n? > > can you try if ( dio_write($handle, 'AT') )? >
Re: [PHP] problem with sending AT command in php
are you sure about ATD03518726535\n? can you try if ( dio_write($handle, 'AT') )?
[PHP] problem with sending AT command in php
In The Name Of Allah The compassionate merciful Hello ; Am trying to send AT commands to my GSMmodem device , in linux with PHP Am sure it is /dev/ttyusb1 But with [URL="http://pastebin.com/aut4ygXf"]this code[/URL] device respond the same command and code prints that command e.g if we send ATD00 it responds ATD00 , or with AT it responds AT instead of Ok, why ? Regards dehqan
Re: [PHP] Pipe To A Program
On 12 Nov 2011, at 11:49, Ron Piggott wrote: > Ok. It works. I finally am able to display the contents of an e-mail! Now > I am able to write my apps > > I see your monster of all incoming e-mail processors ;) Thanks for showing > me this. I will look it over when I am more awake. I kind of get it right > now, but I need to look more closely. Good stuff. I've resurrected an article that used to be on my site that explains that example code in a bit more detail: http://stut.net/2011/11/12/handling-email-notifications/ - hopefully that will make it less of a monster :) -Stuart -- Stuart Dallas 3ft9 Ltd http://3ft9.com/ > -Original Message- From: Stuart Dallas > Sent: Saturday, November 12, 2011 6:21 AM > To: Ron Piggott > Cc: php-general@lists.php.net > Subject: Re: [PHP] Pipe To A Program > > On 12 Nov 2011, at 07:38, Ron Piggott wrote: > >> I am looking at CPanel’s “E-Mail filtering” option “Pipe To A Program” >> http://docs.cpanel.net/twiki/bin/view/AllDocumentation/CpanelDocs/FilterOptions >> >> The goal I am working towards is saving the contents of an incoming e-mail >> address into a mySQL table. I am thinking of trying to program a customer >> contact center application. >> >> Does anyone know what variable the e-mail message is assigned within the >> context of “Pipe To A Program”? Is there a way to find out? I can’t figure >> this out. >> >> What I have tried so far is below: >> >> === >> #!/usr/local/bin/php -q >> > >> >> foreach($_REQUEST as $key => $val) { >>$$key = $val; >> >> $email_body .= "KEY: " . $key . "\r\n"; >> $email_body .= "VAL: " . $val . "\r\n"; >> $email_body .= "\r\n"; >> >> } >> >> mail( user@domain , "Test Pipe To Program" , $email_body ); >> === >> >> The mail command works, but the e-mail message body is empty. I am at a >> loss of how to proceed. > > > When you pipe email to a program the mail server literally does that - it > pipes the contents of the email to the stdin of your program. > > In PHP you would then read the email contents like so... > > $email = file_get_contents('php://stdin'); > > Note that what you get is the raw email, complete with headers. When I do > this in PHP I use the Mailparse extension: http://php.net/book.mailparse > > Here's a (somewhat over-complicated) example from an old version of TwitApps > when I processed Twitter email notifications: https://gist.github.com/1360403 > > -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
[PHP] mysqli question
Not sure if this is the correct mailing list to be asking this question but here goes: I've got a prepared statement. $stmt = $dbh->prepare ("insert into test values (?, ?)") or die ("Error: " . $dbh->error); $stmt->bind_param ('ii', $var1, $var2) or die ("Error: " . $dbh->error); $stmt->execute() or die ("Error: " . $dbh->error); Okay I've taken the "or die" statements and put it into a function called 'db_error'. So it looks kinda like this: or db_error ($dbh, $__FILE__, $__LINE__); My db error then prints out "$dbh->error" and FILE and LINE variables so I know exactly where the error is. I would, however, love to print out the actual SQL text that failed. There's no way to determine it, the closest way I can think of is using a $sql variable holding the 'prepare' sql - but that's not what's going to the database. On a mysqli error - I can only see the actual sql that caused the problem on the development system, as mysql logging is turned on on that. It's not possible to turn on on the live system the overhead would probably kill us. Anyone found a way to do this? Peet -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] question about best practice for coding sql statements
On 11-11-12 06:30 AM, tamouse mailing lists wrote: I'm curious how others feel about this. When I'm creating an SQL string, either for a non-prepared or prepared execution, I build it in a variable and then pass the variable to the query or prepare function. This lets me easily add something to view the SQL statement, and also if there's an error, emit the SQL statement. Thus: $sql = "SELECT * FROM TABLE WHERE id=$id"; if ($debug) error_log($sql." from ".__FILE__.' at '.__LINE__' in '__FUNCTION__.PHP_EOL); // just an example $rslt = $db->query($sql) or die("Could not get result from $sql: ".$db->errno.": ".$db->error.".".PHP_EOL); and so on... While I am not a big fan of OOP, I do like PDO, and recommend its use. This is a sample function I have to maintain a record based on POSTed changes: function updatecategory($dbh, $x) { $sql = "UPDATE gallery_category SET category_name = :name, category_description = :description WHERE category_id = :id"; $catname = fieldcheck($x['catname']); $catdescription = textfieldcheck($x['catdescription']); $stmt = $dbh->prepare($sql); try { $stmt->bindParam(':name', $catname); $stmt->bindParam(':description', $catdescription); $stmt->bindParam(':id', $x['cid']); $stmt->execute(); } catch (PDOException $e) { return 'Error updating category orders: ' . $e->getMessage(); } return "Maintained category " . $catname; } PDO is very efficient when you are looping through updates, and the error handling is simple to code. Using a disciplined format keeps everything readable and easy to use as a template for additional work. Cheers Stephen -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] question about best practice for coding sql statements
I'm curious how others feel about this. When I'm creating an SQL string, either for a non-prepared or prepared execution, I build it in a variable and then pass the variable to the query or prepare function. This lets me easily add something to view the SQL statement, and also if there's an error, emit the SQL statement. Thus: $sql = "SELECT * FROM TABLE WHERE id=$id"; if ($debug) error_log($sql." from ".__FILE__.' at '.__LINE__' in '__FUNCTION__.PHP_EOL); // just an example $rslt = $db->query($sql) or die("Could not get result from $sql: ".$db->errno.": ".$db->error.".".PHP_EOL); and so on... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Pipe To A Program
On 12 Nov 2011, at 07:38, Ron Piggott wrote: > I am looking at CPanel’s “E-Mail filtering” option “Pipe To A Program” > http://docs.cpanel.net/twiki/bin/view/AllDocumentation/CpanelDocs/FilterOptions > > The goal I am working towards is saving the contents of an incoming e-mail > address into a mySQL table. I am thinking of trying to program a customer > contact center application. > > Does anyone know what variable the e-mail message is assigned within the > context of “Pipe To A Program”? Is there a way to find out? I can’t figure > this out. > > What I have tried so far is below: > > === > #!/usr/local/bin/php -q > > > foreach($_REQUEST as $key => $val) { > $$key = $val; > > $email_body .= "KEY: " . $key . "\r\n"; > $email_body .= "VAL: " . $val . "\r\n"; > $email_body .= "\r\n"; > > } > > mail( user@domain , "Test Pipe To Program" , $email_body ); > === > > The mail command works, but the e-mail message body is empty. I am at a > loss of how to proceed. When you pipe email to a program the mail server literally does that - it pipes the contents of the email to the stdin of your program. In PHP you would then read the email contents like so... $email = file_get_contents('php://stdin'); Note that what you get is the raw email, complete with headers. When I do this in PHP I use the Mailparse extension: http://php.net/book.mailparse Here's a (somewhat over-complicated) example from an old version of TwitApps when I processed Twitter email notifications: https://gist.github.com/1360403 -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] Pipe To A Program
On Sat, Nov 12, 2011 at 5:12 AM, Ron Piggott wrote: > > > I used your code and it still didn't work. Would you show me what you put > in for your Pipe To A Program settings? > > What I used is: > > Rules: "To" "Contains" customer service e-mail address > > "Action" > /usr/local/bin/php /path/to/email_to_ticket_gateway.php > > - I know the rule is working because I receive an empty e-mail, just not > passing the e-mail content This sounds more like a CPanel question than a PHP question, however, if your action above is what gets executed, there will be no $_REQUEST global as it is run as a cli program. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Pipe To A Program
I used your code and it still didn't work. Would you show me what you put in for your Pipe To A Program settings? What I used is: Rules: "To" "Contains" customer service e-mail address "Action" /usr/local/bin/php /path/to/email_to_ticket_gateway.php - I know the rule is working because I receive an empty e-mail, just not passing the e-mail content Ron Piggott www.TheVerseOfTheDay.info -Original Message- From: tamouse mailing lists Sent: Saturday, November 12, 2011 4:04 AM To: Ron Piggott Cc: php-general@lists.php.net Subject: Re: [PHP] Pipe To A Program On Sat, Nov 12, 2011 at 1:38 AM, Ron Piggott wrote: Does anyone know what variable the e-mail message is assigned within the context of “Pipe To A Program”? Is there a way to find out? I can’t figure this out. What I have tried so far is below: === #!/usr/local/bin/php -q $val) { $$key = $val; $email_body .= "KEY: " . $key . "\r\n"; $email_body .= "VAL: " . $val . "\r\n"; $email_body .= "\r\n"; } mail( user@domain , "Test Pipe To Program" , $email_body ); === The mail command works, but the e-mail message body is empty. I am at a loss of how to proceed. Ron I'm not sure what's going on with your program, exactly. Here's what I tried: start script<< $val) { $$key = $val; // do not know what this is supposed to do here... $email_body .= wordwrap("KEY: " . $key . "\n",70,"\n"); $email_body .= wordwrap("VAL: " . $val . "\n",70,"\n"); $email_body .= "\n"; } print_r($email_body); mail( 'tamara@localhost' , "Test Pipe To Program" , $email_body ); end script<< (Note: according to http://us3.php.net/manual/en/function.mail.php: "Each line should be separated with a LF (\n). Lines should not be larger than 70 characters." Which is why I added the wordwrap on each line and terminated the lines with "\n" instead of "\r\n".) Calling the script, print_r showed the string as expected: start output<< tamara@caesar:~/$ curl 'http://localhost/~tamara/testmail.php?a=1&b=2&c=3' KEY: a VAL: 1 KEY: b VAL: 2 KEY: c VAL: 3 end output<< And the email message showed up in my inbox: tamara@caesar:~/$ from Test Pipe To Program And the contents of the email are: start email<< Return-Path: Delivery-Date: Sat Nov 12 02:54:13 2011 Envelope-to: Return-path: Received: from www-data by caesar with local (masqmail 0.2.27) id 1RP9Lp-0oD-00 for ; Sat, 12 Nov 2011 02:54:13 -0600 To: tamara@localhost Subject: Test Pipe To Program X-PHP-Originating-Script: 1000:testmail.php From: Date: Sat, 12 Nov 2011 02:54:13 -0600 Message-ID: <1RP9Lp-0oD-00@caesar> KEY: a VAL: 1 KEY: b VAL: 2 KEY: c VAL: 3 end email<< -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Pipe To A Program
On Sat, Nov 12, 2011 at 1:38 AM, Ron Piggott wrote: > Does anyone know what variable the e-mail message is assigned within the > context of “Pipe To A Program”? Is there a way to find out? I can’t figure > this out. > > What I have tried so far is below: > > === > #!/usr/local/bin/php -q > > > foreach($_REQUEST as $key => $val) { > $$key = $val; > > $email_body .= "KEY: " . $key . "\r\n"; > $email_body .= "VAL: " . $val . "\r\n"; > $email_body .= "\r\n"; > > } > > mail( user@domain , "Test Pipe To Program" , $email_body ); > === > > The mail command works, but the e-mail message body is empty. I am at a > loss of how to proceed. > > Ron I'm not sure what's going on with your program, exactly. Here's what I tried: >>start script<< $val) { $$key = $val; // do not know what this is supposed to do here... $email_body .= wordwrap("KEY: " . $key . "\n",70,"\n"); $email_body .= wordwrap("VAL: " . $val . "\n",70,"\n"); $email_body .= "\n"; } print_r($email_body); mail( 'tamara@localhost' , "Test Pipe To Program" , $email_body ); >>end script<< (Note: according to http://us3.php.net/manual/en/function.mail.php: "Each line should be separated with a LF (\n). Lines should not be larger than 70 characters." Which is why I added the wordwrap on each line and terminated the lines with "\n" instead of "\r\n".) Calling the script, print_r showed the string as expected: >>start output<< tamara@caesar:~/$ curl 'http://localhost/~tamara/testmail.php?a=1&b=2&c=3' KEY: a VAL: 1 KEY: b VAL: 2 KEY: c VAL: 3 >>end output<< And the email message showed up in my inbox: tamara@caesar:~/$ from Test Pipe To Program And the contents of the email are: >>start email<< Return-Path: Delivery-Date: Sat Nov 12 02:54:13 2011 Envelope-to: Return-path: Received: from www-data by caesar with local (masqmail 0.2.27) id 1RP9Lp-0oD-00 for ; Sat, 12 Nov 2011 02:54:13 -0600 To: tamara@localhost Subject: Test Pipe To Program X-PHP-Originating-Script: 1000:testmail.php >From: Date: Sat, 12 Nov 2011 02:54:13 -0600 Message-ID: <1RP9Lp-0oD-00@caesar> KEY: a VAL: 1 KEY: b VAL: 2 KEY: c VAL: 3 >>end email<< -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php