Jump to content

Use mysql query in a function error


underv3

Recommended Posts

I'm not sure if that is the problem but I am getting this error:

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

 

The code is:

function read(){ 
mysql_query("UPDATE table SET on = '1' WHERE id = $idstatus", $connection) or die("Error querying database. Please contact the webmaster about this issue.");
header('Location: example.php');
} 

 

This is for one of the functions (the one that I am calling) the rest are just there for now but I am assuming that they don't matter if you don't call them.

 

I essentially want to set the field "on" to 1 only where the id matches the selected "idstatus".

 

Any help would be great! ;D

 

Thanks!

 

Link to comment
Share on other sites

You can't use variables defined outside of your function inside your function, like $connection. You have to make it global like this:

 

function read(){
    global $connection;
    global $idstatus;

    mysql_query("UPDATE table SET on = '1' WHERE id = $idstatus", $connection) or die("Error querying database. Please contact the webmaster about this issue.");
    header('Location: example.php');
}

 

Also you should try to use as few globals as possible. Making your $connection global might be an exception, but you'll probably want to pass your $idstatus as an argument into the function.

Link to comment
Share on other sites

Also you should try to use as few globals as possible.

When would it be appropriate?

 

but you'll probably want to pass your $idstatus as an argument into the function.

I'm not really sure how. I found this article giving methods to pass information in and out of functions but i'm not really sure how to incorporate it.

 

Just for the sake of testing my function I put all the variables inside my function and I still get an error  :-\

function read(){ 
$connection = mysql_connect("localhost", "user", "pass") or die("Error connecting to the database. Please contact the webmaster about this issue.");
mysql_query("UPDATE table SET on = '1' WHERE id = 25", $connection) or die("Error querying database. Please contact the webmaster about this issue.");
header('Location: example.php');
} 

 

Would it matter that the "on" field in my database has a default value of "0"?

 

And even with that code I'm getting the die error "Error querying database. Please contact the webmaster about this issue."

???

 

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.