choose rows where 10 columns - php

I want to get a rows using one string, but this string is used to validate more than 1 column, exactly 5, for example
-----------------------------------------------
| id | code | 1 | 2 | 3 | 4 | 5 |
-----------------------------------------------
| 01 | Number | 5 | 7 | 10 | 21 | 1 |
| 02 | String | v | q | s | f | w |
| | | | | | | |
| | | | | | | |
| | | | | | | |
-----------------------------------------------
I want to make SELECT mysqli where using 5 columns ( 1, 2, 3, 4, 5 ) if i use looping method, it will waste a lot of time
i have tried the problem with this code
$value = "q";
for ($x = 1; $x <= 5; $x++) {
$cek = mysqli_query($con,"SELECT * FROM table where $x='$value' and id='02'")
if(mysqli_num_rows($cek) == 1) {
echo "success";
} else {
echo "fail";
}
}
I use this code successfully, but the process is too long
is there an easier and more practical way to overcome this problem? thank you

As has been pointed out in comments, your database design is sub-optimal. You may want to reconsider and change it. Also, having column names be 1, 2, etc. is a very bad idea. At least have them start with a letter. Better yet, give them some reasonable names indicating what data those columns actually contain.
Now, assuming you figured out what your column names should really be, you can use a simple IN operator to get your data. The usual/common way of using it is
WHERE some_column IN ('value1', 'value2', ...)
Yet, there's nothing preventing you from using it the other way around:
WHERE 'some_value' IN (column1, column2, ...)
This way, your SQL statement becomes something like this:
SELECT *
FROM table
WHERE '$value' IN (column1, column2, column3, column4, column5)
AND id='02'
Now, if you have your column names in an array, you can do it this way:
$testColumns = ['column1', 'column2', 'column3', 'column4', 'column5'];
$value = "q";
$inColumns = implode(',', $testColumns);
$sql = "SELECT * FROM table WHERE '$value' IN ($inColumns) AND id='02'";
$cek = mysqli_query($con, $sql);
UPDATE: If your column names are non-standard, e.g. the aforementioned 1, 2, etc., or have spaces in them, then simple implode will not work, because you need to back-quote each column name. In that case, you would need something like this:
$inColumns = implode(',', array_map(function($c) { return "`$c`"; }, $columns));

Related

do the math for each data in database sql post result to database in php

I'd like to fetch data from my 2 sql database and do some math and post the result in database
let's say my table1 is like this
+---+---+----------------------------+
| A | B | C |
+---+---+----------------------------+
| 2 | 9 | result from A*B*D*E in php |
| 1 | 8 | result from A*B*D*E in php |
| 4 | 7 | result from A*B*D*E in php |
| 3 | 6 | result from A*B*D*E in php |
| 6 | 5 | result from A*B*D*E in php |
| 6 | 5 | result from A*B*D*E in php |
| 5 | 4 | result from A*B*D*E in php |
+---+---+----------------------------+
and my table2 is like this
+---+----+
| D | E |
+---+----+
| 1 | 9 |
| 2 | 7 |
| 3 | 8 |
| 4 | 6 |
| 5 | 5 |
| 6 | 3 |
| 7 | 2 |
+---+----+
so far what i've done
// database connection
include_once("config.php");
// Query
$query = mysqli_query($conn, "SELECT * FROM table1");
$query2 = mysqli_query($conn, "SELECT * FROM table2");
//Source1
while($user_data1 = mysqli_fetch_array($query))
{
$A[] = $user_data1['A'];
$B[] = $user_data1['B'];
}
//Source2
while($user_data2 = mysqli_fetch_array($query2))
{
$D[] = $user_data2['D'];
$E[] = $user_data2['E'];
}
foreach (array_combine($A, $B) as $ValueA=> $ValueB)
{
foreach (array_combine($D, $E) as $ValueD=> $ValueE)
{
$result1 = $ValueA*$ValueB*ValueD*ValueE;
$val = 0.123;
$result2[] = $result1*$val;
}
$final result = min($result2);
echo round($final result, 2);
unset($result2);
}
I haven't inserted the database yet
still echoing for debug if the math is correct
somehow this code found some bug
for example using my database the final result only echo/showing 6 math result
because in table1 row 5 and 6 has same data
btw of course in my table1 and 2 has primary key
To change C in this case, you don't even need PHP. To UPDATE a value in MySQL with multiple tables just add them with a , when selecting the tables, like this:
UPDATE table1,table2 SET C = table1.A * table1.B * table2.D * table2.E WHERE C IS NULL;
Executing this code once will update all rows so that C = A*B*D*E as wanted where C is not yet set or NULL. If you want to update all rows you can just remove the WHERE condition
Note: Sometimes (at least for me) SQL will give a warning when having no WHERE condition in the SQL query. To bypass this just add WHERE 1=1 at the end.
Just for my understanding: you want to calculate a value for your calculation you need some data from table 1 that is clear, but also from table2 But which one? I guess you want to use the data from the same row ( so row 1 from table1 and row 1 from table2, row 2 from table 1 and row 2 from table2 ) right? Now you have an problem because when you make a select * from table You do not know in which order they give back from your database. Most time it may be the same order as you have input them, but there is no garantie. You have sayed you have an primary key on each table, how have you defined them? I guess you may have a id column, so you can join your table on that id?

