MySQLi / PHP - 从一个数据库中提取数据。插入另一个数据库

时间:2014-09-10 08:34:23

标签: php mysql database

尝试从基本的phpmyadmin数据库中提取数据。

下面的代码正确地提取数据(注释掉部分验证)。 我可以把它写到屏幕上并显示出来。 (不需要只是测试) 尝试将其插入另一个数据库,然后失败。

我发现插入的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
 */
?>

2 个答案:

答案 0 :(得分:4)

问题是您关闭了$ webcon连接,然后尝试从中读取^^

你试图这样做......那是不可能的;)

  1. 准备查询mysqli_query($webcon, "SELECT * FROM questions");
  2. 关闭连接&lt;&lt;&lt;之后,我无法读取数据
  3. 阅读数据
  4. 请试试。

    <?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);
    

答案 1 :(得分:0)

等待它在同一台服务器上并使用相同的用户名和密码:

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

如果使用MySQLi而不是MySQL:

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