Corbokhan Posted December 10, 2010 Share Posted December 10, 2010 I'm pretty new to PHP and I'm having trouble inserting data into my database from a webpage. Here is the code. The error is on the lines with $query and $result. The error comes back: Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/content/76/5697876/html/corbinreynolds/insert.php on line 39 Lost connection to MySQL server at 'reading initial communication packet', system error: 111 I've been working at this for hours and can't seem to figure this out. Any ideas? <?php $first=$_POST['first']; $last=$_POST['last']; if (!$first || !$last) { echo 'Error: Enter the required data.'; exit; } if (!get_magic_quotes_gpc()) { $first = addslashes($first); $last = addslashes($last); } $hostname = '127.0.0.1'; $username = 'root'; $password = 'root'; $dbname = 'phonebook'; @ $db = mysql_connect($hostname,$username.$password,$dbname); if (mysqli_connect_errno()) { echo 'Errer: Could not connect to database'; echo '<br>'; echo mysqli_connect_error(); exit; } $query = "INSERT INTO contact (first,last) VALUES ('$first','$last')"; $result = mysql_query($query,$db) or die(mysql_error()); if ($result) { echo $db->affected_rows." contact inserted into the database"; echo 'Contact Entered: '.$first.' '.$last.''; } else { echo 'Error: Contact not added.'; exit; } echo 'Closing Database'; $db->close(); ?> Link to comment https://forums.phpfreaks.com/topic/221227-problem-with-mysql_query/ Share on other sites More sharing options...
MMDE Posted December 10, 2010 Share Posted December 10, 2010 What line is line 39 in your code? =o Same goes for line 111. (I can't count 111 lines ;p neither do I see any code on line 39.) Link to comment https://forums.phpfreaks.com/topic/221227-problem-with-mysql_query/#findComment-1145406 Share on other sites More sharing options...
Corbokhan Posted December 10, 2010 Author Share Posted December 10, 2010 Line 39 starts with $query. 111 is the error code. Link to comment https://forums.phpfreaks.com/topic/221227-problem-with-mysql_query/#findComment-1145407 Share on other sites More sharing options...
AbraCadaver Posted December 10, 2010 Share Posted December 10, 2010 First off, you are trying to use both mysql and mysqlI functions. You can't do that, pick one. Second, you have an error in your connect string. There is a dot instead of a comma. Thirdly, $db is not an object. I think you have just copied code from different places and tried to hack it together. Also, don't use the @ error suppression, ever. Link to comment https://forums.phpfreaks.com/topic/221227-problem-with-mysql_query/#findComment-1145408 Share on other sites More sharing options...
MMDE Posted December 10, 2010 Share Posted December 10, 2010 Line 39 starts with $query. 111 is the error code. There is no reason for that error message when you just assign a string to a variable. So probably the line under? @ $db = mysql_connect($hostname,$username.$password,$dbname); correct me if I'm wrong, but in the php documentation, it never says you can provide the database name when you connect, you gotta do that with it's own function. http://no.php.net/manual/en/function.mysql-connect.php How to do it: mysql_connect($mysqlhost,$mysqlusername,$mysqlpassword); @mysql_select_db($mysqldatabase) or die($mysqlerror); Link to comment https://forums.phpfreaks.com/topic/221227-problem-with-mysql_query/#findComment-1145414 Share on other sites More sharing options...
devilinc Posted December 10, 2010 Share Posted December 10, 2010 moreover once you do the query(supposing u corrected all of the errors above) u need to store it in an array or as an object or fetch it as an asociative array before you get your results....do a mysql_fetch_assoc($result) and storee this in a variable say $result_row and then access your column names or field names of your table just like a normal key,value pair Link to comment https://forums.phpfreaks.com/topic/221227-problem-with-mysql_query/#findComment-1145416 Share on other sites More sharing options...
Pikachu2000 Posted December 10, 2010 Share Posted December 10, 2010 And don't use @ error suppression. The only time, with very limited exception, @ should appear in your code is in an email address. Link to comment https://forums.phpfreaks.com/topic/221227-problem-with-mysql_query/#findComment-1145495 Share on other sites More sharing options...
MMDE Posted December 10, 2010 Share Posted December 10, 2010 nah, it's good when the function doesn't return TRUE when it succeeds, but it returns FALSE when it doesn't. if(@fopen($file,'r')) ^ to check if I actually could load something from another page, because the webpage might be down. would be super happy if you had another short code for that. (it was a picture btw, for then to get different properties from it) Link to comment https://forums.phpfreaks.com/topic/221227-problem-with-mysql_query/#findComment-1145571 Share on other sites More sharing options...
schilly Posted December 11, 2010 Share Posted December 11, 2010 ... sorry for the hijack. that sig video is awesome. Link to comment https://forums.phpfreaks.com/topic/221227-problem-with-mysql_query/#findComment-1145599 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.