I am trying to insert rows into a MySQL table using PHP/PDO, from two sources, namely :
Another Table (Same database)
PHP
The table I am filling looks like this :
Table Name : data_sink
+-------+-----------+--------------+-----------+
| ID | data1 | data2 | data3 |
+-------+-----------+--------------+-----------+
| 1 | text_1 | aa | 8 |
| 2 | text_2 | bb | 8 |
| 3 | text_3 | cc | 8 |
| 4 | text_4 | dd | 8 |
| 5 | text_5 | ee | 8 |
| 6 | text_6 | ff | 8 |
+-------+-----------+--------------+-----------+
In this, data1 and data2 are taken from another table, however data3 comes from the PHP code. I used the following query to get data from just the table :
$insert_rows = $db->prepare("INSERT INTO data_sink (data1, data2)
SELECT username, usergroup FROM data_origins WHERE <condition>");
I am unsure of how to add data3 into this query. I have found a workaround for this by adding another column to my origin table, and filling all rows with the value "replaceme", and then running the query as follows :
$insert_rows = $db->prepare("INSERT INTO data_sink (data1, data2, data3)
SELECT username, usergroup, temp_value FROM data_origins WHERE <condition>");
I then run an additional query, updating the table wherever data3 is set to "replaceme"
$update_rows = $db->prepare("UPDATE data_sink SET data3 = :data3
WHERE data3 = 'replaceme'");
Is there a single step method of solving this problem, to combine inputs from MySQL and PHP? [NOTE : For every batch of inserts, all rows share a common value for data3]
Could be as simple as
$insert_rows = $db->prepare("INSERT INTO data_sink (data1, data2, data3)
SELECT username, usergroup, ? FROM data_origins WHERE <condition>");
$insert_rows->bindParam(1, $data3);
$insert_rows->execute();
This of course assumes that your $data3 is the same for all rows that are being inserted. It's also possible to do a simple arithmatic operation or function call to ensure that each row gets a different value based on $data3
Related
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?
My job is to cleanse the database from broken store records(records with missing data like a store name or streetname, database example below). However it turns out some of those records have employees linked to them so we can't just delete them.
Question: what kind of query do I need to make sure only the employees of those stores are shown?
I have three tables in which i need to work: user, department and store_users (store_users is a table in which all ID's are packed together to see which user_id belongs to which store_id.)
Example of code
its most likely wrong but its to give some insight in what I am trying to accomplish.(its purely mysql at the moment)
SELECT
*
FROM
gebruiker
WHERE
(SELECT
*
FROM
winkel_adresgegevens
WHERE
winkel_id IN (SELECT
winkel_gebruikers_winkel_id
FROM
winkel_gebruikers)
AND (winkel_naam = '' OR winkel_straat = ''
OR winkel_huisnummer = ''
OR winkel_postcode = ''
OR winkel_plaats = ''));
database example
+--------+---------------+----------+------------+------------------+
|store_id|store_branch_id|store_name|store_street|store_streetnumber|
|-------------------------------------------------------------------|
| 1 | 100 | Jumbo | Hilversum | 14 |
|--------+---------------+----------+------------+------------------|
| 2 | 150 | Lidl | Kerkelanden| 24 |
|--------+---------------+----------+------------+------------------|
| 3 | 105 | | Loosdrecht | |
|--------+---------------+----------+------------+------------------|
| 4 | 200 | Coop | | 14 |
+--------+---------------+----------+------------+------------------+
hence the question: only show employees from the broken records. (the ones with missing data, see store_name, store_street and store_streetnumber)
UPDATE : This is what you want
SELECT *
FROM employees
WHERE store_name IS NULL OR store_street IS NULL OR store_streetnumber IS NULL ;
I have a function that runs and gets the labels from a lookup table for values stored in a particular table. When there is 1 value it displays correctly. However when there is multiple values it only returns the first one. For example:
Lookup table is
| Tel_No| Tel_type |
--------------------
| 1 | Info |
| 2 | Support |
| 3 | Call |
Main table is
| TelephoneCalls |
------------------
| 1,3 |
| 3 |
| 1,2,3 |
The function I have at the moment which works for matching 1 value is
function getMonitoring($monitoring){
$query = "SELECT Tel_type FROM TelephoneCalls Where Tel_no = '$monitoring'";
$result9 =mysql_query($query) or die(mysql_error());
list($Tel_type) = mysql_fetch_array($result9, MYSQL_NUM);
return $Tel_type;
}
How can I get it to list the values like below
If 1, 3 then display Info, Call
If 3 display Call
If 1, 2, 3 display Info, Support, Call
Thanks for any help!
I guess the comments touched upon it, but you really should change your schema to be more of a many-to-many relationship than using CSV values in the fields. If you can't this query should work:
SELECT telephonecalls, GROUP_CONCAT(tel_type)
FROM lookup_table
LEFT JOIN main_table ON FIND_IN_SET(tel_no , replace(TelephoneCalls,' ', '')) > 0
GROUP BY telephonecalls;
#outputs
+----------------+------------------------+
| telephonecalls | GROUP_CONCAT(tel_type) |
+----------------+------------------------+
| 1,2,3 | Info,Support,Call |
| 1,3 | Info,Call |
| 3 | Call |
+----------------+------------------------+
http://sqlfiddle.com/#!2/194fd/1/0
I need to fetch data from table whose mem_count is equal to 2 and then insert that data to another table after last unique_id.
Here is my code :
$mem_count2=mysql_query("SELECT mem_count,mem_id FROM member_status WHERE mem_count = 2 order by mem_id ASC");
if(mysql_num_rows($mem_count2 )==0){
die(mysql_error());
}
else{
$start_value=00;
// $start_value dynamically comes from other table which i am not mentioning here
// $start_value can starts from any number For Eg. 2, 5.
while ($row=mysql_fetch_array($mem_count2)) {
$uniq_code="PHR-" . str_pad((int) $start_value, 2, "0", STR_PAD_LEFT);
//if mem_id already inserted then use IGNORE in INSERT i.e INSERT IGNORE
$cql = "INSERT IGNORE INTO member_code (mem_id, temp_code,unique_code) values ('".$row['mem_id']."','".$row['mem_count']."','".$uniq_code."')";
mysql_query($cql) or exit(mysql_error());
$start_value++;
}
}
It runs smoothly but sometimes i am getting this output:
+------------+-------------+
| mem_id | unique_code |
+------------+-------------+
| 1 | PHR-01 |
+------------+-------------+
| 5 | PHR-02 |
+------------+-------------+
| 3 | PHR-04 |
+------------+-------------+
Some problem is surely in the INSERT IGNORE query!I have made mem_id as UNIQUE. Please rectify this as i am completely stuck over it !
Member_code structure
+------------+-------------+-----+
| mem_id | unique_code | Id |
+------------+-------------+-----+
| 1 | PHR-01 | 1 |
+------------+-------------+-----+
| 5 | PHR-02 | 5 |
+------------+-------------+-----+
| 3 | PHR-04 | 3 |
+------------+-------------+-----+
It is probably because you are getting an error at some point at this line
mysql_query($cql) or exit(mysql_error());
and then your increment variable will be incremented for the next insert. You should test the insert and if doesn't get error you do the increment.
You are using a variable called $mem_count2 throughout your code, when the results are stored in $mem_count.
I have one field in the backend, where I input IDs separated by comma - ID1, ID2, ID3....These are videos in fact. All ids are stored in the field product_videos in the database (as they are typed).
How can I echo these id's on the frontend so they all show for the product?
Storing comma separated data in one data field is a bad idea. It is a real pain to manipulate, so you should really consider revising your db structure.
You haven't shown your data structure, so I'll give a basic example and then explain how it can be improved. My example assumes product_videos is linked to particular users:
table: `users`
| user_id | user_name | product_videos |
|---------|-----------|----------------|
| 1 | User1 | 1,2,3,4,6,7 |
| 2 | User2 | 5 |
You would maybe run a query
SELECT `product_videos` FROM `users` WHERE `user_name` = 'User1'
This would give you one row, with a comma separate value - you would then need to use something like PHP's explode() to convert it into an array and then loop through that array. That is a very bad method (and it will only become harder as you try to do more advanced things).
Instead, it would be easier to use a link table. Imagine:
table: `users`
| user_id | user_name |
|---------|-----------|
| 1 | User1 |
| 2 | User2 |
table: `videos`
| video_id | user_id |
|-----------|---------|
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 1 |
| 5 | 2 |
| 6 | 1 |
| 7 | 1 |
In this example, each video is a separate row in a db table (and each video is linked to an existing user). Each row is readily able to be handled independently. This makes it really easy to handle extra data for each video, such as a title, runtime length, date of uploading, etc.
You would then need to run a JOIN query. e.g.
SELECT `videos`.`video_id` FROM `videos`
INNER JOIN `users` ON `users`.`user_id` = `videos`.`user_id`
WHERE `users`.`user_name` = 'User1'
In PHP, you would do something like:
$q = mysql_query("SELECT `videos`.`video_id` FROM `videos` INNER JOIN `users` ON `users`.`user_id` = `videos`.`user_id` WHERE `users`.`user_name` = 'User1'");
while ($row = mysql_fetch_assoc($q)) {
echo "VIDEO ID = " . $row["video_id"] . "<br/>";
}