Placeholder for Column Name - php

I'm writing sql code which gets the name of the columns its supposed to look up of a table from custom php code.
Part of this has to do with location i.e. country ,state, zip, etc. I have it so that on the php code, the user fills out the fields that correspond to these on their tables. However, it is possible that while these are all possibilities they may not have every piece, like the zip code.
If I the php doesn't get anything for the zip column name it throws off the entire code. Null columns are ok with me, but I'm struggling finding a way to make a placeholder for the column. Is there a sql placeholder I can tell the php variable to become if the user enters nothing and is therefore null?
$addressColumn = $_POST["address"];
$stateColumn = $_POST["state"];
$zipColumn = $_POST["zip"];
$cityColumn = $_POST["city"];
$countryColumn = $_POST["country"];
$database = $_POST["database"];
if one of these is not posted on the html page, then the code breaks. suggestions? I can change the value if null to something else if anyone knows of a placeholder value that sql will accept and then as I fetch rows will place null below it

I think that working starting from column names is not a good idea. SQL doesn't concept like stree or ZIP but only stings and number or date.
I suggest you to create a class with all your table columns as properties, putting the business logic in the right place (the PHP), so the method "query" (the one who query the DB) can validate/operate/manage the status of your object.
The PHP that draw the for can get all infos from the class (eg: *get_object_vars* ) instead of DB.
If you're worried from mapping your DB into class you can look at ActiveRecords.

Related

Explain this SQL code in PHP script

I found this piece of code from an answer on Several drop down list input to one field table. It really seems like something I can use in my current project. The current project is about populating a table with two dropdown boxes (which are populated themselves with tables). However, I don't understand what personal changes I need to do in order to make it work. attributes? :attributes? Can someone make sense of it?
$levels = $_POST['level1'][0].",".$_POST['level1'][1];
//Now you have a string called $levels
// Which contains a comma seperated list, to insert into db / one field
//Insert into your table...change your table and field names to real values...
$sql = "INSERT INTO yourTable (attributes) VALUES (:attributes)";
$q = $db->prepare($sql);
$q->execute(array(':attributes'=>$levels));
?>
:attributes is a bound parameter in a PHP prepared statement which inserts a row into a table with an attributes field, setting just that field.

Parsing first line of csv and creating a table inside database

I am downloading new csv's each night using a cron job with PHP. Each csv is normally about the same, possibly one night within a month a field is new. I need to get the new field and append it to the database. I don't know how to get the type of the new field. I saw someone else's question with gettype() but i'm not sure if that would work or not since the data is inside a csv so wouldn't they all be strings when some need to be floats, or ints? How would I go across checking the type?
The second question, is there a way to check if there is not a name inside of a table? For instance, if they add a new field called foo52, and I have foo1 through foo51 in my database, is there a quick way to search for fields that aren't there, or would I have to use a select statement for each one and append it when it's false?
I use MySQL for my database.
Thanks for your help.
The first question on the part about getting the type is the simply try the conversion of the data itself and then seeing if the data is equivalent with a == comparison.
So,
some,data,is,123
After reading in the data you can then try the conversion to various types such as strings, ints, ect...then from that you are able to determine the type of the data.
For the second question you can get the column names by doing:
show columns from db.table_name
Then you can do a simple in_array to test if the new column name is already in the database.
EDIT:
Using array_diff can simplify the finding of the missing/new column names from the CSV.
csv_names = get_csv_column_names();
sql_names = get_sql_column_names();
new_names = array_diff( csv_names, sql_names );
I have found the parsecsv-for-php library very handy for [re|de]constructing CSV data.
For the first question: you can test if it's numeric with is_numeric(). If not, store as string. If yes, create the field as numeric in your database. If you want, you can use regex to check if it's a date or some other datatype you think is required to be stored correctly (i.e. not a a default-text)
for the second question: getting the fieldnames of a table in Postgres is done with the follwoing query
$sql = "SELECT attname FROM pg_catalog.pg_attribute
WHERE attrelid =
(SELECT c.oid FROM pg_catalog.pg_class c LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE c.relname = '$this->tableName' AND n.nspname = 'public')
AND attnum > 0
AND NOT attisdropped";
For MySQL, it should be doable with "show columns from db.table_name".
Once you have the fields, use in_array() to check if it exists already...
Note that: you'd probably need to check for all columns in your CSV if they exist already. If not: add a new colum for it. If yes, leave it as is...

Need to append data in a database column

I'm working on an old legacy system. It breaks all kinds of rules from normalization to common sense, but alas I'm stuck with it. That being said here it goes.
Question:
How would I append a name to a field without getting rid of the existing names in that field? Also, how do I do this over and over again? Same name for the appending, but different names that I need to keep intact that are unique to each row in the database.
Example: Rows as they are in the name column:
"Donnie/Mike/Daniel"
"Donnie/James"
"Steve"
Example: Rows after the script in the name colum:
"Donnie/Mike/Daniel/Dee"
"Donnie/James/Dee"
"Steve/Dee"
I'm thinking SQL will not be enough here and I'll have to write a script. What does SO think? Besides the usual sickening feeling you naturally get from legacy apps.
Here is the syntax in SQL Server:
update table
set field = field+'/'+<newval>
where <i want the new val>
In other databases, the concatenation operation might be:
set field = field || '/' ||
set field = concat(field, '/', )

