English 中文(简体)
mysqli_multi_query notembe to mysql db
原标题:mysqli_multi_query not inserting to mysql db

我使用了理论、实例,并研究了与我的问题有关的许多其他问题,我仍然可以让它去工作,我相对而言是新颖的,对项目主管机构没有任何了解。 我将我的法典改为我的手法,而不是我的手法,以便删除我的大学给我的精通的法典,但是,在这种情形下,这些法典的作用并不大。

If anyone could shed some light onto this issue for me I would be very grateful. Below are my code samples:

<?php /*connect to the db */
   $link=mysqli_connect("dbhost","user","pass");
   mysqli_select_db("db",$link);

   /*checking connection*/
      if ($link->connect_errno) 
        throw new exception(sprintf("Could not connect: %s", $link->connect_error));

session_start();



        $insert_query="
        INSERT INTO testone_tbl (age,hours,flexibility,fastpaced,retailexp,
        workedus,conviction,permit,education)
        VALUES ( $age , $hours , $flexibility , $fastpaced , $retailexp , $workedus ,
         $conviction , $permit , $education ); 
        INSERT INTO testtwo_tbl 
        (contribute,insales,initiative,success,alternatives,targets,
        newthings,custfeed,incdevelop,standards,confident,stretch,
        opportunities,polite,ideas,deadline,supported,duties)
        VALUES ( $contribute , $insales , $initiative ,
         $success , $alternatives , $targets , $newthings ,
         $custfeed , $incdevelop , $standards , $confident , $stretch ,
         $opportunities , $polite , $ideas , $deadline , $supported , $duties )";


    /*execute multi_query*/


mysqli_multi_query ($link, $insert_query);/*error1*/
/*close connection*/
if(!$link>connect_errno) $link->close(); /*error2*/

?>

这些数据既来自以(最后形式)形式写成的表格,也来自以前表格的表格。 然而,我也正在发现这一错误:警告:我的sqli_multi_query()期望参数1为我的sqli和Warning:我的sqli_close()期望参数1为我的sqli,过去几天我一直 st着! 事先感谢你。

最佳回答

You should first check with your web host if they have enabled multi-SQL-queries.

一些网站的主办者只允许单一瑞典广播公司查询,帮助防止注射袭击。

然而,如果你想在同一个桌旁看到多面,你可以这样做:

INSERT INTO tbl_name (col1, col2)
     VALUES ( ? ,  ? ),
            ( ? ,  ? ),
            ( ? ,  ? ); # inserts 3 records to the same table in one query

而且,如果你能向你们提供PDO,则使用它!

有了PDO的标语,你的询问将更加安全。 例:

$db = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);

$data = array($col1, $col2, $col3);
$sql = "INSERT INTO tbl_name (col1, col2, col3) VALUES ( ? ,  ? ,  ? );";
$query = $db->prepare($sql); # prepares the sql statement
$query->execute($data); #binds the array of data to the ?
                        #question mark parameters, and executes.

If you create a database abstraction layer, you could change the database connection mode without having to rewrite your code which executes your queries.

另外,你是否有理由不去 lo和问问? 例:

$sql_array = array("INSERT INTO tbl_one(col1) VALUES  ? ;",
             "INSERT INTO tbl_two(col3) VALUES  ? ;");

function performAll($sql_array) {
    # execute all of the queries
}

对我来说,你可能利用某种职能来获取你的数据库链接。 现在,这不是一个问题,除非你在职能范围内实际尝试利用数据库链接(如果你没有告诉我们)。 例:

$db = new PDO("...", $user, $pass);

$query = $db->prepare($sql); # works fine

function executeQuery($sql) {
    $query = $db->prepare($sql); # error: $db is not defined
                                 # within the scope of this function
    ...
}

为此,请在项目设计书上使用以下关键词: 例:

$db = new PDO("...", $user, $pass);

function executeQuery($sql) {
    global $db; # use $db in the global scope
    $query = $db->prepare($sql); # works fine
    ...
}
问题回答

从警告中可以明显看出,<代码>$link不是我的sqli物体。 您要么没有联系,要么在某个时候把$link调往他人。

你还需要在你接通之后立即检查你的联系。 关于该链接的中间行动(此处为mysqli_select_db)将查明任何错误。

您也不应对我的灵魂和程序性交织在一起。 面向目标的风格非常清楚,但如果难以改变现行法典,那么就遵守程序风格。

相反地连接:

$link = mysqli_connect("dbhost","user","pass", "db"); // no need for an extra db select
if (mysqli_connect_errno()) {
    throw new Exception("Could not connect: ".mysqli_connect_error());
}

此外,我希望这符合你的真正准则,因为它对我的注射攻击非常开放。 考虑完全使用多序列,并使用与地主的发言稿。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签