How to do Multiple MySQL Query in mysql_query() function?


Problem :

I have a problem updating record on a table. Below is the detail:

I have a table named tbl_option, it has 2 field : option_name, option_value. The current table record is follow:

option_name | option_value
site_name   | MySite
site_desc   | About anything

I want to update both site_name and site_desc option_value, here is my php script to update the site_name and site_desc option_value:

require "include/config.php";
$name = "MyNewSitess";
$desc = "About Computer";

$query = mysql_query("UPDATE tbl_option SET option_value='$name' WHEREoption_name='site_name';# UPDATE tbl_option SET option_value='$desc' WHERE option_name='site_desc'");

if ($query) { echo "Saved"; }
else echo "Not saved : ".mysql_errno()." | ".mysql_error();

after executed I get the following error:

Not saved : 1064 | 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 'UPDATE tbl_option SET option_value='About Computer' WHERE option_name='site_desc' at line 1

The record not updatd. But when I add (#) before the second UPDATE query :

<?php
...
$query = mysql_query("UPDATE tbl_option SET option_value='$name' WHERE option_name='site_name'; #UPDATE tbl_option SET option_value='$desc' WHERE option_name='site_desc'");
...
?>

It not showing any error. I get the following :

Saved

and the record is updated

What is the problem ?

Yes, I imitate the wordpress table concept but I don't know how to do the UPDATE query to the table.



Solution :

Change from

$query = mysql_query("UPDATE tbl_option SET option_value='$name' WHEREoption_name='site_name';# UPDATE tbl_option SET option_value='$desc' WHERE option_name='site_desc'");

to

$query = mysql_query("UPDATE tbl_option SET option_value='$name' WHERE option_name='site_name'"); 

if($query) $query = mysql_query("UPDATE tbl_option SET option_value='$desc' WHERE option_name='site_desc'");

However, if this is a WordPress update, then you are better off posting this question here. You might just get a better solution :-)

Another option if you are doing this only once is to directly the console or a popular tool by the name of of PhpMyAdmin to do the updates.


Mysql Tutorials

Mysql Howto..

How to connect mvc with mysql?

How do I efficiently find all records that DO NOT overlap with a date interval?

how to detect incoming chat message?

How do I configure MySQL to save data on another volume?

In MySQL, how to run sql in the command line?

How can I tell what version of mysqli is installed?

How to get all category with their sum of products using their attribute in mysql

How to create a mysql “sha-256” column?

How do write IF ELSE statement in a MySQL query

How to make data in mysql only selectable once via a webform?

How to dump a single table in MySQL without locking?

How manage results from mysql properly

MySQL subquery returning more than one row, how to workaround?

How to change a particualr character of a string in MySQL?

How to Auto Save Selection in ComboBox into MYSQL in PHP without submit button?

How do I get MySQL to throw a conditional runtime exception in SQL

Echo the results to show all values with for each

How to optimize mysql query using index?

How to avoid a possible conflict associating data sent by a form into a MySQL database with an autoincrement unique id?

How to change order of substrings inside a larger string?

How to store the apostrophe into MySQL database from user input? [duplicate]

How to make a Rails Migration be utf-8 instead of my default latin1

How to set mysql Default value NONE

how to get Set data type from mysql to java

How to do an mySQL query that return unique values from different tables

How to increase mysql table comments length?

How to group result retrieved from one column in mysql

How should I store types of coupons in my db?

How do I add indexes to a MySQL table when creating a new table from a SELECT statement?

How to solve SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry?