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.
Related
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))
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 have some code here for a song request program. And it works just fine other than the user has to use surrounding quotes for an exact match. I am wondering how I would go about having the php add the quotes so the user can type a band or song title as normal without having to read the small help notice saying to use quotes?
You can concate quotes in after if you like.
$termToSearch = '"' . $termFromUser . '"';
$query = 'SELECT * FROM table WHERE song = :song'
$statement = $this->db->prepare($query);
$statement->bindValue(':song', $termToSearch);
$statement->execute();
$statement->closeCursor();
Just use "=" instead of "LIKE"
SELECT * FROM table WHERE column = '$searchterm'
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
Can $var[$x]['seeds'] be converted into $var? In mysql statement like the one below, how can I use it?
$length=9;
$x=0;
while ($x<$length){
$query="Select fruits in ('$var[$x]['seeds']') from edible";//This query will go 9 times different seeds.
$x++;
}
Please help me.
I hope that I understand your question correctly:
$query = "SELECT seeds FROM edible WHERE seeds IN ('" . $var[$x]['Apple'] . "')";
If you now perform: echo $query; you will see how the query looks like. But your question is not really clear in what your exactly want.
Also if you want to use variables in query, then look at parameterized queries. This prevents SQL injections.
You can store that value to variable and use in query or you can use escape string .
$query = "Select fruits in ('".$var[$x]['seeds']."') from edible"
It seems that you want to create a query that compares a value against a list of possible other values with SQL. I would do this with a single prepared statement:
$sql = 'SELECT fruits IN (?,?,?,?,?,?,?,?,?,?) FROM edible';
$stmt = $db->prepare($sql);
$stmt->execute(array_map(function($value) {
return $value['seeds'];
}, $var));
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´m trying to insert a current SELECT-Statement into an Database table with an PHP-variable. It works by this way without the variable:
mysql_query("INSERT INTO table (column) SELECT column FROM table1");
Did anyone have a idea how I can fix it?
PHP: 5.3.10 and mysql-databse
try this
$sample="SELECT column FROM table1"; // your select query
$query="INSERT INTO `table` (column) $sample";
mysqli_query('database connection code',$query);
Let me know if any problem arises.
You can use mysql_insert_id() to get the last inserted ID value and try insert query separately.
<?PHP
$lastId = mysql_insert_id();
$query = mysql_query("Your Select Query with $lastId");
?>
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.