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

标签 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
 */
?>

最佳答案

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

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

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

关于php - MySQLi/PHP - 从一个数据库中提取数据。插入到另一个数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25760779/

相关文章:

php - 为什么我不应该在 PHP 中使用 mysql_* 函数?

mysql - 加入具有相同 ID PDO 的 2 个表

c# - 错误 0004 : Could not load file or assembly 'MySql. 数据.Entity,版本=6.9.7.0

PHP MySQL 插入问题

c# - 定期发送自动电子邮件

mysql - mysql 查询中烦人的 IF/THEN 语句

java - 在多个表中查找值

PHP/MySQL 过滤数据时遇到问题

php - 将后台 worker 添加到 AWS Elastic Beanstalk

php - 我无法将我的 styles.css 文件恢复到其原始形式