Why the result return is null? What is wrong? If I use this consult on directly phpmyadmin, the result is correctly.
<?php $postid = get_the_ID(); ?>
<?php
$get_thumb = $wpdb->get_var
( "SELECT meta_value FROM `wp_postmeta` WHERE `post_id` = $postid AND `meta_key` = '_wp_attached_file'" );
echo "<p>Thumb URL: {$get_thumb}</p>";
?>
I'd suggest using the $wpdb->prepare() statement to ensure the SQL query is generated correct and the $get_thumb variable is correctly set in the query.
$postid = get_the_ID();
$get_thumb = $wpdb->get_var($wpdb->prepare(
"SELECT meta_value FROM `wp_postmeta` WHERE `post_id` = %s AND `meta_key` = '_wp_attached_file'",$postid));
echo "<p>Thumb URL: {$get_thumb}</p>";
Again you can always log the SQL string generated by prepare() to ensure it matches your expectations. See https://codex.wordpress.org/Class_Reference/wpdb#Examples for more examples.
I think your syntax has some trouble, so here's your original query revised.
<?php
$get_thumb = $wpdb->get_var(
"
SELECT meta_value
FROM wp_postmeta
WHERE post_id = " . $postid . " AND meta_key = _wp_attached_file
"
);
echo "<p>Thumb URL: {$get_thumb}</p>";
?>
I made sure the PHP is echoing correctly inside the query - I am not sure if that's a problem you were having.
I do recommend the prepared query too. Here is it with what I think is correct syntax (I found it was helpful to prepare items by putting them in a $query_arg_array). See https://codex.wordpress.org/Class_Reference/wpdb.
$postid = get_the_ID();
$query_arg_array = [$postid];
$get_thumb = $wpdb->get_var($wpdb->prepare(
"
SELECT meta_value
FROM wp_postmeta
WHERE post_id = %d AND meta_key = _wp_attached_file
",
$query_arg_array
));
echo "<p>Thumb URL: {$get_thumb}</p>";
If that prepared query didn't work, try putting $postid directly as argument instead of the $query_arg_array.
Remember SQL is super sensitive so even an extra space can throw off your query.
Related
Working traditional query:
$form_id = Caldera_Forms::get_field_data( '_entry_id', $form );
$post_id_qry = mysql_query("SELECT `meta_value` FROM wp_cf_form_entry_meta WHERE entry_id = '$form_id' and meta_key = 'ID'");
$post_id = mysql_fetch_array($post_id_qry);
echo $post_id['meta_value'];
Now when I convert over to a sprintf() to make it more secure and it returns blank. I have tried a syntax check and it came back clean. Any ideas?
$form_id = Caldera_Forms::get_field_data( '_entry_id', $form );
global $wpdb;
$post_id_qry = sprintf("SELECT `meta_value` FROM %s WHERE entry_id = %d and meta_key = 'ID'", $wpdb->prefix . 'cf_form_entry_meta', $form_id );
$post_id = $wpdb->get_results($post_id_qry);
echo $post_id['meta_value'];
$wpdb->get_results() returns an array of all the results of the query, not just a single row. And by default the rows are objects, not associative arrays. So you need to do:
echo $post_id[0]->meta_value;
to display the value.
If you want to get a single row as an associative array, use:
$post_id = $wpdb->get_row(ARRAY_A);
echo $post_id['meta_value'];
Note that WP doesn't have anything analogous to mysql_fetch_array. The options to get_row are ARRAY_A, which is like mysql_fetch_assoc, and ARRAY_N, which is like mysql_fetch_row.
I am trying to get the value of meta_value field from current post.
<?php
$cupost = $post->ID;
$registros = $wpdb->get_results( 'SELECT meta_value FROM auto_postmeta
WHERE post_id = $cupost AND meta_key="mg_game"');
echo $registros[0]->meta_value . "<br/>";
?>
It does not shows the echo from $registros.
But if i put directly on the query the number of the post (post_id = 7), it shows the value of meta_value.
Thy this out:
<?php
$cupost = $post->ID;//First check if this variable gets the id, i would echo the $cupost variable to check.
$registros = $wpdb->get_results( "SELECT meta_value FROM auto_postmeta WHERE post_id = ".$cupost." AND meta_key= 'mg_game' ");
echo $registros[0]->meta_value . "<br/>";
?>
You have to concatenate the Query String with the Php Variables
I need to select some values from a table column which has a datatype of "longtext" where another column has a specific value and add those values up. I need to store the sum as a variable which I will a.) echo on a webpage, and b.) use the value in a calculation later in the page.
This works to find all of the values which I need to add up:
$query = "SELECT meta_value
FROM wp_postmeta
WHERE meta_key = '_crowdfundingtotalprice'
ORDER BY CAST(`meta_value` AS UNSIGNED) DESC";
and I can display the results with:
$result = mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_array($result)){
echo $row['meta_value']. "<br>";
}
From my searching, I think I am close with my query, but my page fails to load when I try to echo the results. Here is what doesn't work:
$query = "SELECT CAST(SUM('meta_value') as UNSIGNED)
FROM wp_postmeta
WHERE meta_key = '_crowdfundingtotalprice'";
$result = mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_array($result)){
echo $row[SUM('meta_value')]. "<br>";
}
As you may have guessed, this is a Wordpress database table, and I cannot change the datatype. As always, your help is appreciated!
EDIT - Trying Gordon Linoff's suggestion below, I have removed the single quotes around meta_value. It still doesn't work, but thank you for the suggestion:
$query = "SELECT CAST(SUM(meta_value) as UNSIGNED)
FROM wp_postmeta
WHERE meta_key = '_crowdfundingtotalprice'";
$result = mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_array($result)){
echo $row[SUM(meta_value)]. "<br>";
}
SOLVED:
Thank you to Gordon Linoff for pointing out my typographical error. I ended up making a sample table and hammering this out on my own. Indeed, the single quotes in my query were not needed. However, where I was echoing the results, I needed the single quotes, but they were in the wrong place. Note $row['SUM(meta_value)'] instead of $row[SUM('meta_value')].
Additionally, I did not need to use CAST (which didn't work), because MYSQL will by default treat values as mathematical values if the query uses math.
$query = "SELECT meta_key, SUM(meta_value)
FROM wp_postmeta
WHERE meta_key = '_crowdfundingtotalprice'";
$result = mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_array($result)){
echo $row['SUM(meta_value)'] ."<br>";
}
I am trying to to filter out numerous (500000) <p>  ;</p> entries in my WordPress site, but I cannot figure out how. I've tried many thing, but still no luck.
Also many \n\n  ; entries should be filtered out and replaced by ''
Here is my code:
$query = mysql_query('select meta_id, meta_value from wp_postmeta where meta_value like "%<p> </p>%" ');
while ($item = mysql_fetch_array($query))
{
echo "item with id:" . $item[meta_id]." found<br>";
$string = preg_replace('~<p> <\/p>~i', '', $item["meta_value"]);
$id = $item[meta_id];
$q = mysql_query('update wp_postmeta set meta_value = "$string" where meta_id = "$id"') or die(mysql_error());
}
Any suggestions?
Why not trying the MySQL replace function?
update table_name set field_name = replace(field_name,'string_to_find','string_to_replace');
Here is a mySQL query with the password and usernames deliberately botched out. It is returning only one result is not returning any other information than today's date for that one result. It does not matter what search one tries it is not functioning.
<?php
$proto = $_GET['p'];
$terms = $_GET['f'];
$return;
if($proto == 'inline'){
echo 'checking';
$username="*******";
$password="*******";
$database="*******";
$my_text = $_GET['f']; //what I'm searching for
$my_category = '8'; //whatever category number it is
mysql_connect(localhost,$username,$password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());
$result = mysql_query("SELECT ID FROM wp_posts WHERE post_title LIKE '%$my_text%' ");
// select all posts that have your text
while ($row = mysql_fetch_array($result));
$postname = $row['post_name'];
$posttitle = $row['post_title'];
$postID = $row['ID'];
$date = date ( 'd-M-Y' , strtotime($row['post_date']) );
$return.= '
'.$posttitle.' ('.$postname.')<br /><span style="font-size:10px; color:#555;">'.get_the_time("d-M-Y", $postID).' - '.get_post_meta($postID, "status", true).'</span>
';
//while have posts
echo $return;
}
?>
In addition to the other information:
I might be missing it, but I don't see you encompassing anything in your while loop.
That would be why it's only getting one. You need to use braces { }
Your query does not select post_name, post_title, or post_date. You said SELECT ID FROM wp_posts, so it selected ID and nothing else, as instructed. Your date() call sort of works because strtotime() is returning false, so it uses today's date as a fallback.
This:
mysql_query("SELECT ID FROM wp_posts WHERE post_title LIKE '%$my_text%' ");
is very unsafe - my_text isn't escaped, and is taken from GET params, so it opens you to a SQL Injection attack. Escape it using mysql_real_escape_string, or, better yet, use parametrized queries in your code.