matt_wood87 Posted June 17, 2008 Share Posted June 17, 2008 Hi Guys, i'm having trouble using connecting to my database with php, i get an error Warning: mysql_query(): 2 is not a valid MySQL-Link resource in /home/content/a/l/e/alexnail/html/website_v2/preupload.php on line 14 where line 14 is $result = mysql_query('SELECT tag_id,tag_name FROM tags',$mysql_link)or die(mysql_error()); however if i try and execute the query "SELECT tag_id,tag_name FROM tags" from the mysql front end admin interface, the query runs fine and i get the correct result. if anyone could offer me any solution as to why this isn't working from my code i'd be much appreciative! the code from my config file: $mysql_link = mysql_connect($servername, $dbusername, $dbpassword); mysql_select_db($dbname) or die('Could not select database'); if (!$mysql_link) { die('Could not connect: ' . mysql_error()); } runs fine and does not throw any errors. thanks again Matt Quote Link to comment https://forums.phpfreaks.com/topic/110572-solved-not-a-valid-mysql-link-resource/ Share on other sites More sharing options...
rhodesa Posted June 17, 2008 Share Posted June 17, 2008 First, please use code tags when posting. It's the button with the # sign on the toolbar. Second, if you are only using 1 mysql connection, the second argument can be left off, as mysql will know which resource to use: $result = mysql_query('SELECT tag_id,tag_name FROM tags')or die(mysql_error()); If that still doesn't work, please post the lines before line 14 Quote Link to comment https://forums.phpfreaks.com/topic/110572-solved-not-a-valid-mysql-link-resource/#findComment-567261 Share on other sites More sharing options...
matt_wood87 Posted June 17, 2008 Author Share Posted June 17, 2008 thanks for the reply! i made the change, and i get the print out startConnected successfullyDirectory handle: Resource id #3 Files: end Warning: mysql_query(): Can't connect to local MySQL server through socket '/usr/local/mysql-5.0/data/mysql.sock' (2) in /home/content/a/l/e/alexnail/html/website_v2/preupload.php on line 14 Warning: mysql_query(): A link to the server could not be established in /home/content/a/l/e/alexnail/html/website_v2/preupload.php on line 14 Can't connect to local MySQL server through socket '/usr/local/mysql-5.0/data/mysql.sock' (2) pre 14 is: <?php include 'config.php'; // initialization $photo_upload_fields = ''; $counter = 1; // If we want more fields, then use, preupload.php?number_of_fields=20 $number_of_fields = (isset($_GET['number_of_fields'])) ? (int)($_GET['number_of_fields']) : 5; // Firstly Lets build the Category List $result = mysql_query('SELECT tag_id,tag_name FROM tags')or die(mysql_error()); and config.php is <?php echo "start"; $servername = 'h41mysql69.secureserver.net'; $dbusername = '***username***'; $dbpassword = '***password***'; $dbname = '***dbname***'; $mysql_link = mysql_connect($servername, $dbusername, $dbpassword); mysql_select_db($dbname) or die('Could not select database'); if (!$mysql_link) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; $folderName = "gallery/full_res"; $imgarray = array(); if ($handle = opendir($folderName)) { echo "Directory handle: $handle\n"; echo "Files:\n"; $loopi=0; /* This is the correct way to loop over the directory. */ while (false !== ($file = readdir($handle))) { //while there are files left in the folder $fileext = substr(strrchr($file, "."), 1); if(strtolower($fileext) == "jpg"){ $imgarray[$loopi] = $folderName.'/'.$file; $loopi++; } } closedir($handle); } mysql_close($mysql_link); echo "end"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/110572-solved-not-a-valid-mysql-link-resource/#findComment-567279 Share on other sites More sharing options...
rhodesa Posted June 17, 2008 Share Posted June 17, 2008 you close the mysql link at the end of config.php mysql_close($mysql_link); get rid of that. php will automatically close the mysql link at the end of the script Quote Link to comment https://forums.phpfreaks.com/topic/110572-solved-not-a-valid-mysql-link-resource/#findComment-567287 Share on other sites More sharing options...
matt_wood87 Posted June 17, 2008 Author Share Posted June 17, 2008 i feel stupid. thank you! Matt Quote Link to comment https://forums.phpfreaks.com/topic/110572-solved-not-a-valid-mysql-link-resource/#findComment-567588 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.