return '-' dash if value doesn't exist in two tables in mySQL and PHP

I'm quite new with databases and stuck with this situation.
My databases data updates from another URL. The local database is updated once per week, so I can't be sure when the data changes or does all of the data exist in all of the tables.
That's why I need to compare Spec_Names and if the Spec_Name doesn't exist i.e. table_2, the program should print a dash as an indicator for the user.
table_1
+----------+-----------+--------------+
| category | Spec_Name | Spec_Value |
+----------+-----------+--------------+
| Engine | x | 111111111111 |
+----------+-----------+--------------+
| Engine | y | 222222222222 |
+----------+-----------+--------------+
table_2
+----------+-----------+--------------+
| category | Spec_Name | Spec_Value |
+----------+-----------+--------------+
| Engine | x | 111111111111 |
+----------+-----------+--------------+
So, the Spec_Name.y wouldn't exist in table_2.
In that case, I'd like to show this for a user:
+---------+----------------+----------------+
|Spec_Name| car1.Spec_Value|car2.Spec_Value |
+---------+----------------+----------------+
| x | 111111111111 | - |
| y | - | 55555565 |
| z | 333333333333 | 77777777 |
etc.
Currently I loop throught my data with PHP loops like this one:
function printCarData($conn, $table){
$query = "SELECT Spec_Name, Spec_Value, SUBSTRING(Spec_Value, 1, 20), TechSpecCategoryName FROM ".$table;
if($result = mysqli_query($conn, $query)){
echo"<ul>";
while($row = mysqli_fetch_assoc($result)){
if($query != $row["TechSpecCategoryName"]){
echo "<li>"."<br>"."</li>";
}
echo "<li>".$row["SUBSTRING(Spec_Value, 1, 20)"]."</li>";
$query = $row["TechSpecCategoryName"];
}
echo"</ul>";
}
}
printCarData($link, $table_name)
I thought to use COALESCE(), but I could not figure out, how to use it in situations as this is.
Also, I thought I could achieve this with PHP, but there are 144 Spec_Names in perfect cases, so it would be a quite long, hacky and expensive function.
Thirdly I thought, is there a way to use template tables in MySQL. So, I would have a table with all possible Spec_Names
It would be great, if anybody could point me the right direction.

Creating a column number with the number in the column

