PHP:mysqli_query无效

时间:2014-02-09 22:08:09

标签: php mysql sql

我对mysqli_query有疑问..

这是我的代码的一部分:

$con = db_connect();

$sql= "SET foreign_key_checks = 0; DELETE FROM users WHERE username = 'Hola';";

$result = mysqli_query($con, $sql);
return $result;

我无法进行查询... 如果我尝试这样的查询:

$sql= "INSERT INTO categorias(id_categoria,name) VALUES ('15','ssss');";

有效。

有什么问题?我不能将SET与mysqli_query一起使用?

由于

4 个答案:

答案 0 :(得分:1)

您无法使用mysqli_query一次执行多个查询,但您可能希望使用mysqli_multi_query,因为您可以在官方文档中找到: http://www.php.net/manual/en/mysqli.multi-query.php

答案 1 :(得分:0)

让我们从创建一个有效的PHP脚本开始。

<?php
// replace for you own.
$host ="";
$user = "";
$password = "";
$database = "";

$con=  mysqli_connect($host, $user, $password, $database); 
if (!$con)
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else{
// Begin SQL query
$sql = "SELECT * FROM users";
$result = mysqli_query($con,$sql) OR Die('SQL Query not possible!');
var_dump($result);
return $result;
var_dump($result);
// End SQL query
mysqli_close($con);
};

?>

INSERT查询:

$sql= "INSERT INTO categorias(name) VALUES ('ssss')";
mysqli_query ($con,$sql) OR Die('SQL Query not possible!');

更新和删除查询:

$sql= "DELETE FROM users WHERE username = 'Hola';";
$sql.= "UPDATE users SET foreign_key_checks = 0 WHERE username = 'Hola'"; /* I made a guess here*/
mysqli_multi_query ($con,$sql) OR Die('SQL Query not possible!');

检查SET查询。我觉得缺少了一些东西。我把它改成了我认为你的目标。

答案 2 :(得分:-1)

连接应该像这样建立:

$Hostname = "Your host name mostly it is ("localhost")";
$User = "Your Database user name default is (root)"//check this in configuration files
$Password = "Your database password default is ("")"//if you change it put the same other again check in config file
$DBName = "this your dataabse name"//that you use while making database

$con = new mysqli($Hostname, $User , $PasswordP , $DBName);

$sql= "INSERT INTO categorias(id_categoria,name) VALUES ('15','ssss');";

在此查询中: 将分类放入魔术引号(`)和列名

对于下一个查询,请执行以下操作:

$sql= "SET foreign_key_checks = 0; DELETE FROM users WHERE username = 'Hola';";

更改为:

$sql= "SET foreign_key_checks = 0; DELETE FROM `users` WHERE `username` = 'Hola'";

答案 3 :(得分:-1)

<?php

$db1="database";

$con = mysqli_connect('localhost', 'root', 'root');

$db = mysqli_select_db($con, $database);

 $sql = "INSERT INTO `firstdata`(`name`, `email`, `pass`) VALUES ('$username','$email','$password')";
 $link = mysqli_query($db,$sql);

if($link){

    echo "success";
    }

else{

    echo "fail";

    }
?>

无效

  • 列表项