ryanfilard Posted March 11, 2012 Share Posted March 11, 2012 I have created a script to insert data into a database, but it returns this. Warning: mysql_select_db() expects parameter 2 to be resource, null given in /RJFWS198/testing/elemata_project/admin/elements/create_page.php on line 51 Here is the code <?PHP //Protect Against Attacks if (empty($_SESSION['E_User'])) { die("Access Denied"); } ?> <?php if (!empty($_REQUEST['title']) && (!empty($_SESSION['E_User']))) { require_once('../Connections/default.php'); if (empty($_REQUEST['password'])) { $password = "0"; }else{ $password = $_REQUEST['password']; } if (empty($_REQUEST['menu_order'])) { $mo = "0"; }else{ $mo = $_REQUEST['menu_order']; } if (empty($_REQUEST['meta_keywords'])) { $meta_keywords = "0"; }else{ $meta_keywords = $_REQUEST['meta_keywords']; } if (empty($_REQUEST['meta_desc'])) { $meta_desc = "0"; }else{ $meta_desc = $_REQUEST['meta_desc']; } if (empty($_REQUEST['meta_copyright'])) { $meta_copyright = "0"; }else{ $meta_copyright = $_REQUEST['meta_copyright']; } $author = $_SESSION['E_User']; $date = date("m.d.y"); $day = date("d"); $month = date("n"); $year = date("Y"); $time = ''.date("g").''.date("i").''.date("a").''; $ptitle = $_REQUEST['title']; $post_content = $_REQUEST['postContent']; $searchable = $_REQUEST['seachable']; $locked = $_REQUEST['privacy']; $tags = $_REQUEST['tags']; $meta_robots = $_REQUEST['robots']; mysql_select_db($database_default, $default); mysql_query("INSERT INTO posts (`time`, `year`, `month`, `day`, `date`, `author`, `title`, `content`, `searchable`, `locked`, `password`, `menu_order`, `tags`, `meta_keywords`, `meta_desc`, `meta_robots`, `meta_copyright`) VALUES ('$time', '$year', '$month', '$day', '$date', '$author', '$ptitle', '$post_content', '$searchable', '$locked', '$password', '$mo', '$tags', '$meta_keywords', '$meta_desc', '$meta_robots', '$meta_copyright')"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/258694-warning-mysql_select_db-expects-parameter-2-to-be-resource/ Share on other sites More sharing options...
AyKay47 Posted March 11, 2012 Share Posted March 11, 2012 No where in the code do I see the variable $default being defined. Judging from the error, I would say that it isn't at all, unless it's coming from the required page. Quote Link to comment https://forums.phpfreaks.com/topic/258694-warning-mysql_select_db-expects-parameter-2-to-be-resource/#findComment-1326171 Share on other sites More sharing options...
PFMaBiSmAd Posted March 11, 2012 Share Posted March 11, 2012 The variable $default doesn't exist. What is your code (less any database credentials) in ../Connections/default.php? Quote Link to comment https://forums.phpfreaks.com/topic/258694-warning-mysql_select_db-expects-parameter-2-to-be-resource/#findComment-1326172 Share on other sites More sharing options...
ryanfilard Posted March 11, 2012 Author Share Posted March 11, 2012 This is the code in the required file <?php # FileName="Connection_php_mysql.htm" # Type="MYSQL" # HTTP="true" $hostname_default = "localhost"; $database_default = "X"; $username_default = "X"; $password_default = "X"; $default = mysql_pconnect($hostname_default, $username_default, $password_default) or trigger_error(mysql_error(),E_USER_ERROR); //Get Elemata Pro User Data $ecms_user = "XXXX"; $ecms_password = "XXXX"; $ecms_key = "XXXX"; //Elemata Connection Information $site_id = "XXXXXX-XXXXX-XXXX-XXXXX"; //Do Not Change This ID $action_key = "XXXXXXXXXXXXX-XXXXXXXXXX-XXXXXXXXX-XX"; //Do Not Change This Key ?> Quote Link to comment https://forums.phpfreaks.com/topic/258694-warning-mysql_select_db-expects-parameter-2-to-be-resource/#findComment-1326175 Share on other sites More sharing options...
PFMaBiSmAd Posted March 11, 2012 Share Posted March 11, 2012 The code you have posted cannot produce the error you are getting. Either you have more than one default.php file at different paths and you are including/requiring the wrong one or your require_once() statement is actually using a URL and you altered the code for the post or you have some code somewhere that is un-setting the $default variable after the database connection was made or the code you posted is inside of a function definition and you didn't bother to post the actual code. Quote Link to comment https://forums.phpfreaks.com/topic/258694-warning-mysql_select_db-expects-parameter-2-to-be-resource/#findComment-1326201 Share on other sites More sharing options...
ryanfilard Posted March 11, 2012 Author Share Posted March 11, 2012 Its confusing because it still posts the data. Quote Link to comment https://forums.phpfreaks.com/topic/258694-warning-mysql_select_db-expects-parameter-2-to-be-resource/#findComment-1326210 Share on other sites More sharing options...
ryanfilard Posted March 11, 2012 Author Share Posted March 11, 2012 I fixed it, the file was included on the wrong page which already had that variable. Quote Link to comment https://forums.phpfreaks.com/topic/258694-warning-mysql_select_db-expects-parameter-2-to-be-resource/#findComment-1326217 Share on other sites More sharing options...
xyph Posted March 11, 2012 Share Posted March 11, 2012 This is a good reason to use CONSTANTS for values that won't change. If you try to define a constant twice, you'll get a fatal error. Quote Link to comment https://forums.phpfreaks.com/topic/258694-warning-mysql_select_db-expects-parameter-2-to-be-resource/#findComment-1326226 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.