Access MySQL field's Comments with PHP
Poke around information_schema
.
SELECT table_comment
FROM information_schema.tables
WHERE table_schema = 'myschema' AND table_name = 'mytable'
Is there a way to get the COMMENT from mysql columns using php?
You can query catalog view INFORMATION_SCHEMA.COLUMNS
for this:
select column_comment
from information_schema.columns
where table_name = 'car_comfort' and column_name = 'boardcom'
Comment function with MySQL and PHP
To know which post a comment belongs to, you can use an input with type="hidden"
like this
<input type="hidden" name="comment_post_id" value="' . $row['id'] . '">
and access it like you would any other field via $_POST['comment_post_id']
.
Unless you have lots (and you have to consider the definition for lots depending the dbms you're using) of comments, this is an overkill. You create many tables feed_comment_X
which contain only few entries.
The basic solution is to have one table comments, which could look like this
CREATE TABLE comments (
comment_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
post_id INT NOT NULL FOREIGN KEY REFERENCES posts (id) [ON..],
full_name VARCHAR(32) NOT NULL,
comment TEXT NOT NULL,
date_time DATETIME NOT NULL
);
You save all comments in this table and don't need to check for existance of and (if necessary) create a new table on each page load.
To get all comments you simply (using parameterized statements)
SELECT full_name, comment, date_time FROM comments
WHERE post_id = :post_id
And add a new entry by
INSERT INTO comments (post_id, full_name, comment, date_time) VALUES
(:post_id, :full_name, :comment, :date_time)
This allows fast operations if you want to get additional info, eg. the number of comments each post has:
SELECT p.id, .., COUNT(c.post_id) AS total_comments FROM posts AS p
LEFT JOIN comments AS c on c.post_id = p.id
GROUP BY (p.id)
php to display table; column one with mysql column comments and column two transposed row results
Finally, the solution. Spencer7593 got me on my way but the final code required is as follows:
$con=mysqli_connect("localhost","user","password","test");
if (mysqli_connect_errno($con)) {echo "MySQL conn. err:".mysqli_connect_error();}
$sql = "SELECT column_comment,column_name FROM information_schema.columns
WHERE table_name = 'mytablename';";
$query = mysqli_query($con,$sql) or die(mysql_error());
$columnArray = array();
while(($result = mysqli_fetch_array($query, MYSQL_ASSOC))){
if($result['column_comment'])
{
$CurCol = $result['column_comment'];
}
else
{
$CurCol = "No text stored";
}
$CurName = $result['column_name'];
$columnArray[$CurName]=$CurCol;
}
//echo $columnArray['FirstName']; //test example to return "1(a) First Name"
So I looped through fetch_fields to get the corresponding column comments, and then looped through the result of the answers query to display each answer down the next table column.
Storing Comments with PHP
Replace
$a= array('comments' =>'$_GET["like"]');
mysql_query("INSERT INTO try1 (count) VALUES ('$a')",$conn);
with this:
$a= array('comments' =>$_GET["like"]);
mysql_query("INSERT INTO try1 (count) VALUES ('".mysql_real_escape_string($a['comments'])."')",$conn);
mysql, how to query the table comments?
Query the COLUMNS table in the information_schema database.
SELECT a.COLUMN_NAME, a.COLUMN_COMMENT
FROM information_schema.COLUMNS a
WHERE a.TABLE_NAME = 'YOUR_TABLE_NAME'
You can do similar queries for any type of element in your db (ie tables, triggers, etc).
Show Comment of Fields FROM Mysql Table
You can use the query
SHOW FULL COLUMNS FROM <tablename>
If you don't want to use information_schema
.
Store comments on networking site with MySQL
For this I'm thinking about creating a table for named user_comments
and that has column user_id ,commenter_id(all the commenter who
commented their id seprated by comma) ,comments(then All the user
comments seprated by comma)
God no! You are almost there:
Table comments
id INT AUTO_INCREMENT
recipient_id
sender_id
message TEXT
[ sent DATETIME ]
[ other meta data ]
Store one message in message
. Create one row per message. Never store several records in the same field separated by anything.
PHP/MYSQL Comment-reply table structure
I would add two columns for referenceid
, and parentid
. That way, you can have nested comments, if you so choose. Much easier and more efficient than joining multiple tables in your queries. If the comment is not a reply, referenceid
is 0 (or null). No need to create another column to flag whether it's a reply.
Related Topics
Select Statement with Fetch_Array in MySQLi Prepared Statements
How to Execute MySQL Script with Variables Using PHP::Pdo
PHP MySQL Character Set: Storing HTML of International Content
Share Session Between Two Websites
Appending a Value of a Variable to a Variable Name
Use of Undefined Constant Stdin - Assumed 'Stdin' in C:\Wamp\Www\Study\Sayhello.PHP on Line 5
How to Determine If a PHP File Is Loaded via Cron/Command Line
Passing and Parse Paypal Ipn Custom Field
How to Add More Custom Field in Linked Product of Woocommerce
Parsing Extremely Large Xml Files in PHP
Shell_Exec() Timeout Management & Exec()
Upload Images Through PHP Using Unique File Names
Soapclient: How to Pass Multiple Elements with Same Name
PHP Require_Once Failed to Open Stream Permission Denied
Php: Re Order Associative Array