PHP multiple MYSQL commands in one mysql_query() query [duplicate] - php

This question already has answers here:
How to execute two mysql queries as one in PHP/MYSQL?
(8 answers)
Closed 8 years ago.
I want to issue multiple mysql commands with one mysql_query function.
This is my code:
$query .= "INSERT INTO `users` VALUES(1,'stack','overflow');";
$query .= "INSERT INTO `posts` VALUES('other','stack','overflow');";
mysql_query($query);
If I do that I get a warning that my syntax would be incorrect.
If I echo the output, copy it and execute it in phpMyAdmin it works.
Where is the error there?

$query = "INSERT INTO `users` VALUES (1,'stack','overflow'), ('other','stack','overflow');";
mysql_query($query);
PHP does not support sending more than one query at a time via mysql_query, but you can achieve your result in a single one by using the above.

I think you need this?? http://us2.php.net/manual/en/mysqli.multi-query.php

according to http://www.php.net/manual/en/function.mysql-query mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier.
but this guy said that you just have to pass flag 65536 as mysql_connect's 5 parameter
http://www.php.net/manual/en/function.mysql-query.php#91669

Related

How to performs multiple query on the database Oracle using PHP Language [duplicate]

This question already has answers here:
Best way to do multi-row insert in Oracle?
(9 answers)
Closed 3 years ago.
In order to run a query for database MySQL, we are using mysqli_query. Then, for running a query for database Oracle, we are using oci_execute.
When we wish to running multiple query for database MySQL, we are using mysqli_multi_query. Example as below:-
$mysqliconn = mysqliconn(); //mysqli connection
$sql = '
INSERT INTO TABLE VALUES();
INSERT INTO TABLE VALUES();
INSERT INTO TABLE VALUES();
INSERT INTO TABLE VALUES();
';
if(mysqli_multi_query($mysqliconn, $sql)) {
echo 'Success';
}
My Question is if there anyone of you whom can come out with the most simplest solution to run multiple inserting values into the database table using one command execution.
Oracle has an INSERT ALL statement for that.
Otherwise you could just loop in your code and execute it n times.
$sql = '
INSERT INTO TABLE abc VALUES(123,'xyz'),(456,'def'),(789,'qwe');
';
I suggest that using support library ^^

Settling up a MySQL database to hold code [duplicate]

This question already has answers here:
How can I prevent SQL injection in PHP?
(27 answers)
Closed 5 years ago.
Maybe a silly question, but I'm really struggling.
I've created a MySQL Database and Table and am trying to push data into it. The data in question is a unique ID and a load of Javascript.
Code is simple:
$sql = "INSERT INTO TableName (id, code)
VALUES ('$uniqueid', '$codeoutput')";
However, whilst I can get the ID in there, I can't get the DB to accept the Javascript (which is currently stored in the variable $codeoutput.
I get the error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax
I'm not that familiar with using MySQL, and I have played around with different data types in PhpMyAdmin, but sadly still no luck.
What am I doing wrong? Do I need to have a very specific setup on the column where I'm storing the code? I'm currently trying to store it as medium text.
Is it something to do with needing to escape special characters? Is there an easy solution to this?
simplest way to do it is
$sql = "INSERT INTO TableName ($uniqueid) VALUES ('id')";
$sql2 = "INSERT INTO TableName ($codeoutput) VALUES ('code')";

PHP MYSQL Select Last Inserted ID [duplicate]

This question already has answers here:
How do I get the last inserted ID of a MySQL table in PHP?
(16 answers)
Closed 6 years ago.
I tried to find an answer on here already, as well as read the PHP doc on it, however it doesn't explain it very well.
I'm just trying to get the last ID inserted by the user. I know MYSQL is deprecated, but I need to do it this way.
This is what I have so far.
$query="INSERT INTO teams (team_name)
VALUES
('$team_name')";
mysql_query($query) or die('error');
$team_id = mysql_query("SELECT LAST_INSERT_ID()");
Thanks!
Take a look at mysql_insert_id
But beware of the issues when using ON DUPLICATE KEY UPDATE statements. It is described in the comments on the php doc page.
But your solution should work, too.
Also, if you're using mysql, don't forget to mysql_real_escape your input, $team_name in your case.
[insert obligatory mysql is deprecated warning here ;)]
You can do it by PHP. You don't need a query: mysql_insert_id()

mysql_affected_rows() always return 0 [duplicate]

This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 7 years ago.
I need to update mysql table, and get the number of tables updated using php, the code look like below,
$query = "update ACCESSUSERS set ACTIVE='111' where UPPER(USERNAME)=UPPER('firstname') and PINNUMBER='7777'";
mysqli_query($conn, $query);
$numrows = mysql_affected_rows();
printf("Records updated: %d\n", $numrows);
But the issue is the above print 0 always even if changed the value for ACTIVE with different one.
What can be the issue, any help will be appreciated.
Note: I have tested the above query from phpMyadmin and it's working, only problem while executing in a php.
Thanks,
Haris
You are using the mysqli_ API to make your database query.
You are using the obsolete mysql_ API to count the rows.
You can't switch APIs and expect them to interact with each other. Stick to mysqli_.
Use mysqli_affected_rows instead of mysql_affected_rows
You need to use the mysqli version of affected_rows. Which is
$numrows = mysqli_affected_rows($conn);

how to get data after sql inerst command in php [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Is there a way to return the id of a row that was just created in MySQL with PHP?
hi, i have an inset command such as
insert (name) values($name);
where i have id column as outoincrement how can i get the id of the inserted record after inserting directly
If you are working with mysql_* function, you'll have to use mysql_insert_id() (quoting) :
Retrieves the ID generated for an
AUTO_INCREMENT column by the
previous query (usually INSERT).
With mysqli, you'll use mysqli::insert_id().
And, with PDO, you'll call PDO::lastInsertId().
mysql_query("Your query here");
$last_id = mysql_insert_id();

Categories