I am trying to query the mysql database with the following statement:
SELECT form_data.*, forms.orderId, forms.eType
FROM form_data
LEFT JOIN forms ON form_data.fieldId = forms.id
WHERE form_data.submitId='somehashedid'
ORDER BY forms.orderId ASC
If I use this statement within phpmyadmin the output returns only rows containing the submitId which is handed over. Which is exactly what I want.
But trying to use the same query in php returns also rows not containing the correct submitId
function getReportDetails($reportId) {
$stmt = $this->prepare("SELECT form_data.*, forms.orderId, forms.eType FROM form_data LEFT JOIN forms ON form_data.fieldId = forms.id WHERE form_data.submitId=? ORDER BY forms.orderId ASC");
$stmt->bind_param("i", $reportId);
$stmt->execute();
return $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
}
// $_REQUEST['id'] is checked not to by empty
$data = $dao->getReportDetails($_REQUEST['id']);
// $data is checked to have an array of results
submitId is a varchar(35) utf8_general_ci
submitIds look like this f89cf0bc3660424b017f9bfe0d8c0252
My workaround for now is to check in my loop again if the db id matches the requested id and limit the output of only intended information. But why do I get wrong results?
If you do also have other improvements to my code please let me know.
Thanks for your help!
because submitId is a varchar, you should use character 's' instead of 'i' for bind_param method(character 's' uses for bind string data type or varchar while 'i' uses for integers)
so try something like below..
$stmt->bind_param("s", $reportId);
I have table with 10 columns and I want to check input value in where clause of the MySQL query.
I want to do something like this. But, when I use this query I am getting an error.
for example :
SELECT * FROM user_data
where poll_title='$poll_title'
and '$voter' IN (user_vote_1,user_vote_2,user_vote_3...user_vote_10)
order by idpoll ASC
user_vote_1 to 10 (value is null'ed in the database) and I want to retrieve only that rows from a column which have $voter value.
I think you need this comparison (Not Sure OfCourse) :-
SELECT * FROM user_data
where poll_title = "$poll_title"
and (user_vote_1 = "$voter"
OR user_vote_2 = "$voter"
OR user_vote_3 = "$voter"
OR user_vote_4 = "$voter"......OR user_vote_10 = "$voter")
order by idpoll ASC
If I've understood what you want to do - return only the column with the value - then would coalesce do the job? This assumes that the value in user_vote_n will either match the value you're looking for or be null, since coalesce returns the first non-null argument.
(untested)
select coalesce(user_vote_1, user_vote_2, user_vote_3, ) as UserVote from user_data
where coalesce(user_vote_1, user_vote_2, user_vote_3, ) = '$voter';
That aside, this looks like a structure that could do with normalising - a single 'user_vote' column and a single 'user_vote_number' column.
I have the following query in PHP:
$titlematch = mysql_query("SELECT `item_id` FROM `items` WHERE `item_id` IN ($matches_s) AND MATCH (`eng_name`) AGAINST ('$query') IN NATURAL LANGUAGE MODE AS score;");
$titlematch2 = mysql_fetch_assoc($titlematch);
In plain words, I want to select the item_id from the table items where
the item_id value can be found within the array $matches_s, and
the string stored in eng_name can be found within the string $query
and then output a 2-dimensional array, where the second-level array contains the item_id and score (relevance) elements.
I have tried switching the order of which criteria comes first, or using brackets. Here are the results I get:
var_dump($titlematch); // returns bool(false)
var_dump($titlematch2); // returns NULL
Question is 'What am I doing wrong? Do I have to write this differently to other AND statements?
I've looked all over the interwebs, and cannot find that simple answer I'm looking for - possibly because it doesn't exist, but.. possibly because I don't know the correct terms to search for.
ok, so, i've got a variable - it's actaully a key value pair in an array that i'm passing into my function. the key is args[comments_mentioned] and the value is dynamically generated for me - it's ALWAYS going to be number, separated by commas (i.e. 1,2,3,4,5)
so, just to be super clear:
$args[comments_mentioned] == "1,2,3,4"; //could be any amount of number, not just 1,2,3,4,5
i'd like to pass this into a sql statement as a variable to use in an "IN" clause, like so:
$sr_sql = <<<SQL
SELECT *
FROM $wpdb->commentmeta
WHERE meta_value = %s
AND comment_ID in ($args[comments_mentioned])
ORDER BY meta_id DESC
SQL;
Then, Prepare it using the wordpress prepare and get results
$sr_query = $wpdb->prepare( $sr_sql, $args[user_id]) );
//receive the correct sql statement, and plug 'er in.
$sr_comment_rows = $wpdb->get_results($sr_query);
and run my foreach loop:
foreach ($sr_comment_rows as $sr_comment) {
$sResults .= 'do something with $sr_comment';
}
now, i realize the code above won't work - i can't just pass the variable in there like that. BUT, i can't pass it as a string (%s), because it wraps it in '1,2,3,45', and so it looks for the entire string, and not each number. I can't pass it as an int (%d), because of the commas...
In other posts, they mentioned create a temp table or variable, but, i'm not sure if that's the correct way to do it in mysql, or how to reference it once I do.
so, how do I do this? preference for actual code that works ;)
Thank you for reading and helping out!
One option, if you cannot normalize your data, is to enclose your string in commas such that it be ",1,2,3,4," and then you could do:
AND LOCATE( CONCAT(',',comment_ID,',') , ($args[comments_mentioned]) )
which will match if it finds a eg. ',3,' in ',1,2,3,4,' (Using 3 as an example)
I believe this should be enough:
$params = $args[comments_mentioned];
$table = $wpdb->commentmeta;
$sr_sql = "
SELECT *
FROM $table
WHERE meta_value = %s
AND comment_ID in ($params)
ORDER BY meta_id DESC
";
It will be result something like:
SELECT *
FROM table_on_variable
WHERE meta_value = %s
AND comment_ID in (1,2,3,4)
ORDER BY meta_id DESC
If your mainly issue is regarding the in clause, so you will not have problems if you use double quotes and single variable as illustrated above.
I need to fetch a particular column value of a particular row using php in mysql.
For example if I want to fetch column2 value in row2, I tried using:
$qry = mysql_query("SELECT * from $table");
$data = mysql_fetch_row($qry);
$result = $data[1];
but this always returns only the first row value.
SELECT Column2
FROM $table
ORDER BY Something
LIMIT 1,1;
Or, if you know the key of the row
SELECT Column2
FROM $table
WHERE Key = Something
-- Optional: if you want 2nd after filtering
-- ORDER BY Something
-- LIMIT 1,1;
select COLUMNNAME from TABLENAME where ROWELEMENT="SOME_VALUE";
This should be your sql query..
You will need to access the value of that element using
$result['COLUMNNAME']
Complete code should look like this.
$query="select COLUMNNAME from TABLENAME where ROWELEMENT='SOME_VALUE'";
$result=mysql_query($query);
$result=mysql_fetch_array($result);
$columnvalue=$result['COLUMNNAME'];
Its bcoz mysql_query returns the result in an associative array form.
After the above code, You can access all elements like this.
$columnname[$index];
This will return the first value,
According to your question, You will need to first get the column values in a saperate array, and then access it using your index value..