I'm trying to insert a failsafe into a code to prevent them from going a step further, and I've been scratching my head for a while now. I am able to put something into an array, but I am not able to get the items from the array to match the second select query. I only get Array instead of the value from the item.
My first select query is this:
$datohenter3 = "select DATE_FORMAT(datotid, '%Y.%m.%d') AS dato from gramorapport34 group by dato order by dato asc";
$hentdatoer = $db->query($datohenter3);
$periodedatoer = array();
for ($x = 1; $x <= $db->affected_rows; $x++) {
$periodedatoer[] = $hentdatoer->fetch_assoc();
}
Then I want to match the values from this array with my next select query:
$rapportdatoer = "select fradato, tildato from gramorapportlogg WHERE fradato IN('".$periodedatoer."') OR tildato IN('".$periodedatoer."')";
$rapportdatoeksist = $db->query($rapportdatoer);
if ( !$rapporteksist ) die('Database Error: '.$db->error);
while($row = mysqli_fetch_array($rapportdatoeksist))
{
print_r($row);
}
The errors I am getting are:
Notice: Array to string conversion for the second select
Database Error: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' fradato IN('Array') OR tildato IN('Array')' at line 1
I'm not an expert in JOIN SELECT queries. This is using MariaDB 10.3.12 with PHP7.2
var_dump available at: https://www.lokalradio.no/rapport/gramo/datohenttest.php
Notice that $periodedatoer is an array of array. Each element inside has 1 key of dato (as you var_dump displays).
So use array-column to get the values and then implode as:
$rapportdato = implode("','", array_column($periodedatoer, "dato"));
Now you can use $rapportdato in your second query as:
$rapportdatoer = "select fradato, tildato from gramorapportlogg WHERE fradato IN('" . $rapportdato . "') OR tildato IN('" . $rapportdato . "')";
Related
I have an array like below and that array refers to another data on my db,
Array
(
[0] => 4
[1] => 1
[2] => 15
[3] => 1
[4] => 1
)
how do I want to select value no 1, and get the most top value from table that has value=1?
I use in_array(1,$array) but it does not output anything. Below are my coding;
$sql = " Select * FROM table_name WHERE staff_id='".$_SESSION['staff_id']."' ORDER BY table_name DESC ";
$result=mysqli_query($conn,$sql) or die(mysqli_error());
$leave_id= array();
while($row_permohonan=mysqli_fetch_assoc($result_permohonan))
{
$leave_available = $row_permohonan['leave'];
$leave_id[] = $row_permohonan['leave_id'];
} mysqli_free_result($result_permohonan);
//echo '<pre>'; print_r($leave_id); echo '</pre>';
if(in_array(1,$leave_id)){
echo $leave_available .'/'.$row['total_leave'];
}
I think you have missed $result.
$sql = " Select * FROM table_name WHERE staff_id='".$_SESSION['staff_id']."' ORDER BY table_name DESC ";
$result=mysqli_query($conn,$sql) or die(mysqli_error());
$leave_id= array();
while($row_permohonan=mysqli_fetch_assoc($result)) //Here
{
$leave_available = $row_permohonan['leave'];
$leave_id[] = $row_permohonan['leave_id'];
} mysqli_free_result($result); //And here
First, your code is not clear. You are using:
echo $leave_available.'/'.$row['total_leave'];
Where "$row" is not set anywhere into your code. If you have put on top of your code:
ini_set("display_errors", "On"); error_reporting(E_ALL);
PHP would break with a fatal error if you are executing the code exactly as you gave it. But I assume that you set it somewhere else into your code.
2nd: You should return the data you want into your MySQL query. If you just want to know if there is at least 1 row into your database having leave_id=1 and staff_id=$_SESSION['staff_id'], then add this condition in your SQL query:
$sql = "Select COUNT(*) AS nbrows FROM table_name WHERE staff_id='".$_SESSION['staff_id']."' AND leave_id=1 ORDER BY column_name DESC ";
And then: you have to use "ORDER BY" statement with a column name and not the table name.
Using "in_array": as you are matching "1", I would recommend you to add "true" as 3rd parameter of this function (force using "strict comparison" in case sensitive and type matching):
in_array(1, $array, true)
Then, you'll also have to cast your $leave_id result:
$leave_id[] = (int)...
(int) cast the string value to integer (as PHP results from database are almost strings)
I have some code that is supposed to show certain things depending if a radio button is pressed.
When no is selected, it hides entries in a table with a status_id of 15.
When yes is selected, it shows entries in a table with a status_id of 15
if($_REQUEST['statusVal']=="") {
$condition1= " AND MO.status_id <>'15'";
}
if($_REQUEST['statusVal']=="archive") {
$condition1 =" ";
} if($_REQUEST['statusVal']=="all") {
$condition1= " AND MO.status_id <>'15'";
}
$mode = $_REQUEST['mode'];
$mode_toggle = $_REQUEST['mode_toggle'];
$sql="SELECT MO.ssrs_id,MO.member_id,MO.assr_user_id,MO.product_id,MO.component_id,MO.status_id,MO.summary,MO.priority,V.product_name,M.component,T.name,S.statusTitle,S.flag_id FROM ".ASSR_SSRS." MO,".PRODUCTS." V,".PROD_COMPONENTS." M,".ASSR_USER." T ,".ASSR_STATUS." S WHERE MO.product_id=V.product_id AND MO.assr_user_id=T.assr_user_id AND MO.component_id=M.component_id AND MO.status_id=S.status_id AND MO.member_id='$memberID' ORDER BY MO.lastupdate DESC".$condition1;
$row_count = getRowCount($sql);
$sql .= $GLOBALS[sql_page];
$result=mysql_query($sql) or die(mysql_error());
I get this error when it is shown on the web browser (using latest Chrome)
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 'AND MO.status_id <>'15' LIMIT 0,10' at line 1
Please help me
This is your query
$sql="
SELECT
MO.ssrs_id,
MO.member_id,
MO.assr_user_id,
MO.product_id,
MO.component_id,
MO.status_id,
MO.summary,
MO.priority,
V.product_name,
M.component,
T.name,
S.statusTitle,
S.flag_id
FROM ".ASSR_SSRS." MO,".PRODUCTS." V,".PROD_COMPONENTS." M,".ASSR_USER." T ,".ASSR_STATUS." S
WHERE
MO.product_id=V.product_id
AND MO.assr_user_id=T.assr_user_id
AND MO.component_id=M.component_id
AND MO.status_id=S.status_id
AND MO.member_id='$memberID'
ORDER BY MO.lastupdate DESC".$condition1;
Here you are appending the dynamic condition at the end and this makes the query invalid when you have some conditions for $condition1 something as
order by MO.lastupdate DESC AND MO.status_id <>'15' and hence the syntax is wrong
It should be as
$sql="
SELECT
MO.ssrs_id,
MO.member_id,
MO.assr_user_id,
MO.product_id,
MO.component_id,
MO.status_id,
MO.summary,
MO.priority,
V.product_name,
M.component,
T.name,
S.statusTitle,
S.flag_id
FROM ".ASSR_SSRS." MO,".PRODUCTS." V,".PROD_COMPONENTS." M,".ASSR_USER." T ,".ASSR_STATUS." S
WHERE
MO.product_id=V.product_id
AND MO.assr_user_id=T.assr_user_id
AND MO.component_id=M.component_id
AND MO.status_id=S.status_id
AND MO.member_id='$memberID'
".$condition1."
ORDER BY MO.lastupdate DESC";
You are adding condition after ORDER BY clause, which is error.
Following is the corrected SQL:
$sql="SELECT MO.ssrs_id,MO.member_id,MO.assr_user_id,MO.product_id,MO.component_id,
MO.status_id,MO.summary,MO.priority,V.product_name,M.component,T.name,
S.statusTitle,S.flag_id FROM ".ASSR_SSRS." MO,".PRODUCTS."
V,".PROD_COMPONENTS." M,".ASSR_USER." T ,".ASSR_STATUS." S WHERE
MO.product_id=V.product_id AND MO.assr_user_id=T.assr_user_id AND
MO.component_id=M.component_id AND MO.status_id=S.status_id AND
MO.member_id='$memberID' " . $condition1 . "ORDER BY MO.lastupdate DESC";
I'm building a WordPress website.
With a MySql Query I load all companies from my database that have a specific brand:
$results = $wpdb->get_results("SELECT * FROM $wpdb->postmeta WHERE meta_key = 'brand'")`
brand is an array so I search for a specific value in array:
$brand = $_GET['brand'];
$brandNeedle = $brand;
if(in_array($brandNeedle, $brand[0]))
Now, my client asked to list all users that are connected to all those companies that are selected.
So I need to create a new query. Fortunately the users all have a field added to their colum that tells me where they are working.
So I thought: what if I create a array that holds all companynames that are queried from the companylist.
With this array I can select all users who have the company name in their column
So I think the query should be something like this:
SELECT * FROM $wpdb->usermeta WHERE meta_key='company' AND meta_value ='THE ARRAY'
But this doesn't work obvious because it can't search with an array in an array.
I can't seem to figure it out. Any ideas?
-- EDIT --
Okay so I did the implode function and I do something wrong:
$brand_array = array();
for($i=0; $i <count($results); $i++){
$result = $results[$i];
if ($i % 2 == 0){
$oddeven = 'even';
}else{
$oddeven = 'odd';
}
$post_id = $result->post_id;
$company_name = get_post_meta($post_id, 'company_name', true);
$brand_array[] = $company_name;
}
print_r($brand_array);
$imploded = implode(',',$brand_array);
$query = "SELECT * FROM $wpdb->usermeta WHERE meta_key='company' AND meta_value IN $imploded";
$persons = $wpdb->get_results($query);
This gives me the results I need (the two companies in a new array)
and gives me the following error:
WordPress database error: [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
'brand1,brand2' at line 1]
SELECT * FROM wp_usermeta WHERE meta_key='company' AND meta_value IN
brand1,brand2
Have you thought about using the "IN" clause on MySQL?
https://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_in
You would probably have something like:
$query = "SELECT * FROM ".$wpdb->usermeta." WHERE meta_key='company' AND meta_value IN ('".implode("', ', $brand)."')";
(edit: forgot to add the parenthesis around the "in" content)
Search for mysql for IN clause
SELECT * FROM $wpdb->usermeta WHERE meta_key='company' AND meta_value IN implode(',',$brand_array)
I have a SELECT statement that pulls a limited number of items based on the value of one of the fields. (ie ORDER BY rate LIMIT 15).
However, I need to do some comparisons that and change the value of rate, and subsequently could alter the results that I want.
I could pull everything (without the LIMIT), alter the rate, re-sort, and then just process the number that I need. However, I don't know if it's possible to alter values in a php result array. I'm using:
$query_raw = "SELECT dl.dragon_list_id, dl.dragon_id, dl.dragon_name, dl.dragon_level, d.type, d.opposite, d.image, dr.dragon_earn_rate
FROM dragon_list dl
LEFT JOIN dragons d ON d.dragon_id = dl.dragon_id
LEFT JOIN dragon_rates dr ON dr.dragon_id = dl.dragon_id
AND dr.dragon_level = dl.dragon_level
WHERE dl.dragon_id IN (
SELECT dragon_id
FROM dragon_elements
WHERE element_id = 3
)
AND dl.dragon_list_id NOT IN (
SELECT dh.dragon_list_id
FROM dragon_to_habitat dh, dragon_list dl
WHERE dl.user_id = 1
AND dh.dragon_list_id = dl.dragon_list_id
AND dl.is_deleted = 0
)
AND dl.user_id = " . $userid . "
AND dl.is_deleted = 0
ORDER BY dr.dragon_earn_rate DESC, dl.dragon_name
LIMIT 15;";
$query = mysqli_query($link, $query_raw);
if (!$query) {
echo "DB Error, could not query the database\n";
echo 'MySQL Error: ' . mysqli_error($link);
exit;
}
$d = mysqli_fetch_array($d_query);
Well, after a lot of research and some trial and error I found my answers....
Yes, I CAN alter the result rows using something like:
$result['field'] = $newvalue;
I also learned I could reset the pointer by using:
mysqli_data_seek($d_query,0);
However, when I reset the counter, I lost the changes I made. So ultimately, I'm still a little stuck, but individually I had the answers.
I have the following block of code which is expected to return the count
$sql = "SELECT sum(count) as count
FROM multipleowners WHERE owner = ? " . $localityquery;
$queryarray = array($owner, $locality);
$query = $this->db->query($sql, $queryarray);
if ($query->num_rows() > 0)
{
$result = $query->row_array();
$count = $result['count'];
}
But I am getting empty values when i try to print $count.
I have used print_r($this->db->last_query()); and I got the following query,
SELECT sum(count) as count FROM multipleowners WHERE owner = 'Davenports Harbour Trustee (2012) Limited' and locality = 'Auckland Central'
When I executed this query directly onto my Postgresql IDE i got the output of count as 2.
What and where could this query be gone wrong ? I doubt the existence of ( and ) in the WHERE clause. How do I fix this ?
Update
When I enabled the profiler I got the following query,
SELECT sum(count) as count
FROM multipleowners WHERE owner = 'Davenports Harbour Trustee (2012) Limited' and locality = 'Auckland Central'
So obviously the problem exists on the ( and ) !!
Bingo!! I have added the following line before I passed the variable $owner to the query and it worked,
$owner = html_entity_decode($owner);
Have you tried doing select sum(db.[count] as icount because in the language count could be a reserved word as well. so bracket the field or rename it, try to not name the new output as count..