id | ozID | ozDa1 | ozDa2 | ozDa3
-----------------------------------------------
1 | 3,4 | Khar | Lorem | Emre
2 | 1,2 | Imson | Berkay | ade
3 | 2 | abc | Goksel | casc
4 | 5 | teq | Insanlik | fdsc
5 | 1,5,2 | asd | Oyle | asdas
6 | 2,1 | fav | Boyle | dsssa
7 | 3 | qwrewq | Filan | dcsd
I can write the content like:
$partNameWD->ozDa2; // Berkay
But the number at the end can be variable, like this: ozDa4.
At first I get the data from the ozID table, for example: 1,2
What does it mean 1,2?
It points to column numbers: ozDa1 and ozDa2
Access to this number is like this:
$ozColNo = explode(",", $partNameWD->ozID);
foreach (ozColNo as $ozColId) {
echo ozColId." - "; // 1 - 2
}
So what is my problem?
How do I get data from the column with these numbers? So, how can I print the data from one and the second
It has to be transformed: ozDa1 // Imson and ozDa2 // Berkay
Please can you tell me the method?
Thanks
I'm not 100% sure that I correctly understand the question, but you can get the mapping between "column number" and "column name" in information_schema.columns. So, if you are given a number, you can look up the name using:
select column_name
from information_schema.columns
where table_name = #table_name and table_schema = #table_schema and
ordinal_position = #op;
You can do a group of columns with a slightly more complicated approach (because I assume ordering is important)
select group_concat(c.column_name order by cols.ordering) as columns
from (select 1 as colpos, 1 as ordering union all
select 2, 2
) cols join
information_schema.columns c
on c.ordinal_position = cols.colpos
where table_name = #table_name and table_schema = #table_schema;
Try this.
$output= $partNameWD->{'ozDa' . $ozColId};
echo $output;
Check out the PHP.net site for this
http://php.net/manual/en/language.variables.variable.php
Caution
Further dereferencing a variable property that is an array has
different semantics between PHP 5 and PHP 7. The PHP 7.0 migration
guide includes further details on the types of expressions that have
changed, and how to place curly braces to avoid ambiguity.

How to query based on multiple relations between columns - MySQL?

I have four columns in a properties table: property_id, value, id, material_id.
I also have an array of properties: Array $properties
The schema is a bit complicated, because I want to find the material_id based on the matching properties.
An example:
$properties = array(['property_id'=>1,'value'=>3],['property_id'=>2,'value'=>6],['property_id'=>3,'value'=>4]);
Example table output:
+----+-------------+-------------+-------+
| id | material_id | property_id | value |
+----+-------------+-------------+-------+
| 1 | 1 | 3 | 5 |
| 2 | 1 | 3 | 5 |
| 3 | 1 | 3 | 5 |
| 4 | 2 | 1 | 3 |
| 5 | 2 | 2 | 6 |
| 6 | 2 | 3 | 4 |
| 10 | 4 | 1 | 9 |
| 11 | 4 | 2 | 3 |
| 12 | 4 | 3 | 6 |
+----+-------------+-------------+-------+
Now, I need material_id that satisfies all the properties. How can I do that..? Do I need to use exist statement of MySQL?
Now, for each element in your array you will want to run a statement that looks like this:
SELECT material_id FROM properties WHERE property_id = 2 AND value = 3;
Do you need help on the php code also? You could run a for each loop, but I will need to know what way you are using to communicate with your database for more specifics.
edit
foreach ($properties as $foo => $bar)
{
$sql = 'SELECT material_id FROM properties WHERE ';
foreach ($bar as $key => $value)
{
$sql .= $key .' = '. $value .' AND ';
}
$sql .= 'true';
*run your PDO code on $sql here*
}
On behalf of performance, it's not a good idea to run a query per array's value. If you have an oversized array things can get pretty slower.
So, best solution can be to build a single query including all conditions presented on $properties array:
<?php
$properties = array(['property_id'=>1,'value'=>3],['property_id'=>2,'value'=>6],['property_id'=>3,'value'=>4]);
$qCondition = [];
foreach($properties as $prop) {
$q = sprintf("(property_id = %d AND value = %d)", $prop["property_id"], $prop["value"]);
$qCondition[] = $q;
}
// assuming that your database table name is 'materials'
$sql = sprintf("SELECT * FROM materials WHERE (" . implode(" OR ", $qCondition) . ")");
echo $sql;
Result:
SELECT * FROM materials
WHERE ((property_id = 1 AND value = 3) OR (property_id = 2 AND value = 6) OR (property_id = 3 AND value = 4))
Therefore, you need to run only one single query to get all desired rows.
You can play with suggested solution here: http://ideone.com/kaE4sw

