bsamson Posted May 11, 2008 Share Posted May 11, 2008 I am sure I am missing something here but I have this script: <?php $today = date("Y/m/d"); $unixtime = date("U"); // connection information $db2con = "reports"; include "../dbConnect.php"; $query = mysql_query("SELECT * FROM v2breakdown WHERE DATE_FORMAT(FROM_UNIXTIME(`entrydate`), '%Y/%m/%d') = '$today'") or die(mysql_error()); // connection information $db2con = "stats"; include "../dbConnect.php"; while ($row = mysql_fetch_assoc($query)) { mysql_query("INSERT INTO v2ActsMTD (gendate, store, region, spnew, sp12, sp22, nxnew, nx12, nx22) VALUES ('$unixtime', '$row[store]', '$row[region]', '$_POST[spNew]', '$_POST[sp12Mo]', '$_POST[sp22Mo]', '$_POST[nxNew]', '$_POST[nx12Mo]', '$_POST[nx22Mo]')") or die(mysql_error()); } ?> Contents of dbConnect.php <?php $hostName = "localhost"; $dbName = "nnyserve_".$db2con; $userName = "nnyserve_username"; $password = "password"; mysql_connect($hostName, $userName, $password) or die("Unable to connect to host $hostName"); mysql_select_db($dbName) or die( "Unable to select database $dbName"); ?> Everytime I run this script I get this error: Unable to select database nnyserve_stats The dbConnect.php script simply makes the connection and $db2con is the name of the DB. I have confirmed that stats exists and can't seem to figure out what causing this. Any suggestions would be greatly appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/105138-odd-mysql-db-selection-issue/ Share on other sites More sharing options...
rarebit Posted May 11, 2008 Share Posted May 11, 2008 You set this: $db2con = "stats"; and the db file uses this: mysql_select_db($dbName) or die( "Unable to select database $dbName"); e.g. use $dbName, not $db2con... Quote Link to comment https://forums.phpfreaks.com/topic/105138-odd-mysql-db-selection-issue/#findComment-538304 Share on other sites More sharing options...
MadTechie Posted May 11, 2008 Share Posted May 11, 2008 try somethingk like this **untested <?php //dbConnect.php function ConnectTo($db2con) { $hostName = "localhost"; $dbName = "nnyserve_".$db2con; $userName = "nnyserve_username"; $password = "password"; $link = mysql_connect($hostName, $userName, $password) or die("Unable to connect to host $hostName"); mysql_select_db($dbName, $link) or die( "Unable to select database $dbName"); return $link; } ?> <?php //main script $today = date("Y/m/d"); $unixtime = date("U"); // connection information include_once("../dbConnect.php"); $reports = ConnectTo("reports"); $query = mysql_query("SELECT * FROM v2breakdown WHERE DATE_FORMAT(FROM_UNIXTIME(`entrydate`), '%Y/%m/%d') = '$today'", $reports) or die(mysql_error()); // connection information $stats = ConnectTo("stats"); while ($row = mysql_fetch_assoc($query)) { //SQL injection problem mysql_query("INSERT INTO v2ActsMTD (gendate, store, region, spnew, sp12, sp22, nxnew, nx12, nx22) VALUES ('$unixtime', '".$row['store']."', '".$row['region']."', '".$_POST['spNew']."', '".$_POST[sp12Mo]."', '".$_POST['sp22Mo']."', '".$_POST['nxNew']."', '".$_POST['nx12Mo']."', '".$_POST['nx22Mo']."')", $stats) or die(mysql_error()); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/105138-odd-mysql-db-selection-issue/#findComment-538305 Share on other sites More sharing options...
rarebit Posted May 11, 2008 Share Posted May 11, 2008 oops, I didn't notice this bit! $dbName = "nnyserve_".$db2con; Therefore you can check for the existence of the db named 'stats', but it needs to be 'nnyserve_stats'... Change one or 'tother Quote Link to comment https://forums.phpfreaks.com/topic/105138-odd-mysql-db-selection-issue/#findComment-538323 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.