I need on coding to check a mysql DB to see if it needs to be updated. The DB is for stock info & will only download the info if it is not currently in the DB. I'm trying to figure how to design my code without making some long peice of coding that may be able to be done in a few lines of code.
I currently am using the information_schema UPDATE_TIME info for the table, so I have that info. But where I'm stuck is how to I use php to check if the DB needs to be updated.
So in essence, I'm looking for a code snippet that will check to see if the table needs to be updated but will check for the duplicate info (i.e. if it makes it through an "if" statement & then downloads it anyway, if it is duplicate info, it will not insert it [I can use the date for this]).
Thanks
Think of this :
$result = mysql_query("SELECT * FROM tableName" WHERE ConditionYouHave)
or die(mysql_query("INSERT INTO tableName VALUES ('value1','value2','value3')"));
Do you have some sort of datafeed you can run through to check for new data? If this is the case I would go line-by-line through the file looking for new rows. When you find one, you insert it into your table. If the row already exists just continue on to the next row. This can be done fairly simply in PHP.
INSERT INTO table_name (id, column2)
SELECT {$int_id}, '{$str_column2}'
FROM table_name
WHERE id = {$int_id} AND column2 != 'what ever you want'
ON DUPLICATE KEY UPDATE column2 = VALUES(column2)
table_name is your own table's name.
I assumed you have a id column. And column2 is just an example.
You can validate your row through the WHERE and have whatever condition you wish.
Your question's comment links to this example.
Related
is it possible adding a column into a database table after an insert?
If yes kindly give me an example code for it i don't know how to make of it
yes it is possible for example :
CREATE TABLE my_table (
id integer(10),
fname varchar(50) );
and insert the values :
INSERT INTO `my_table`(`id`, `fname`) VALUES (1,'edverd');
see the value is added here
NOW Alter the Table
ALTER TABLE my_table ADD COLUMN lname varchar(50);
see the table is altered here
but the values in the added column will be null for every row present in that table.
now you can simply add the values in the added column
INSERT INTO `my_table`( `lname`) VALUES ('cullen');
To Answer Your Question:
Yes you totally can.Just an FYI
Insert Is basically Creating a Brand New row.(If there is something already present in the row You use UPDATE)
to add Column to the database the syntax remains the same, if you are using mysql this is something your code should look like
$query="ALTER TABLE Users ADD COLUMN PASSWORD VARCHAR(100)";
$result= $mysqli->query($query);
if(!$result) echo "Failed to Add column TO The database";
BUT:
1.What you are trying to do isn't a good practice.
2.Read More about Php, You can get a course on udemy or just buy books on Php one of which is Learning PHP MYsql and Javascript By Robin Nixon, or just read more from the php manual, or mysql manual.
3.Nobody is Rude here, but lack of effort is something nobody appreciates.
4.Lastly don't get disheartened,Work Hard
working on the PHP project related to web scraping and my aim is to store the data into the mysql database,i'm using unique key index on 3 indexes in 9 columns table and records are more than 5k.
should i check for unique data at program level like putting values in arrays and then comparing before inserting into database ?
is there any way so that i can speed up my database insertion ?
Never ever create a duplicate table this is a anti SQL pattern and it makes it more difficult to work with your data.
Maybe PDO and prepared statement will give you a little boost but dont expect wonders from it.
multible INSERT IGNORE may also give you a little boost but dont expect wonders from it.
You should generate a multiinsert query like so
INSERT INTO database.table (columns) VALUES (values),(values),(values)
Keep in mind to keep under the max packet size that mysql will have.
this way the index file have to be updated once.
You could create a duplicate of the table that you currently have except no indices on any field. Store the data in this table.
Then use events to move the data from the temp table into the main table. Once the data is moved to the main table then delete if from the temp table.
you can follow your updates with triger. You should do update table and you have to right trigger for this table.
use PDO, mysqli_* function, to increase insertion into database
You could use "INSERT IGNORE" in your query. That way the record will not be inserted if any unique constraints are violated.
Example:
INSERT IGNORE INTO table_name SET name = 'foo', value = 'bar', id = 12345;
I hope this is not a dumb question but I really can't find anything on the net as well as anything in the export settings in phpmyadmin.
My question: Is it possible to export data in phpmyadmin based on matching data to column names. For instance if you export a database or table sql statements are written like
INSERT INTO table VALUES ('1','2','3')
What I want to know if it is possible to export the data so the insert statements read
INSERT INTO table (number1,number2,number3) VALUES ('1','2','3')
The idea is that as I am writing a database I want to be able to export at certain times. But since I am constantly adding new fields in the tables I want to be able to import an old export and still be able to match the columns to the data. That way existing fields contain the data and new ones are just left empty.
Is this possible in just php?
Yes, it it possible. You should use the INSERT format that is similar to update:
INSERT INTO table SET number1 = '1', number2 = '2', number3 = '3';
Phpmyadmin knows how to do this, just search for "Update style inserts"
In "View dump (schema) of table"
Check the "Complete inserts" Option
you will get this
INSERT INTO table (number1,number2,number3) VALUES ('1','2','3')
Firstly please excuse my lack of knowledge for SQL, I have only done basic inserts before.
I am currently improoving a little system that I have, and I want to insert some data that is obtained via _GET (php) into two tables. My problem is as follows.
My first table (table_one) has an auto incrementing value called "id", which I need to obtain, and post over to my second table (table_two).
Because of the way data will be updated at the later date, the ID in table two, is a reference to the ID that is automatically generated upon insert in table one (hence no ID in the code below). (I will be using the ID in table one to do a for loop for each matching ID instance in table_two)
How can I run one query to update one table, then update the 2nd with the unique id obtained from the first table?
My current code is this...
INSERT INTO table_one (valueone,valuetwo,valuethee) VALUES ('$v1','$v2','$v3')
you can use mysql_insert_id() built in command of php this will give you the id of the recently inserted data
mysql_query("insert into.... ");
$a = mysql_insert_id();
mysql_insert_id() after first query will give you id
I want to insert some data that is obtained via _GET
that's wrong. this data should be obtained via POST
Expanding on #Ujjwal's answer, you can do the same just using SQL.
INSERT INTO table1 (x,y) VALUES ('x','y');
INSERT INTO table2 (t1_id, z) VALUES (LAST_INSERT_ID(), 'z');
See: http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html
I was wondering how can I select a value from a database that a user just entered it into and then add it to another mysql table all in the same script before the script is finished running.
You're probably looking for an insert ... select statement.
If you're talking about adding a value that a user just entered into a form, to something, and then putting that into the database, you should do the addition while in PHP. There's no point in going to the database after you've just inserted the value for this purpose.
If I'm misunderstanding something, please elaborate your question and let us know WHY you would want to figure out a just-inserted database value and do an operation on it, rather than trying to do it before you insert in the first place.
Also, if it's a fairly simple modification consider using an UPDATE statement, not a select --> insert.
Like nash said, you perform a select.
But to get the data from the row that the user just entered, you'll need:
mysql_insert_id()
Which grabs the last ID inserted (this is assuming you have an increment id column)
So assuming just entered his first and last name in a form, you'd insert his first and last name in the database(which i assume you know how since the title of this question is "SELECT a value from MySQL database"), you can get what he just entered by:
$last_id = mysql_insert_id();
If there are no rows on that table yet, then this will return 1. $last_id is now 1 (one).
To select:
SELECT * FROM users WHERE userID = "$last_id"
this will grab what the user just inserted....however, this seems pointless as you can use the variables from the form he just filled
enter code here
In the PHP MySQL module, you normally perform a mysql_select_db() to switch database.
You can insert your data into tables in different databases by switching between them with that function.
However, you can insert data into any table of any database (which the user has access to) by prefixing the database name to the table like so:
INSERT INTO test_db.test_table (`column1`,`column2`) VALUES ('abc',123);
You can use that also to insert data from one table into another using:
INSERT INTO `db1`.`myTable` (`column1`,`column2`) SELECT `column1`,`column2` FROM `db2`.`myTable` WHERE `id`= 5
The WHERE id part should obviously match the id of a row in db2.myTable
If you use doctrine you have the inserted data in the object representing the table and in addition you have primary key assigned for the record inside the object.
Con is doctrine is huge database abstraction layer, so if your application is not big doctrine is hammer for mosquito.
what is the structure of your database? The names of your tables, columns?
Some tutorial that you may want to look at: (grabbed from google)
http://www.phpf1.com/tutorial/php-mysql-tutorial.html
In theory you perform a select, take the data you need and perform an insert.