0

I've a doubt with mysqli_query..

this is a part of my code:

$con = db_connect();

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

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

I can't do the query... If I try to do a query like this:

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

It works.

What's the problem?? I can't use SET with mysqli_query?

Thanks

2
  • 1
    Where to start? ;-) db_connect() is not part of the mysqli_ library. Use mysqli_connect("host","user","password","database_name"). Commented Feb 9, 2014 at 22:18
  • id_categoria is likely an auto_increment type INT field. And primary key. So, I would not recommend inserting it manually. Commented Feb 9, 2014 at 22:23

3 Answers 3

1

You can not execute multiple queries at once using mysqli_query but you might want to use mysqli_multi_query as you can find out in the official documentation: http://www.php.net/manual/en/mysqli.multi-query.php

Sign up to request clarification or add additional context in comments.

Comments

0

Lets start with creating a working php script.

<?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 query:

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

UPDATE and DELETE query:

$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!');

Check the SET query. I think something is missing. I have changed it to what I think was your aim.

Comments

-1

The connection should be established like this:

$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');";

In this query: put categorias in magic quotes(`) and column names also

For your next query do this:

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

Change to:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.