如何在php中运行mysql脚本?

时间:2019-05-22 07:19:58

标签: php mysql

我有一个mysql脚本需要在php中运行,我已经尝试了,但是没有成功。有人可以帮我吗?

我也尝试使用mysqli_multi_query,但是它也不起作用。

<?php 
include 'connection.php';
if(isset($_GET["action"])&&($_GET["action"]=="versave")){ 
$gp_name = $_GET['gp_name'];
$version = $_GET['version'];
$query_vers = "CREATE TEMPORARY TABLE tmp SELECT * from `gp_info` WHERE `gp_name`= $gp_name";
$query_vers .= "UPDATE tmp SET `version` = $version";
$query_vers .= "ALTER TABLE tmp DROP id";
$query_vers .= "INSERT INTO `gp_info` SELECT 0, tmp.* FROM tmp";
$query_vers .= "DROP TABLE tmp";

mysqli_multi_query($connect, $query_vers);
header("Location: gp_data.php");
}
?>

<form id="verForm" name="versaveForm" method="get">
<input type="hidden" name="action" value="versave">
<div>
<input type="text" id="gp_name" name="gp_name">
<input type="text" id="version" name="version">
</div>
<input type="submit" name="vers" value="Version" id="versave">
</form>

3 个答案:

答案 0 :(得分:0)

使用PDO代替mysql_query()(另请参见:Loading .sql files from within PHP

<?php

if(isset($_POST["action"])&&($_POST["action"]=="versave")) {
    $query_vers = "CREATE TEMPORARY TABLE tmp SELECT * from `gp_info` WHERE `gp_name`= gp_name;
            UPDATE tmp SET `version` = version;
            ALTER TABLE tmp DROP id;
            INSERT INTO `gp_info` SELECT 0, tmp.* FROM tmp;
            DROP TABLE tmp";

    $db = new PDO('mysql:host=localhost;dbname=test', 'username', 'passwd');
    $st = $db->exec($query_vers);
}

答案 1 :(得分:0)

使用mysqli_query代替mysql_query。在php的新版本中已不推荐使用它,并且仅使用1个参数。我们没有在mysql_query中传递连接变量。

mysql_query($connect, $query_vers);

将此行更新为mysqli_query

mysqli_query($connect, $query_vers);

答案 2 :(得分:0)

如果您使用的是GET方法,则我们$ _GET来获取提交表单数据。

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
session_start();
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 


if(isset($_GET["action"])&&($_GET["action"]=="versave")){ 
$gp_name = $_GET['gp_name'];
$version = $_GET['version'];
$query_vers = "CREATE TEMPORARY TABLE IF NOT EXISTS tmp SELECT * from `gp_info` WHERE `gp_name`= '$gp_name';
UPDATE tmp SET `version` = '$version';
ALTER TABLE tmp DROP id;
INSERT INTO `gp_info` SELECT 0, tmp.* FROM tmp";

if(mysqli_multi_query($conn, $query_vers)){
    echo "query exicute";
}
 //header("Location: gp_data.php");
}
?>

<form id="verForm" name="versaveForm" method="get">
    <input type="hidden" name="action" value="versave">
    <div>
    <input type="text" id="gp_name" name="gp_name">
    <input type="text" id="version" name="version">
    </div>
    <input type="submit" name="vers" value="Version" id="versave">
</form>