I am running this query through phpMyAdmin in a Wordpress site:
SELECT trid FROM wp_icl_translations WHERE element_type = 'post_product' AND language_code = 'el'
This query returns a bunch of IDs. I want to collect these inside one variable and then run a foreach to update another table depending on the results I have. I do so like this:
<?php
global $wpdb;
$result = $wpdb->get_results("SELECT trid FROM wp_icl_translations WHERE element_type = 'post_product' AND language_code = 'el'");
foreach($result as $row) {
// run UPDATE code here
}
?>
Now the things I want to update come from this query:
UPDATE wp_postmeta SET meta_value = replace(meta_value, 'Specs','Specifications') WHERE post_id = $row->trid
It is a simple change of the word Specs to Specifications. But how do I write the above query inside the foreach in my code above? If I do var_dump($row->trid) I get correct results; all the IDs of the posts. So I just want to run the UPDATE query for each ID I find.
There is no point to get the data, then update it row by row. Just do it in one statement:
UPDATE wp_postmeta
SET meta_value = replace(meta_value, 'Specs','Specifications')
WHERE post_id IN
(
SELECT element_id
FROM wp_icl_translations
WHERE element_type = 'post_product'
AND language_code = 'el'
)
Related
Right now I'm trying to set a variable from an SQL response without doing it more than necessary.
The situation is I have a SQL result from a SELECT + JOIN query with a user_id column that has only a single value. The other columns are different per row and I need to loop through them for that data. I was wondering if there was a way to extract the homogeneous value from the user_id column without setting it over and over again to a variable in my while loop.
Code:
#where I would like the $user_id to be set
while($responseArray = $response->fetch_assoc()){
$userId = $responseArray["user_id"]; #what I don't want to do
#other fetching stuff
}
SQL:
SELECT users.username, users.user_id, posts.post_id, posts.post_content, posts.number_comments, comments.comment_id, comments.comment_post_id
FROM users
JOIN posts
JOIN comments
WHERE delete_bit = 0 AND username = "john";
You cannot get away from fetching at least one result row. Even the fetchOne()-type functions do this. But if you only want a single row, why even bother using a loop in the first place?
$result = $db->query(...);
$row = $result->fetchRow();
$value = $row['somefield'];
would be far better than something silly like
$result = $db->query(...);
while($row = $result->fetchRow()) {
$value = $row['somefield'];
break;
}
I need some help updating the price of a wp table. I am trying to do this outside of wordpress running a php script on the server.
This the table "wp_pzvy_postmeta" which has these four columns:
meta_id post_id meta_key meta_value
18538 4356 _sku GF-2070
18541 4356 _price 2.343
I need to be able to update the meta_value 2.343 based on the meta_value GF-2070, as you can see they both have the same Post_id (4356).
I do ok with php but this mysql stuff really drives me crazy.
I know how to connect to the database but after that I don't know what to do.
I guess I need to be able to retrieve the post_id based on the meta_value (GF-2070) and then use it on the UPDATE
$sql= "update `wp_pzvy_postmeta` set meta_value=5 where meta_key ='_price' and post_id = $postid";
I suggest to work with mysqli:
Create an mysqli host:
$mysqli = new mysqli("host", "user", "password", "database");
Update your $sql query:
a) Get the post_id you want:
select post_id from wp_pzvy_postmeta where meta_key = '_sku' and meta_value = #your_value#
b) Pick your expression :
update wp_pzvy_postmeta set meta_value = 5 where meta_key = '_price' and post_id = #exp_a#
c) Join everything:
Replace #your_value# with 'GF-2070'
Replace #exp_a# with a)
$sql = b)
Make your query, using the previous object:
$result = $mysqli->query($sql);
And you're ready to go! Check here for an working example :)
i have the following 2 queries that i was wondering if i can merge into a single statement.
$sql = "SELECT sale_qty FROM sale_device WHERE id = '$id' ";
and
$sql = "UPDATE device SET qty = $sale_qty WHERE id = $deviceId";
i want to run a select query to get the current quantity of a device from a row, then use that value minus one in a update query to set the new quantity, but don't allow it under 0
is this possible or advised to join? or would it be easier to just run 2 queries?
Yes. I think this does what you want:
UPDATE device
SET qty = (SELECT greatest(sale_qty - 1, 0)
FROM sale_device
WHERE id = '$id'
)
WHERE id = $deviceId
If I'm understanding your question correctly, I think this should work:
UPDATE Device D
JOIN Sale_Device SD ON D.Id = SD.DeviceId
SET D.qty = GREATEST(SD.sale_qty-1,0)
WHERE SD.Id = '$id'
This assumes the Sale_Device table has a DeviceId field.
I have a table (wp_postmeta), which contains four columns (meta_id, post_id, meta_key, meta_value). meta_key contains a field called neighborhood_value and the values for neighborhood_value are stored under the meta_value column. How do I retrieve the contents of neighborhood_value from my table and use them as a variable? Any support is appreciated
Ok, as everything is meta, thecode will also be "meta" :)
UPDATE - now when I know what you use to connect to DB I can change my answer to be more specific.
global $wpdb;
$sql = "SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=XXX";
$retMeta = array();
$results = $wpdb->get_results($sql);
foreach ($results as $resultRow)
{
if (!isset($retMeta[$resultRow['meta_key']]))
{
$retMeta[$resultRow['meta_key']] = array();
}
$retMeta[$resultRow['meta_key']][] = $resultRow['meta_value'];
}
The resulting $retMeta will be two dimensional array, and then you will be able to access your neighbourhood value by using $retMeta['neighbourhood_value'][0] (of course first you have to check if this is set - isset($retMeta['neighbourhood_value'])).
UPDATE2
For script imporing data, it'll look like that:
$sql = "SELECT meta_key, meta_value FROM wp_postmeta WHERE post_id=XXX";
$retMeta = array();
$result = mysql_query($sql);
while ($resultRow= mysql_fetch_assoc($result))
{
if (!isset($retMeta[$resultRow['meta_key']]))
{
$retMeta[$resultRow['meta_key']] = array();
}
$retMeta[$resultRow['meta_key']][] = $resultRow['meta_value'];
}
mysql_free_result($result);
But you should use WP_Query anyway, to maintain portability (the wp_postmeta doesn't have to be named that way, it can be wp2_postmeta etc).
If I understand correctly, you want to key a list of keys from meta_value?
I would do this in php: (assuming you are using some sort of framework that supports this construct. This example works with the codeigniter framework))
$sql = "SELECT distinct meta_value FROM wp_postmeta"
$result = $db->query($sql)
foreach ($result->reult() as $row) {
$mydata[] = $row->meta_value;
}
$mydata is now array containing all of your meta_value values.
I was doing same as the following.
<?php foreach($values as $value)
{
$myVarname[$value[valuefield]]=$value[valuefield];
}
$myVarname[field1]="Cow";
$myVarname[field2]="dog";
?>
If you're working with Wordpress template files such as loop.php etc., just use the handy get_post_meta() function.
You don't specify what language you want to use for your variables - but if it happens to be SQL in MySQL, here's how you "Retrieve Value From MySQL Table and Save in Variable":
select #var_name := column_name from table_name where other_column = 5;
This does the select, and as a side-effect assigns the value of the user variable #var_name to the value of column_name.
or
select #var_name := count(*) from other_table where other_column > 5;
which sets #var_name to the number of records that matched the condition.
I have a while loop of a mysql call but I also am trying to run another mysql query inside of the while loop but it is only doing it once. I can not figure it out.
Here is my code:
$sql = "SELECT * FROM widget_layout WHERE module_id=".mysql_real_escape_string($id)." AND state='".mysql_real_escape_string($page)."' AND position=".mysql_real_escape_string($position);
$query = mysql_query($sql);
while ($row = mysql_fetch_assoc($query)) {
$layout .= $row['widget_id'].'<br/>'; //test if it is looping through all rows
$sql2 = "SELECT title FROM widgets WHERE id=".$row['widget_id'];
$query2 = mysql_query($sql2);
$result2 = mysql_fetch_array($query2);
$layout .= $result2[0]; // test the title output
}
It is looping through the first query no problem but the second query is only load the title of the first widget, return null for the rest. Any idea of why this is doing this?
You don't have to use a WHILE loop -- this can be done in a single SQL statement:
SELECT wl.widget_id,
w.title
FROM WIDGET_LAYOUT wl
JOIN WIDGETS w ON w.id = wl.widget_id
WHERE wl.module_id = mysql_real_escape_string($id)
AND wl.state = mysql_real_escape_string($page)
AND wl.position = mysql_real_escape_string($position);
The issue with NULL title values depends on if the WIDGET.title column is NULLable, or there isn't a record in the WIDGETS table for the id value. You need to check the values coming back from the first query, confirm they have supporting records in the WIDGETS table first, then look at the title value...
Directly from the mysql_query() docs: multiple queries are not supported. Your innery query is killing the outer one.