2014-09-10 39 views
1

嘗試從基本的phpmyadmin數據庫中提取數據。MySQLi/PHP - 從一個數據庫中提取數據。插入另一個數據庫

以下代碼正確地拉取數據(註釋掉部分驗證)。 我可以將它寫入屏幕並顯示它。 (不需要測試) 嘗試將其插入另一個數據庫,但失敗。

我發現while循環插入不運行。雖然我找不到原因。

這是一個基本的本地主機數據庫(現在測試)所以連接數據只是暫時的。

任何幫助,非常感謝 謝謝。

<?php 

/* 
    Connect to database 
*/ 
$webhost = 'localhost'; 
$webusername = 'root'; 
$webpassword = ''; 
$webdbname = 'transfertest'; 
$webcon = mysqli_connect($webhost, $webusername, $webpassword, $webdbname); 
if (mysqli_connect_errno()) { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 
/* 
* 
*/ 
$questions = mysqli_query($webcon, "SELECT * FROM questions"); 
$scenarios = mysqli_query($webcon, "SELECT * FROM scenarios"); 
$results = mysqli_query($webcon, "SELECT * FROM results"); 
$employees = mysqli_query($webcon, "SELECT * FROM employees"); 
/* 
* These while loops display the content being pulled from the database correctly. 
while ($row = mysqli_fetch_array($questions)) { 
    echo $row['questionID'] . " : " . $row['question'] . " : " . $row['answers']; 
    echo "</br>"; 
} 
while ($row = mysqli_fetch_array($scenarios)) { 
    echo $row['scenarioID'] . " : " . $row['scenarioTitle'] . " : " . $row['scenarioInformation']; 
    echo "</br>"; 
} 
while ($row = mysqli_fetch_array($results)) { 
    echo $row['employeeID'] . " : " . $row['scenarioID'] . " : " . $row['questionID'] . " : " . $row['answers'] . " : " . $row['correct']; 
    echo "</br>"; 
} 
while ($row = mysqli_fetch_array($employees)) { 
    echo $row['employeeID'] . " : " . $row['firstName'] . " : " . $row['lastName'] . " : " . $row['email'] . " : " . $row['password']; 
    echo "</br>"; 
} 
*/ 
/* ////////////////////////////////////////////////////////////////////////// 
    Connect to database 
*/ 
$mobhost = 'localhost'; 
$mobusername = 'root'; 
$mobpassword = ''; 
$mobdbname = 'exampletransfer'; 
$mobcon = mysqli_connect($mobhost, $mobusername, $mobpassword, $mobdbname); 
if (mysqli_connect_errno()) { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 
/* 
* 
*/ 
while ($row = mysqli_fetch_array($questions)) { 
    mysqli_query($mobcon, "INSERT INTO questions (questionID, question, answers) VALUES (" . $row['questionID'] . ", " . $row['question'] . ", " . $row['answers'] . ")"); 
} 
while ($row = mysqli_fetch_array($scenarios)) { 
    mysqli_query($mobcon, "INSERT INTO scenarios (scenarioID, scenarioTitle, scenarioInformation) VALUES (" . $row['scenariosID'] . ", " . $row['scenarioTitle'] . ", " . $row['scenarioInformation'] . ")"); 
} 
while ($row = mysqli_fetch_array($results)) { 
    mysqli_query($mobcon, "INSERT INTO results (employeeID, scenarioID, questionID, answers, correct) VALUES (" . $row['employeesID'] . ", " . $row['scenariosID'] . ", " . $row['questionID'] . ", " . $row['answers'] . ", " . $row['correct'] . ")"); 
} 
while ($row = mysqli_fetch_array($employees)) { 
    mysqli_query($mobcon, "INSERT INTO employees (employeeID, firstName, lastName, email, password) VALUES (" . $row['employeesID'] . ", " . $row['firstName'] . ", " . $row['lastName'] . ", " . $row['email'] . ", " . $row['password'] . ")"); 
} 
/* 
    Close Connections 
*/ 
mysqli_close($webcon); 
mysqli_close($mobcon); 
/* 
* Error code: 
Notice: Undefined index: scenariosID on line 75 

Notice: Undefined index: employeesID on line 78 

Notice: Undefined index: scenariosID on line 78 

Notice: Undefined index: employeesID on line 81 
*/ 
?> 
+1

在您從第一個數據庫獲取數據之前,您已關閉與第一個數據庫的連接。複製後稍後關閉它。 – 2014-09-10 08:41:39

+1

我認爲mysqli_fetch_array($ questions)無法完成,因爲您關閉了該數據庫的連接。 – 2014-09-10 08:42:02

+0

但是我有2個mysqli_connects,如果我把它打開的時間長了,它會發生衝突嗎? 我在將數據存儲在變量中後關閉$ webcon。那不好嗎? – Hamedar 2014-09-10 08:43:17

回答

3

的問題是,你閉上你的$ webcon連接,然後你嘗試讀取它^^

你試圖做到這一點...那不是poss IBLE;)

  1. 準備查詢後mysqli_query($webcon, "SELECT * FROM questions");
  2. 關閉連接< < <我不能讀取數據
  3. 讀取數據

試試這個吧。

<?php 

/** 
* Connect to database 
*/ 
$webhost  = 'localhost'; 
$webusername = 'root'; 
$webpassword = ''; 
$webdbname  = 'transfertest'; 
$webcon   = mysqli_connect($webhost, $webusername, $webpassword, $webdbname); 
if (mysqli_connect_errno()) 
{ 
    echo 'Failed to connect to MySQL: ' . mysqli_connect_error(); 
} 

/** 
* Queries for reading 
*/ 
$questions = mysqli_query($webcon, 'SELECT * FROM `questions`'); 
$scenarios = mysqli_query($webcon, 'SELECT * FROM `scenarios`'); 
$results = mysqli_query($webcon, 'SELECT * FROM `results`'); 
$employees = mysqli_query($webcon, 'SELECT * FROM `employees`'); 

/** 
* Connect to database 
*/ 
$mobhost  = 'localhost'; 
$mobusername = 'root'; 
$mobpassword = ''; 
$mobdbname  = 'exampletransfer'; 
$mobcon   = mysqli_connect($mobhost, $mobusername, $mobpassword, $mobdbname); 
if (mysqli_connect_errno()) 
{ 
    echo 'Failed to connect to MySQL: ' . mysqli_connect_error(); 
} 

/** 
* Insert data from old database 
*/ 

// questions 
while ($row = mysqli_fetch_array($questions)) 
{ 
    // escape your strings 
    foreach($row as $key => $val) 
    { 
     $row[$key] = mysqli_real_escape_string($mobcon, $row[$key]); 
    } 
    mysqli_query($mobcon, "INSERT INTO `questions` (`questionID`, `question`, `answers`) VALUES ('" . $row['questionID'] . "', '" . $row['question'] . "', '" . $row['answers'] . "');"); 
} 

// scenarios 
while ($row = mysqli_fetch_array($scenarios)) 
{ 
    // escape your strings 
    foreach($row as $key => $val) 
    { 
     $row[$key] = mysqli_real_escape_string($mobcon, $row[$key]); 
    } 
    mysqli_query($mobcon, "INSERT INTO `scenarios` (`scenarioID`, `scenarioTitle`, `scenarioInformation`) VALUES ('" . $row['scenariosID'] . "', '" . $row['scenarioTitle'] . "', '" . $row['scenarioInformation'] . "');"); 
} 

// results 
while ($row = mysqli_fetch_array($results)) 
{ 
    // escape your strings 
    foreach($row as $key => $val) 
    { 
     $row[$key] = mysqli_real_escape_string($mobcon, $row[$key]); 
    } 
    mysqli_query($mobcon, "INSERT INTO `results` (`employeeID`, `scenarioID`, `questionID`, `answers`, `correct`) VALUES ('" . $row['employeesID'] . "', '" . $row['scenariosID'] . "', '" . $row['questionID'] . "', '" . $row['answers'] . "', '" . $row['correct'] . "');"); 
} 

// employees 
while ($row = mysqli_fetch_array($employees)) 
{ 
    // escape your strings 
    foreach($row as $key => $val) 
    { 
     $row[$key] = mysqli_real_escape_string($mobcon, $row[$key]); 
    } 
    mysqli_query($mobcon, "INSERT INTO `employees` (`employeeID`, `firstName`, `lastName`, `email`, `password`) VALUES ('" . $row['employeesID'] . "', '" . $row['firstName'] . "', '" . $row['lastName'] . "', '" . $row['email'] . "', '" . $row['password'] . "');"); 
} 

/* 
    Close Connections 
*/ 
mysqli_close($mobcon); 
mysqli_close($webcon); 
+0

嘗試了您的代碼。 獲取相同的錯誤。 注意:未定義指數:scenariosID上線60 注意:未定義指數:employeesID在線71 注意:未定義指數:scenariosID在線71 注意:未定義指數:employeesID上線82 – Hamedar 2014-09-10 08:52:46

+0

其工作......雖然它仍然是拋出我上面發佈的錯誤。奇怪。我會盡力解決這些錯誤。謝謝PatrickB – Hamedar 2014-09-10 08:56:54

+0

發送給我sql示例轉儲(1個數據條目足夠+結構),我會修復^^ – PatrickB 2014-09-10 09:03:24

0

待定這是在同一臺服務器上,並使用相同的用戶名和密碼:使用庫MySQLi而不是MySQL的的

// Create a new MySQL database connection 
if (!$con = mysql_connect('localhost', $username, $password)) { 
    die('An error occurred while connecting to the MySQL server!<br/>' . mysql_error()); 
} 

if (!mysql_select_db($database)) { 
    die('An error occurred while connecting to the database!<br/>' . mysql_error()); 
} 

// Create an array of MySQL queries to run 
$sql = array(
    'DROP TABLE IF EXISTS `exampletransfer.questions`;', 
    'CREATE TABLE `exampletransfer.questions` SELECT * FROM `transfertest.questions`' 
); 

// Run the MySQL queries 
if (sizeof($sql) > 0) { 
    foreach ($sql as $query) { 
     if (!mysql_query($query)) { 
      die('A MySQL error has occurred!<br/>' . mysql_error()); 
     } 
    } 
} 

如果:

// Create a new MySQL database connection 
if (!$con = new mysqli('localhost', $username, $password, $database)) { 
    die('An error occurred while connecting to the MySQL server!<br/>' . $con->connect_error); 
} 

// Create an array of MySQL queries to run 
$sql = array(
    'DROP TABLE IF EXISTS `exampletransfer.questions`;', 
    'CREATE TABLE `exampletransfer.questions` SELECT * FROM `transfertest.questions`' 
); 

// Run the MySQL queries 
if (sizeof($sql) > 0) { 
    foreach ($sql as $query) { 
     if (!$con->query($query)) { 
      die('A MySQL error has occurred!<br/>' . $con->error); 
     } 
    } 
} 

$con->close(); 
相關問題