PHP - Recommend Turning Errors Into Exceptions?
I recently had a need to check several arrays, and if an index wasn't set, set a variable to a given value. The code was such that I couldn't perform the isset check all at once. I then thought "wouldn't it be great if I could try the script, and catch the error or warning". Searching a bit, I came across the following script. I am a little nervous, however, that there might be negative consequences of doing so. Thoughts?
set_error_handler(function($errno, $errstr, $errfile, $errline, array $errcontext) { // error was suppressed with the @-operator if (0 === error_reporting()) { return false; } throw new ErrorException($errstr, 0, $errno, $errfile, $errline); }); try { dns_get_record(); } catch (ErrorException $e) { // ... } Similar TutorialsHaving trouble getting PDO exceptions to work, instead is raising the error handler in PHP. The connection string is fine, the port is supposed to be incorrect so I can generate a log message and show an simple error screen to the client. Warning: PDO::__construct() [pdo.--construct]: [2002] A connection attempt failed because the connected party did not (trying to connect via tcp://127.0.0.1:3305) PHP Version 5.3.2 MySQL Version 5.1.47-community try { $options = array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION); $this->db = new PDO($dsn, $username, $password, $options); //$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //$this->db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true); } catch (PDOException $e) { $this->throwError('PDODatabase->connect', 'Connection Failed: ' . $e->getMessage()); return false; } i keep getting this error Fatal error: Uncaught OAuthException: Error invalidating access token: Session does not match current stored session. This may be because the user changed the password since the time the session was created or Facebook has changed the session for security reasons. thrown in /home/sites/socialnewsoffice.com/public_html/sm-login/social-media-connect/facebook/base_facebook.php on line 1050 how can i catch and display this warning to the user?
I'd love to use anonymous placeholders on my ecommerce site project. I am writing half with php and half with golang. On the three examples below, when run, gives the following exception, " Error: Call to a member function execute() on string. " I tried it with a decimal too. Thanks in advance. $stmt = $dbo->prepare = ("SELECT * FROM products WHERE ProductName = ?"); //this one calls exception $stmt->execute(); $stmt = $dbo->prepare = ("SELECT * FROM products WHERE ProductName = ?"); //this one calls exception $stmt->bindParam(1, $productID, PDO::PARAM_INT); $stmt->execute(); $stmt = $dbo->prepare = ("SELECT * FROM products WHERE ProductName = ?"); //this one calls exception $stmt->bindValue(1, $productID, PDO::PARAM_INT); $stmt->execute(); Here is the rest of the code : <?php $filename = ""; $keyword1 = $_GET['keyword']; $titleOfSelectedDropDown = $_GET['val1']; $fileID = ""; $imageID = "a"; $displayID = ""; $keyword1 = "test"; $titleOfSelectedDropDown = "cc"; $host = 'localhost'; $user = 'root'; $pass = ''; $database = 'ecommerce'; $options = array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => false ); $gKeyword1 = ""; $gKeyword2 = ""; $gKeyword3 = ""; $key1ID = ""; $key2ID = ""; $key3ID = ""; $string1 = "<center><h1><u>Search Results</u><h1></center></p>"; $dbo = new PDO("mysql:host=$host;dbname=$database", $user, $pass, $options); $stmt = $dbo->prepare = ("SELECT * FROM products WHERE ProductName = ?"); $test = "1"; $stmt->execute([ $test ]); Edited March 17 by JoshEir mistake Hi guys. I have a record shop online. The following URL example shows the product info page without a problem: http://www.mysite.com/tp/html/product/33/record1.html Im using an .htaccess file as follows:
RewriteEngine On My problem is that the sound files are in a different folder on the server : www.mysite.com/sf/soundfile.mp3 The mp3 sound files are not found or shown on the detail.page as they are in a different folder which doesnt have a rule associated with it. It doesnt need one as the mp3 file doesnt need SEO referencing. How do i tell the .htaccess file to look in the sf folder to find the mp3 files on my detail.php page? thanks! Not strictly php, but this seems a reasonable place to ask
If documenting a routine ( which may or not directly throw an exception ), but whichh calls another one which definitely DOES, how would one write the @throws lines in the procedure header?
1. Ignore the exceptions thrown by called routines.
2. Attempt to create and maintain a list of @throws which reflect ANYTHING the routine and its callees can throw.
3. List exceptions thrown by this routine explicitly, and add
@throws as fred()
for each routine we know is called and may throw something.
1. Is consistent and easy to maintain, but scarcely useful.
2. Is a maintenance nightmare.
3. Is a slightly smaller nightmare.
I guess what is needed is an automated system that scans the routine source but I haven't been introduced to anything of that ilk.
David
Hello It's about exception handling... I have two if statements, one inside the other. And I want to throw an exception from each, if one of them is FALSE. What "technique" should I use? I think throwing same exception with the same message within two if's is a bit messy. like: if $x is true //go to 2nd if: if $y is true return value if $y is false: throw exception "Invalid number" if $x is false: throw exception "Invalid number" Thanks, Will an uncaught exception in any of the child methods percolate up? Same thing for functions? Thank you.
<?php class bla { function bla() { try { if($bad) {throw new Exception('bla bla.');} $this->something_else_that_might_throw_an_uncaught_exception(); } catch (Exception $e) {echo('do something to deal with the exception');} } function something_else_that_might_throw_an_uncaught_exception(){ $this->even_something_else_that_might_throw_an_uncaught_exception(); } } ?> Well I am working on this new site for fun I don't have any of the development done for you to even look at but I am here trying to see what others would recommend to solving on issue that I have. Ok, well basically what I want to be able to do is allow for other users of the site to "like" a comment that another member has made, and I want to keep track of how many people clicked the link and do what I can to not let them click the like again. What I can see for two options are to 1) in the comment table keep a number that increments every time someone clicks it, and then in a cookie tell the site the link has been clicked so the user does not see the like anymore, but then if the user removes the cookie they will be able to click the link again.(Not to big of a deal I guess) or 2) set up a different table that keeps track of the comment and the user so I can then use sql to count the amount of times that the commentID is in the table and then the user would not be able to click the some comment again this way. (But this just seems like a lot of database space that could be taken up) Any recommendations would be great! Thanks Hello All, I currently code in procedural PHP, but am now in a position to start learning OOP in PHP. Now i'm hoping to get my company to pay for a course in the UK (Ideally in the South East/London but if up North or elsewhere then that's fine). Anyone been to any training courses or heard of any that they can recommend and can post the links to? Thanks looking for suggestions and experiences from those of you that use a Password Manager
This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=306605.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=334552.0 MySQL cannot store Boolean values and thus I use 0/1. Is there much benefit to cast them as Boolean immediately after querying the database, do whatever PHP processing is required, and then convert them back to 0/1 before writing to the database? One benefit is I can type declare my arguments, but I am debating whether it is worth it. Thanks Finally getting around to giving Doctrine a try. Looks like YAML is depreciated, so my choices are DocBlocks or XML. I am sure it is personal choice, however, I would like to start off with what most feel is the best path. Any recommendations? Thanks This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=331627.0 having a problem turning this sql into a query and I need it to only update rows where id field is empty. Code: [Select] UPDATE systems SET naqahdah = naqahdah + naqahdah_mines * 2 I tried this out with failure Code: [Select] <?php mysql_query("UPDATE systems SET naqahdah= naqahdah + naqahdah_mines * 2 WHERE id <> ' ' "); ?> so i tried getting rid of WHERE and couldnt even get this to work. I tested the sql out it works fine. Code: [Select] <?php mysql_query("UPDATE systems SET naqahdah= naqahdah + naqahdah_mines * 2 "); ?> I am trying to turn a .txt file into an array, count the items in the array, and display a random quote from it.
<p class = "center"> <?php $Proverb = file('../proverbs.txt'); $ProverbCount = count($Proverb); echo rand(0, $ProverbCount-1); ?> <br> © 2014 </p>The issue I am having is that it states the file does not exist: FULL PATH: G:\EasyPHP-5.3.2i\www\PHP_Projects\ChineseZodiacs\Includes\inc_footer.php TEXT FILE: G:\EasyPHP-5.3.2i\www\PHP_Projects\ChineseZodiacs\proverbs.txt Please help, thank you. Hey i'm having trouble figuring out how to do this. This is the code grabbing the users who are online and listing them one by one. Code: [Select] $qt=mysql_query("SELECT username FROM users WHERE lastvisit > '$tm' and online='ON'"); echo mysql_error(); while($nt=mysql_fetch_array($qt)){ echo "<tr><td> $nt[username] </td></tr>"; } ?> what i want to do is have each username be a hyperlink that links to their profile how would i go about this? cause i'm completely clueless on this. is there some way i can do it in the php or will i need to make a varchar column for each player that holds their profile link and grab that with the query too? Hi, I'm pretty new to PHP, struggled my way through most of my encounters with PHP so far, but getting the hang of it I'm looking for a simple script that turns a number or word served by a weather station into a graphic. E.g. If the temp is 20 degrees, a graphic I've designed would be shown, or a cloud or the sun or whatever. At the moment the weather station feed is converted from 'webtags' in to php tags (temp is $temp, wind is $wind etc.) and these echo out as numbers and words on the website. I can sort of see how it would work in my head, but need a little help. Can anyone please point me in the right direction? Thanks (for reference site is www.southtyneweather.co.uk) |