calmchess Posted December 21, 2007 Share Posted December 21, 2007 I'm useing the following script to connect to an sql database now after it connects and everything a function is being called and information from the database is being passed as parameters....my question is how do i stop that sigSucc function from firing if a connection to the database cannot be established ? i was thinking of using an if statement but I don't know what event is called or what data is returned when a successful connection is made ....could somebody plz shed some light on this subject for me. <? //connect to db $conn = mysql_connect("localhost", "root", "KKT1pKT2KKT1pKT2") or die(mysql_error()); mysql_select_db('refid', $conn) or die(mysql_error()); global $conn; //query database $q = mysql_query ("select * from vidref"); //fetch row from database while($row = mysql_fetch_array($q)) { //get count from database record $counter0=$row['vidid']; } //??????want if statement to fire sigSucc function only after a successful connection and query to the database has been made????????? sigSucc($counter0,$totUsers0,$q,$conn); ?> Quote Link to comment https://forums.phpfreaks.com/topic/82700-solved-php-mysql-fire-function-question/ Share on other sites More sharing options...
chigley Posted December 21, 2007 Share Posted December 21, 2007 Well, considering you have or die() commands, the sigSucc() function would never be executed if the connection failed. Quote Link to comment https://forums.phpfreaks.com/topic/82700-solved-php-mysql-fire-function-question/#findComment-420625 Share on other sites More sharing options...
calmchess Posted December 21, 2007 Author Share Posted December 21, 2007 can i use the die commands to fire a function defined by me if it does fail? Quote Link to comment https://forums.phpfreaks.com/topic/82700-solved-php-mysql-fire-function-question/#findComment-420632 Share on other sites More sharing options...
GuitarGod Posted December 21, 2007 Share Posted December 21, 2007 You have this code <? //connect to db $conn = mysql_connect("localhost", "root", "KKT1pKT2KKT1pKT2") or die(mysql_error()); The 'or die' part basically means that if the connection to the database fails, then no code after the line above will be executed (read and carried out). So your function won't be executed if there is no database connection anyway, no need for extra code. If you must insist on re-arranging your code (even though you don't need to), then you can try this: <? //connect to db $conn = mysql_connect("localhost", "root", "KKT1pKT2KKT1pKT2"); mysql_select_db('refid', $conn) or die(mysql_error()); global $conn; //query database $q = mysql_query ("select * from vidref"); //fetch row from database while($row = mysql_fetch_array($q)) { //get count from database record $counter0=$row['vidid']; } //?Huh??want if statement to fire sigSucc function only after a successful connection and query to the database has been made?HuhHuh?? if ( $conn ) { sigSucc($counter0,$totUsers0,$q,$conn); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/82700-solved-php-mysql-fire-function-question/#findComment-420645 Share on other sites More sharing options...
calmchess Posted December 21, 2007 Author Share Posted December 21, 2007 no need to write extra code you have cleared my questions/worries/confusion........thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/82700-solved-php-mysql-fire-function-question/#findComment-420655 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.