Can I create mysql view columns from an array? [closed] - php

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.

Related

MySQL Column name defined by php variables [closed]

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))

sort a JSON array with PHP [closed]

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
I'm trying to register for youtube API statistics as JSON data to its proper database. I figured out listing whole data so far,
but I'm not able to list and sort the individual data, which stands for "dislikeCount", descending from more to less.
The JSON data I mentioned in "$ Row ['page_content']" section is below. and I want the "dislikeCount" data to be sorted from large to small.
Any help will be appreciated.
Database page_content
{
"kind":"youtube#video",
"etag":"LVLEiwEdUU043rY6JqiUrm7FmYY",
"id":"IMiBrszc40s",
"statistics":{
"viewCount":"6416534",
"likeCount":"26553",
"dislikeCount":"1862",
"favoriteCount":"0",
"commentCount":"987"
}
}
index.php
$query = $db->from('pages')->orderBy('page_id', 'ASC')->all();
if ($query) {
foreach ($query as $row) {
$json = json_decode($row['page_content'], true);
$videoView = $json['statistics']['viewCount'];
$videoLike = $json['statistics']['likeCount'];
$videoComment = $json['statistics']['commentCount'];
echo $videoView;
echo $videoLike;
echo $videoComment;
}
}
From what I understand from your question, you have a database table pages. This table has a JSON column, page_content, which contains the JSON as you've specified. Now you want to sort the pages based on the pages.page_content.statistics.dislikeCount?
If you're using MySQL or PostgreSQL, then you can order the resultset in the SQL query directly. For example, for MySQL (untested, as I don't have mysql running locally right now):
SELECT * FROM pages
ORDER BY page_id, page_content->"$.statistics.dislikeCount"
If SQL is not an option, you can use PHP's usort.

Inserting php arrays into mysqli database rows? [closed]

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.

Mysqli get array from database and format to string [closed]

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
I'm trying to get array of fields from database:
$query=("SELECT firstname FROM users");
$res = $connect->query($query);
Target is to display first name of every users:
echo $firstname
Output:
Firstname1,Firstname2,Firstname3
I know should be easy, if I just knew more.
For getting all records or a set of records you have to fetch each records inside a while loop.
$query=mysqli_query($connect,"SELECT firstname FROM users"); // execute the query
$string="";
while ($row = mysqli_fetch_array($query)) // fetch each records
{
$string.=$row['firstname']).','; // append the firstname to a variable
}
echo $string; // output the constructed variable with all firstnames

How to select all fields where a string is common in all the tables in a mysql database? [closed]

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 a mysql database where there is 3 tables 'groupstage', 'quarterfinal,' semifinal. All of these table have a column named 'Date'. Now I want to write a query from where I can get all the fields from those tables with a specific date? what should i write it in php?
To get what you want, you can write a query which join the 3 tables like :
SELECT *
FROM groupstage, quarterfinal, semifinal
WHERE groupstage.date = quarterfinal.date AND quarterfinal.date = semifinal.date
AND date = "YOUR DATE"
In php you should just create a var $query = "My query above"

Categories