Jump to content

Db Connect and calling a function


Levan

Recommended Posts

Quick query:

I have database connect script at the beginning of my PHP page which sets the dbconnection.

 

when I run a query in the script it works fine I have decided that rather than use the default error catch like this:

$result=mysql_query($query,$db_connect) or die("There has been an error adding the appointment to the database.".mysql_error());

which works fine I would call a function to handle the errors - particularly in the case of a duplication

like so:

function mysql_error_check(){
    $error_num=mysql_errno();
    $error=mysql_error();
    echo $error_num.':'.$error;
    switch ($error_num){
    case '0':
        break;
    case '1062':
        $time=$time+1;
        mysql_select_db($database_db_connect, $db_connect);
        $query="INSERT INTO `appointment` (`Time`, `Details`, `PatientID`, `Type`, `Vet_init`,`Room`) VALUES ('$time', '$detail', '$petid', '$type', '$vet','$room')";
        $result=mysql_query($query,$db_connect) or die("There has been an error adding the appointment to the database. <a href=\"http://".$config_host.$config_dirhost."/".$extra."\">Click here to return to the main appointment page.</a> Error:".$error);
        break;
    default:
        echo "Default Switch";
        die("There has been an error adding the appointment to the database. <a href=\"http://".$config_host.$config_dirhost."/".$extra."\">Click here to return to the main appointment page.</a> Error:".$error);
        break;
}

 

The problem is when the function is called it doesnt seem to be able to use the link identifier to access the mysql database

the following error is generated

Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in myfile.php on line 31

 

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in myfile.php on line 33

 

Given the db_connect script is the first thing I call as a required script and it does work if the error function doesnt get called (ie no error occurse ) and that the dbconnect parameters are defined before the funtion is called.

Why do I get this error.....

 

 

Link to comment
Share on other sites

The scope of the mysql connection is outside of the function.  PHP is not like a lot of other languages (Javascript) where a variable declared at the "base" level is automatically available to all functions below it.

 

You can either make the mysql connection global (not recommended) or pass the connection to your function...

 

function mysql_error_check($db_connect){
...
}

Link to comment
Share on other sites

Ok then just so I can clarify this in my own head .

 

In the example above $time is the unique key for the database that in some cases a user may attempt to create a duplicate....I am adding 1 to $time and rerunning the INSERTION in my error function. 

 

However the variable $time is not passed to the function and therefore is not available within the function.  To enable me to do what I intend I will need to all the variables into the function

 

....maybe a function is not exactly what I need here.....

 

Will redesign I think

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.