jav805 Posted July 21, 2009 Share Posted July 21, 2009 Hi, I've been trying to learn this for about a week now. I'm trying to make my search box work on my site. If a user searches for something, I want to be able to show them results that match. I cannot for the life of me get my DB to connect. I have a table within the DB and data within that table. I'm about to lose my mind over this. I've put the .php file on my webhost and when I go to the file I get the following error "Could not select database" What am I doing wrong? Here's the code: *I've changed the un, pw, and other private information. <?php if (!$link = mysql_connect('HOST', 'USERNAME', 'PASSWORD')) { echo 'Could not connect to mysql'; exit; } if (!$link = mysql_select_db('DATABASE')) { echo 'Could not select database'; exit; } $sql = 'SELECT * FROM `TABLE` LIMIT 0, 30 '; $result = mysql_query($sql, $link); if (!$result) { echo "DB Error, could not query the database\n"; echo 'MySQL Error: ' . mysql_error(); exit; } while ($row = mysql_fetch_assoc($result)) { echo $row['foo']; } mysql_free_result($result); ?> Link to comment https://forums.phpfreaks.com/topic/166874-site-search-box-and-db-search/ Share on other sites More sharing options...
TomNomNom Posted July 21, 2009 Share Posted July 21, 2009 Hi there :-) There could be a number of things wrong. Check (and double check) you do not have the wrong database name as an argument to mysql_select_db() It could be that you are assigning the result of mysql_select_db() to $link, which should hold the resource identifier. It couldn't hurt to specify the resource identifier as an argument either. Try: if (!mysql_select_db('DATABASE', $link)) { echo 'Could not select database'; exit; } If that still doesn't work, let me know and I'd be glad to help further. Link to comment https://forums.phpfreaks.com/topic/166874-site-search-box-and-db-search/#findComment-879880 Share on other sites More sharing options...
jav805 Posted July 21, 2009 Author Share Posted July 21, 2009 Hi there, thanks for the info, however; I put it in the code (what you suggested) and I got the following error: Parse error: syntax error, unexpected T_VARIABLE in /home8/DATABASE/public_html/FOLDER/search.php on line 8 I changed DB and Folder for privacy. Link to comment https://forums.phpfreaks.com/topic/166874-site-search-box-and-db-search/#findComment-879893 Share on other sites More sharing options...
TomNomNom Posted July 22, 2009 Share Posted July 22, 2009 How very odd. Here's my complete edited version: <?php if (!$link = mysql_connect('HOST', 'USER', 'PASSWORD')) { echo 'Could not connect to mysql'; exit; } if (!mysql_select_db('DATABASE', $link)) { echo 'Could not select database'; exit; } $sql = 'SELECT * FROM `TABLE` LIMIT 0, 30 '; $result = mysql_query($sql, $link); if (!$result) { echo "DB Error, could not query the database\n"; echo 'MySQL Error: ' . mysql_error(); exit; } while ($row = mysql_fetch_assoc($result)) { echo $row['foo']; } mysql_free_result($result); I've run it against one of the tables in one of my databases. I assure you that it works and there are no syntax errors. Does your new code differ from this? Link to comment https://forums.phpfreaks.com/topic/166874-site-search-box-and-db-search/#findComment-879907 Share on other sites More sharing options...
jav805 Posted July 22, 2009 Author Share Posted July 22, 2009 nope, didn't work. I think I'm just going to have to rebuild my DB. I think I might take the day off from this...it's driving me insane. Link to comment https://forums.phpfreaks.com/topic/166874-site-search-box-and-db-search/#findComment-880425 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.