Setting multiple fields in a database, from just one field

I am working in the confines of a CMS system, which defines certain fields which can be used to make forms for use within the application in PHP.
I am making use of the inputSmartSearch field, which is basically similar to Google suggest.
It allows me to define an SQL query, and then displays records that match as I type in my search.
For my smartsearch, I have chosen it to search through three fields in a different table, and to display those fields concatenated together.
I use define my field like so:
$theinput = new inputSmartSearch($db, "chooseguests", "Choose Guests");
The last parameter is the name of the SQL query to execute.
This works fine, and a guest can be located by searching his last or first name.
However, I have implemented this smartsearch what is meant to be the page to add a sales order.
In each sales order stored in the sales table, distinct from the guest table, I also want to have the firstname and lastname of the guest making the order.
The design of the sales order table has two separate fields for firstname and lastname.
Using smartsearch, I cannot find any way to tokenize the selected input and insert it back into the field.
If I have a smartsearch and can search by firstname or lastname it shows the result as just one fields, and I want it to save the firstname to the firstname field and the lastname to the lastname field.
Each form defined has an include file which defines the function for inserting a record and such, like so:
function prepareVariables($variables){
// if ($variables["webaddress"]=="http://")
// $variables["webaddress"] = NULL;
return $variables;
}
function updateRecord($variables, $modifiedby = NULL, $useUuid = false){
$variables = $this->prepareVariables($variables);
return parent::updateRecord($variables, $modifiedby, $useUuid);
}
function insertRecord($variables, $createdby = NULL, $overrideID = false, $replace = false, $useUuid = false){
$variables = $this->prepareVariables($variables);
return parent::insertRecord($variables, $createdby, $overrideID, $replace, $useUuid);
}
However, I am unsure of how I could modify the insert or update functions to do what I need them to do, or if that is even the correct approach.
Should I be looking for a complex sql query? hidden fields with the content autopopulated from the result of my inputSmartSearch? Something else?
Not sure what's possible with the setup you've got going, but... my first reaction would be to return from the db the names combined with tokens - like - ',' So Smith, John. Then, when getting ready to send back, to split those up ( based upon the known token ). Now where that goes what you've got there, I do not know.
You just need to set the starting value of the smart search manually using the values from the database. Then when the data is posted to the server you can parse and append to the array that is used to store the data in the database. At least that seems like it should work.

PHP/MySQL - How do you determin the field names from a given query result?

Given a result set, how can I determin the actual names of the fields specified in the query (NOT their aliases).
$query = "SELECT first AS First_Name, last AS Last_Name FROM people";
$dbResult = mysql_query($query);
$fieldCount = mysql_num_fields($dbResult);
for ($i=0; $i<$fieldCount; $i++) {
// Set some values
$fieldName = mysql_field_name($dbResult, $i);
}
This example returns field names, but in this example it returns the alias "First_Name" instead of the actual field name "first".
Is it possible to get the actual field name from such a query. Particularly if I am writing a function and have no idea what query will be thrown at it.
If you are using MySQLi:
http://www.php.net/manual/en/mysqli-result.fetch-field.php
The field object has a "orgname" property.
The "classic" MySQL equivalent function doesn't report back the original column names.
Short answer: you don't.
Long answer: Once the dataset is pulled by MySQL and sent back to PHP, the only information PHP now has is the columns, or aliases if you used them. There is no way to look at a result set and determine what the original column names were. You have to switch to another DB driver like mysqli to obtain this info.
Your question doesn't make sense.
What are you going to do if you get a derived column i.e.
select column_a + column_b as order_total from orders;
are you saying you want to know that the original query was column_a + column b ??
if so, you probably need to write a query parser, or get one off the internet.
I think the implementation of that is beyond the scope of your question though :)
I'm not 100% sure about this, but I would say: there is no way.
The MySQL gives you back the result set, nothing more. It does not return the select statement nor any details about it.
So you cannot get the original field names because the server will provide you the information you asked: alias names.
If you don't mind making a second query (and your using MySQL 5 or greater) you can ask information_schema for the names.
Check out MySQL Reference for the details:
SHOW COLUMNS FROM tbl_name;
if you have access to the string of the query you could try a regular expression to parse it.
I'm no regex master but you could chop up the string by looking at the text between 'select' and 'from' then grabbing all the field names as either
field FieldAlias
or
field as FieldAlias
If you're trying to write some functionality to let you know what fields are being fetched for handling updates - the only way to do this correctly is for it to present an SQL-less interface to the code above and manage all SQL generation itself. This is called a data abstraction layer.

Categories