Count same values in different column mysql

I need to count how many times in ripeted the same values in different columns for the same id..
I'll try to clarify with an example:
TABLE:
+-----+-----+-----+-----+-----+
| id | d01 | d02 | d03 | d04 |
+=====+=====+=====+=====+=====+
| 1 | A | A | B | B |
+-----+-----+-----+-----+-----+
| 2 | A | A | A | A |
+-----+-----+-----+-----+-----+
| 3 | B | B | A | A |
+-----+-----+-----+-----+-----+
| 4 | A | A | A | A |
+-----+-----+-----+-----+-----+
| 5 | A | A | A | A |
+-----+-----+-----+-----+-----+
| 6 | B | A | A | A |
+-----+-----+-----+-----+-----+
I need to know how many times the value "B" is repeating for any person (ID)..
Is that possible to do that? RESULTS
+-----+-----+-----+
| id | count B |
+=====+=====+=====+
| 1 | 2 |
+-----+-----+-----+
| 2 | 0 |
+-----+-----+-----+
| 3 | 2 |
+-----+-----+-----+
I was thinking to use the function "SUM" but I have no idea how to display just the single ID.
Thanks in advance, hope the question is clear enough!
If there are only four columns:
SELECT id, (d01 = 'B') + (d02 = 'B') + (d03 = 'B') + (d04 = 'B')
FROM tablename
No there are 31 columns
That's a problem which you can solve in two ways:
Repeat the condition for the other 27 columns :)
Normalize your structure so that each value is dependent on both the id and a numeric value that represents a calendar.
The PHP way
You can also fetch all columns and let PHP solve this for you:
$res = $db->query('SELECT * FROM tablename');
foreach ($res->fetchAll(PDO::FETCH_ASSOC) as $row) {
$id = $row['id'];
unset($row['id']); // don't count the id column
$count = count(array_keys($row, 'B', true));
printf("ID %d: %d\n", $id, $count);
}
Since you seem to be using mysql_*:
// SHOW COLUMNS returns all the columns and constrains of the defined table
// We only need the column names so we will be later calling it by 'Field'
$sql = mysql_query("SHOW COLUMNS FROM table"); //your table name here
$val_to_count = 'B'; //value to count here
$id = 1; //id to search for
$new_sql = 'SELECT id, ';
// In this loop we will construct our SELECT query using the columns returned
// from the above query
while($row=mysql_fetch_array($sql)){
if($row['Field']!='id'){
$new_sql .= ' ('.$row['Field'].' = "'.$val_to_count.'") + ';
}
}
//Removing the last "+ " produced in the select query
$new_sql = rtrim($new_sql,"+ ");
$new_sql .= ' as count FROM table WHERE id = '.$id; //table name here again
// so $new_sql now has an output like:
// SELECT ID, (d01 = 'B') + (d02 = 'B') ... WHERE id = 1
$sql2 = mysql_query($new_sql);
//executing the constructed query with the output below
while($row2=mysql_fetch_array($sql2)){
echo 'ID - '.$row2['id']."<br>";
echo 'Count - '.$row2['count']."<br>";
}
Note:
mysql_* is deprecated, please consider to migrate to mysqli_*

Categories