futumuch Posted July 4, 2013 Share Posted July 4, 2013 Hi all,i am trying to insert some data into a table via a php function as below function save_link($new_link) { echo 'new link = '.$new_link; #just to make sure the code gets to here if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } else { echo 'success! were in!'; } $query = "INSERT INTO college_values (link_code, year_2011, year_2012, year_2013, year_2014) VALUES ('AAAB',0,0,0,0)"; $retval = mysql_query($query, $dbc);if(! $retval ){die('Could not enter data: ' . mysql_error());} return $result; } but i get the following message -Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in.... I have tried many different combinations of $query but the one above does work if I paste it into the query section of PHPMyAdmin.I can substitute the query for a SELECT and it works fine. I just cant see why the INSERT doest work. Many thanks. Link to comment Share on other sites More sharing options...
Antonio Conte Posted July 4, 2013 Share Posted July 4, 2013 You need to do one of two things: 1. Declare $dbc as a global: function save_link($new_link) { global $dbc; .... 2. Pass $dbc to the function: function save_link($new_link, $dbc) { // Now $dbc is defined Your query seems absolutly fine. Try running it in PHPMyAdmin if this doesn't help out. Link to comment Share on other sites More sharing options...
futumuch Posted July 5, 2013 Author Share Posted July 5, 2013 I think i am connecting ok, i also get this on my screen just before the error message. first link = aaacnew link = aaacsuccess! were in! This is from my echo statements. I declared the $dbc variable as global in the main body. If you need any other info let me know. Many thanks. Link to comment Share on other sites More sharing options...
HartleySan Posted July 5, 2013 Share Posted July 5, 2013 If you're using the MySQL family of functions, you should switch over to the MySQLi set of functions. In other words, use mysqli_connect instead of mysql_connect, and mysqli_query instead of mysql_query. Either way, be sure to check the documentation for the functions you are using on PHP.net to make sure that you are providing the right parameters in the right order, etc. Link to comment Share on other sites More sharing options...
futumuch Posted July 6, 2013 Author Share Posted July 6, 2013 Thanks guys for your help, in trying to solve a problem with my syntax, I copied and pasted some code from a website that had mysql_query instead of mysqli_query. While i solved the original problem, I couldnt see the problem in the pasted code. A valuable lesson learned! Many thanks. 1 Link to comment Share on other sites More sharing options...
Recommended Posts