Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/105138-odd-mysql-db-selection-issue/
Share on other sites

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());
}

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.