Err Posted April 18, 2012 Share Posted April 18, 2012 function dbErr($num,$n) { switch ($num) { case 1044: case 1045: $err = err("Access to \"{$n}\" denied using current DB User and/or DB Pass."); break; case 1049: $err = err("The database \"{$n}\" does not exist."); break; case 2002: $err = err("There is no such DB Host. Check spelling."); break; default: $err = err("There is a problem connecting to the database. ".mysql_errno().": ".mysql_error()); } return $err; } function dbConnect($h,$u,$p,$n) { if (!$con = @mysql_connect($h,$u,$p)) {$err = dbErr(mysql_errno(),$n);} else if (!@mysql_select_db($n,$con)) {$err = dbErr(mysql_errno(),$n);} return (isset($err)) ? $err : ""; } I got told my code was wrong so I come here hoping people can prove there is a fault in my code. I suspect that people are simply not understanding some PHP syntax. If you can find anything wrong with this, let me know. Edit: I should note that the code works fine, but since I was told by several people it was wrong, I'm out to be proved wrong. Link to comment Share on other sites More sharing options...
birbal Posted April 18, 2012 Share Posted April 18, 2012 what they told about what is wrong? Link to comment Share on other sites More sharing options...
Err Posted April 18, 2012 Author Share Posted April 18, 2012 That I was connecting wrong. Link to comment Share on other sites More sharing options...
birbal Posted April 18, 2012 Share Posted April 18, 2012 I cant see anything wrong in your code syntacticaly. I belive that they are saying that cause you are encapsulating the mysql_conect() to other function. you have to memorise two function for (almost )same work. mysql function and the custom encapsulate function which could be troubleosme later. Link to comment Share on other sites More sharing options...
thescientist Posted April 18, 2012 Share Posted April 18, 2012 maybe because you are trying to surpress errors? Link to comment Share on other sites More sharing options...
Err Posted April 18, 2012 Author Share Posted April 18, 2012 Maybe. But then again they could of simply been wrong. Least it's now verified that my code is okay two other people. Link to comment Share on other sites More sharing options...
Don E Posted April 18, 2012 Share Posted April 18, 2012 I think why they are saying 'you're connecting wrong' is because if all the info is correct, you never end up making a connection with the above if that's your intent. If the 'if' or 'elseif' is FALSE, because you have it if it's TRUE if they are FALSE, but if all the info correct, the if and elseif get skipped it just returns $err if it's set or not. The only way $err gets set to one of the error messages in the switch is if it enters the 'if' or 'elseif'. When you call it like this and then echo and all info correct, meaning connection info, password, db etc, when echoing $error, it will be blank: $error = dbConnect('yourhost','user','pass','yourdb'); echo $error; Just my two cents.. I could be wrong. Link to comment Share on other sites More sharing options...
Err Posted April 18, 2012 Author Share Posted April 18, 2012 Yes. I only want the errors returned if it does not connect. I think the concept they were not getting was that even in IF.. ELSEIF statements code inside of it is executed. Also that fact that you can assign variables inside those statements coupled with the @ suppressor.I call it like this: $err = dbConnect('yourhost','user','pass','yourdb');if (!$err) { // do db stuff}echo $err;// echo other stuff, but if there is an error, it gets echo'd as well. Okay, that's all the feedback I needed. Thanks for assisting, everyone. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.