如何对SQL执行多个查询 [英] How to do multiple queries to SQL

查看:523
本文介绍了如何对SQL执行多个查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个表,并借助PHP脚本在数据库中插入一些值. 仅插入1行时,它可以正常工作.当我尝试输入更多行数时,它给出了错误;

I am trying to create a table and , insert some values in database with help of PHP script. While inserting only 1 row , it works fine. An when I try to enter more number of rows , It gives error;

当我使用在线excel到SQL查询转换器时,我需要为每个查询编写整个插入语句.

I need to write whole insert statement for every query as I am using an online excel to SQL query converter.

<?php

error_reporting(E_ALL);
ini_set('display_errors', '1');
include_once("connect_to_Mysql.php");
$sqlCommand = "CREATE TABLE book (
Book_Index INT,
Book_name VARCHAR(38) CHARACTER SET utf8,
author_name VARCHAR(5) CHARACTER SET utf8,
Publisher VARCHAR(5) CHARACTER SET utf8,
Category INT,
Subcategory VARCHAR(21) CHARACTER SET utf8,
Price INT)";

$query = mysqli_query($db_conx,$sqlCommand) or die(mysqli_error($db_conx));
echo "<h3>Success creating book table</h3>";

$sqlCommand = "
INSERT INTO book VALUES (1,'Concepts of Physics Volume I','hc verma','bharti bhavan','jee','physics',635,'english');
 INSERT INTO book VALUES (2,'PLANE TRIGONOMETRY Part-1','sl loni','arihant publication','jee','math',95,'english');
 INSERT INTO book VALUES (3,'VIT (VELLORE) EDGE SOLVED PAPERS & 10 MOCK        TESTS (2007-2015)','Arihant Experts','arihant publication','vit','all in one',345,'english');
 INSERT INTO book VALUES (4,'A Textbook of Algebra','sk goyal','arihant publication','jee','math',425,'english');
INSERT INTO book VALUES (5,'New Pattern IIT JEE PHYSICS','dc pandey','arihant publication','jee','physics',665,'english');
INSERT INTO book VALUES (6,'Objective Approach to Mathematics','amit m aggrawal','arihant publication','jee','math',710,'english');
INSERT INTO book VALUES (7,'A Complete Success Package for','Experts Compilation','arihant publication','bitsat','all in one',775,'english');
INSERT INTO book VALUES (8,'15 Years'' Solved Papers for AMU Engineering     Entrance Exam','arihant Experts','arihant publication','AMU','all in one',325,'english');
INSERT INTO book VALUES (9,'Solved Papers & 5 Mock Tests for BVP Engineering','arihant Experts','arihant publication','BVP','all in one',365,'english');

";
$query = mysqli_query($db_conx,$sqlCommand) or die(mysqli_error($db_conx));
echo "<h3>Success populating the pages table with data</h3>";

?>

输出

Successful database connection, happy coding!!!
Success creating book table
You have an error in your SQL syntax; check the manual that corresponds      to your MySQL server version for the right syntax to use     near 'INSERT INTO book VALUES (2,'PLANE TRIGONOMETRY Part-1','sl loni','arihant public' at line 2

推荐答案

使用mysqli_multi_query代替mysqli_query,应该这样做.

Use mysqli_multi_query instead of mysqli_query, that should do it.

$query = mysqli_multi_query($db_conx,$sqlCommand) or die(mysqli_error($db_conx));

http://php.net/manual/en/mysqli.multi- query.php

这篇关于如何对SQL执行多个查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