Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Is it possible to CREATE a table in mysql with the the Column names being defined as php variables?
I have an array called $stretch[];
It is an array of years from one point to another eg: 2005-2020.
I would like to assign this array as column names for the database columns within the MySQL DB.
A simple question I know. But I cant seem to find an answer anywhere within the site.
You could foreach over the array and build a string, or assuming they are the same type and length (which is important to know):
$columns = implode(" VARCHAR(255),", $stretch) . " VARCHAR(255)";
// if numeric column names need to be escaped use backticks
$columns = "`" . implode("` VARCHAR(255), `", $stretch) . "` VARCHAR(255)";
$query = "CREATE TABLE table_name ($columns)";
Yields something like:
CREATE TABLE table_name (2020 VARCHAR(255),2021 VARCHAR(255))
Or:
CREATE TABLE table_name (`2020` VARCHAR(255), `2021` VARCHAR(255))
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have multiple tables all with different columns.
For example: Table1 has the columns (Username, Group, Car, pet). Table2 has the columns (Username, Extra_groups, Vehicle, animals)
I want to Return the username that has 'Red' anywhere in anywhere in the 2 tables minus the username field of course. Now becuase each table has different columns im describing the table to find what they are then doing the following query however it doesn't want to work.
SELECT Username FROM $Table WHERE $column LIKE '%$search%'
What is the best way to query the columns without knowing what they are going to be and omit one of them from the search.
"SELECT Username FROM " . $Table ." WHERE ".$column . " LIKE '% " . $search. "%'"
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In my MySQLi database, I have 2 columns in the table. One is 'author' and the other is 'books'. I have a variable called '$author' and an array called '$books' which contains unknown numbers of values.
I want to populate column 'books' with values inside array '$books' and in the other column variable '$author' which will remain constant in all rows.
Please help. It will be even more appreciated if you provide it in procedural way instead of OOP.
Assuming $author is a string and $books is an array of integers,
$sql = "INSERT INTO your_table (books,author) VALUES(";
foreach($books as $book_data){
$sql .= "($book_data,'$author'),";
}
$sql = rtrim($sql, ",") . ")";
//Execute the query
This will insert every $books variable as a new row with the constant $author.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to set the default username as like wechat which have something like wxid_1234
so can we set default username in our mysql table?
There is a table named 'username' and have two columns: 'username' and 'phone'. Since 'username' needs to be set something like foo_123 and may be auto increased with foo_124, foo_125 etc. It could be changed afterwards.
So is it possible to set the username like that?
if not what is the other method to do it.
Yes, you can do that, but it would be two queries, and you would have to have an autoincrementing user_id as another column:
$sql = $dbo->prepare('INSERT INTO users(username, phone) values("temp", ?)');
$sql->execute(array($phone_number));
$id = $dbo->lastInsertId();
$sql = $dbo->prepare("UPDATE users SET username=? WHERE id=?");
$username = "foo_" . $id;
$sql->execute(array($username, $id));
Consider the following approach:
Create an id column set to int and AUTO_INCREMENT
Create a username column set to VARCHAR with a DEFAULT value of foo or foo_
Create a phone column set to VARCHAR
Then concatenate the id and username like this: (Using PHP)
$username . "_" . $id
or
$username . $id
depending on which method you'll want to use.
which will produce foo_1 foo_2 foo_3 etc. respectively. The numbers will increase automatically from thereon.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
The array is json_encoded and stored in another table. Now I want to create a mysql View with MERGE type, and json_decode the array into the View's columns. Is this possible? If yes, How?
If you plan to do that stuff NOT in the database and just use PHP it will be easy. You just have to select the encoded array, decode it, parse it to columns (or any valid SQL), store it in an string and perform this SQL string, than you have what you want.
$sql_array = 'select json_array from tbl';
//use mysql-query/fetch/execute whatever to get your data
//use json_encode to get your PHP $columns = array()
$columns = json_encode(...);
$sql_cols = 'select null';
//iterate through your PHP array()
foreach($columns as $column) {
//make the row-value to columns
$sql_cols .= ', '.$column;
}
$sql_cols .= ' from tbl';
//use mysql-query/fetch/execute whatever to get your data
This should do it.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
How can I post only one word into the database.
I only need to insert one word from the values entered by a user.
For example: this is my car to insert only the word this
Here is my present code:
$write=mysql_query("INSERT INTO `staff_description` (`id`, `staff_name`, `description`, `description2`,`writer`, `date_stamp`) VALUES ('$id', '$staff_name', '$description', '$description2', '$user', '$date_stamp');") or die(mysql_error());
please assist
If you are looking to take only 1 word from user inputs and insert it into your database you could use explode() function as follows:
$description1 = $_POST['description1'];
$arr = explode(' ',$description1 ); // this will explode the string after every space
echo $arr[0]; //this is the first word
echo $arr[1];
echo $arr[2];
you could use this to insert into database like :
insert into database VALUES($arr[0])
// this will take the first word from the user input
If your table has only 1 column then you can simply use the INSERT query. If your table has multiple rows there can be 2 cases:
The row already exits - In this case you will have to use the UPDATE query.
The row doesn't exist - You may insert "1 word" into any column using the INSERT query. The other columsn will take default values which you have defined while creating the table OR you can simple add blanks if you plan to use those columns later.