Jump to content

[SOLVED] php mysql fire function question


calmchess

Recommended Posts

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);

 

?>

Link to comment
Share on other sites

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);
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.