I have a form wich permits the users to leave inputs unfilled. I register those fields as decimal(2) "0.00" in the database.
After the form is inserted in the database, I want to permit the users to edit that form.
My question is, for performance's sake, how do I select only the columns different than 0.00 for that specific row corresponding to the user's form input?
By the way, I am using CodeIgniter's Active Record functionality but I don't get upset if I get what I need using a string sql query :D
A pseudocode I imagine:
select _columns-greater-than-zero_ from _table-name_ where id=_row-id_
So you want to select a variable list of columns based on what value a specific row has in that column? Why would you want to do that? It would not improve performance at all, and in fact, the extra checks it would need to run against the values of those columns would make the query slower.
If you're trying to figure out whether or not to display the column on a page, just add some conditions to your code to check if the value in the returned row equals 0.
Related
Using PHP and mySQL, I need to add multiple values in 2 mysql tables : the first table would have the more important informations and the second one would have the less important informations about each items.
To be more clear, ONE element would have his informations split in two tables.
(I need this for some reasons but two of them are : having the first table the less weight possible, and the second table would store datas that will be erase after a short time (meanwhile the first table keeps all the datas it stored).)
In the best scenario, I'd like to add a row in each table about one item/element with the same id in each table. Something like this :
Table 1 id|data_1_a|data_1_b|...
Table 2 id|data_2_a|data_2_b|...
So if I add an element which get the ID "12345" in the table 1, it adds the datas in the table 2 with the same ID "12345".
To achieve this, I think of two solutions :
Create the ID myself for each element (instead of having an auto_increment on table 1). The con is that it would probably be better to check if the ID doesn't already exist in the tables everytime I generate an ID...
Add the element on table 1, get its ID with $db->lastInsertId(); and use it to add the element's datas on table 2. The con is that I have to add one element by one element to get all the IDs, while most of the time I want to add a lot of elements (like one, two or three hundreds !) at once
Maybe there's a better way to achieve this ?
lastInsertId() reports the first value generated by the last INSERT statement executed. It's reliable to assume that when you insert many rows, they are given consecutive id values following that first value. For example, the MySQL JDBC driver relies on this assumption, so it can report the set of id values generated.
This assumption breaks only if you deliberately set innodb_autoinc_lock_mode=2 (interleaved). See https://dev.mysql.com/doc/refman/8.0/en/innodb-auto-increment-handling.html for details about that.
But if it were my task, I would still choose to use a single table. When you find you don't need some of the columns anymore, use UPDATE to set them to NULL. This will eliminate the problems you're facing with assuring the same id is used across two tables.
What would be the best way to create a filter feature in PHP so the user can select which records they want displayed in a table.
For example, a database of addresses. I want to add a feature so the user can select which county they want displayed in a table (A filter feature).
I plan to just run a for each to loop through each of the rows and add the county to a checkbox group so then the user can check which of the counties they want to be displayed then I can base my MySQL query on that but I figures it would take time especially since I'd be having 5000 or more records.
What would be the most convenient way to achieve this and is there a command or a feature in PHP to get all the unique values in a column so I can list them in the filter box?
Focus more on the SQL aspect of the problem. Try:
SELECT DISTINCT column_name FROM table_name;
Also, you could make your database such that the countries are foreign keys to another COUNTRY table. this will reduce a huge amount of redundancy as the database is not filled with repeated mentions of same names.
NOTE: Never retrieve all the entries from the database and run for each loops, try to retrieve as few rows as possible from the database.
I want to create a dynamic number display of texbox, there will be a a textbox and a submit button, then the numeric value of my first textbox shall display also the number of textbox. Which will allow me to input my records in my database. I am using MySQL for my database and how would I insert the records to my table if the first set records of my textbox is not the same as the second (example I input 3 on my first textbox then three textbox shall appear and a submit button, on my second set of records I decided to have 6 on the first textbox then six textbox shall appear in the page) my question again how am I going to do this?
If ever I use array in fetching records and indexing the value of my dynamic textbox, what type of array shall I use to this? How will I going to name the value of my textbox dynamically? and lastly the I wanted the restrictions also that will set the maximum number of textbox.
Ok. Multiple textboxes, let's call them columns. The first record the user enters in each row is the number of columns they require. The submit button would just call a javascript function to create the required amount of columns for that row on the page. This would have the restriction as to how many can be created and the PHP would also ignore extras if the user works out how to cheat it.
Jumping to your last question (because the second question needs this info). You have decided what the maximum number of columns is so the easiest way if that number isn't large would just be to create a table that can satisfy that many columns. Some fields would be NULL on some rows.
It doesn't matter that the number of columns is different when you want to insert because, for each row, you expect up to a certain amount and only deal with the values you are given, those missing are NULL. When you come to output them you only output those that aren't NULL.
The bit about using an array in fetching records and indexing doesn't make sense to me sorry, but I suspect you're fishing for what php database class to use get the data back from the database? I think the trend is mysqli. Personally I use a bespoke one.
Hello I have a mysql database and all I want is basically to get a value on the second table from a first table query
I have figured something like this but is not working.
select src, dst_number, state, duration
from cdrs, area_code_infos
where SUBSTRING(cdrs.src,2,3) = area_code_infos.`npa`;
Please help me figure out this. I have tried in PHP to have multiple queries running one after the other but when I loaded the page after 45 minutes of wait time I gave up.
Thanks,
I assume the tables are farily big, and you are also doing an unindexed query.. basically substring has to be calculated for every row.
Whenever you do a join, you want to make sure both of the joined fields are indexed.
An option would be to create another column containing the substring calculation and then creating an index on that.
However, a better option would be to have an areaCodeInfosID column and set it as a foreign key to the area_code_infos table
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.