PHPMyAdmin LaTeX-export doesn't show row comments - mysql

I'm trying to export my database tables to LaTeX in PHPMyAdmin. It does generate a "comments" column, but nothing is put in there eventhough I do have comments for several fields of the table I'm trying to export. (and of course I do have the "comments"-checkbox checked). Does anybody know a solution to this, or is this simply a bug in (this version of) PHPMyAdmin?
I'm using MySQL 5.5.9 with PHPMyAdmin version 3.3.9.2.

I decided to solve the problem by myself by writing the following PHP-script. It makes LaTeX-tables from all MySQL tables in the database, with a Field column for the row name, and a Description column for the comments. The code does not contain the MySQL connection logic. It can be a nice addition in your documentation to the graphical schemes that can be made with MySQLWorkbench. For good displaying in a web browser, use nl2br().
function showDescriptions(){
$result = "";
$tables = mysql_query("SHOW TABLES");
while($table = mysql_fetch_row($tables)){
$columns = mysql_query("SHOW FULL COLUMNS FROM `".$table[0]."`");
$result .= "\begin{table}[h!] %b!p!\n";
$result .= '\begin{tabular}{|p{0.3\textwidth}|p{0.63\textwidth}|}'."\n";
$result .= "\hline\n";
$result .= "Field & Description\\\\\n";
$result .= "\hline \hline\n";
while($column = mysql_fetch_array($columns)){
$result .= LaTeXSafe($column['Field']);
$result .= " & ";
$result .= LaTeXSafe($column['Comment']);
$result .= "\\\\\n";
$result .= "\hline\n";
}
$result .= '\end{tabular}'."\n";
$result .= '\vspace{-7pt}'."\n";
$result .= '\caption{\textit{Field descriptions of table '.$table[0].'}}'."\n";
$result .= '\vspace{-7pt}'."\n";
$result .= '\label{table-'.$table[0].'}'."\n";
$result .= '\end{table}'."\n\n\n";
}
return $result;
}
function LaTeXSafe($text){
return str_replace("_", "\_", $text);
}
I hope it's useful to someone.

Related

How to make reference to one of my MySQL databases inside my PHP file?

I have already asked this related question: https://webmasters.stackexchange.com/questions/116055/using-mysql-database-data-directly-into-generating-articles-for-my-website-new/116056?noredirect=1#comment154341_116056
At this point, I'm starting to understand the code syntax and project structure a little better.
But I have made my database using MySQL console. it only has a few entries so far, I wanted to try to adapt the code in Zach's example, but here is the problem I have:
The problem is, I am unsure how to get the reference to my database object? In the code sample from Zach there is variable $db, I guess this is where i need to keep a reference to my own actual database.
Here is the psuedo-code (maybe) from Zach, note: he always said to me not to copy-paste it, but I'm just trying to see how I can use it in my project.
<?php
$SQL_Query = "SELECT * FROM your_table";
$SQL_Run = mysqli_query($db, $SQL_Query);
while ($row = mysqli_fetch_assoc($SQL_Run)) {
echo
"<section class='wrapper style1'>
<div class='inner'>
<header class='align-center'>
<h2>" . $row['imageurl'] . "</h2>
<img src='" . $row['title'] . "'>
<p>" . $row['description'] . "</p>
</header>
</div>
</section>";
}
?>
So my question simply at moment is: How to create the reference $db?
Your answer is the correct way to establish a connection. I want to point out that there are two ways of writing that statement. The version you found online is one way, but from our previous conversation, you can write it like this:
<?php
// Establish how to log in
$servername = "127.0.0.1";
$username = "root";
$password = "yourpasswordhere";
$dbname = "yourdatabasenamehere";
// create the database connection
$db = new mysqli($servername, $username, $password, $dbname);
// if it fails, kill the site.
if (mysqli_connect_error($db)) {
die("Connection failed: " . mysqli_connect_error($db));
}
// your first query to grab all the article data
$SQL_Query = "SELECT * FROM your_table";
// run the query
$SQL_Run = mysqli_query($db, $SQL_Query);
// while data exists (it makes sure that you have post data, otherwise nothing shows up)
while ($row = mysqli_fetch_assoc($SQL_Run)) {
echo
"<section class='wrapper style1'>
<div class='inner'>
<header class='align-center'>
<h2>" . $row['imageurl'] . "</h2>
<img src='" . $row['title'] . "'>
<p>" . $row['description'] . "</p>
</header>
</div>
</section>";
}
// Close the connection
mysqli_close($db);
?>
You will notice that the connections are written like a function.
mysqli_num_rows($result);
instead of
$result->num_rows
Both do the same thing, just a personal preference. That should hopefully clear some things up from your first post :)
I have got further on and I think have answered my own question. I found it a bit tricky to research because I don't understand all the different terms and names of features/api/scripts/etc. But I had just to read the documentation for mysqli_connect(), I set up the code as follows and now I have pulled all the data from the database into words on my html/php files.
From here I think I can rewrite the code to first sort it by date and then can of course put the latest posts at the top of each page etc.
I can also allow the user to click 'Genre' and only view Comedy for example.
Here is the code just to get the data parsed into my index.php file:
<?php
$servername = "127.0.0.1";
$username = "root";
$password = "yourpasswordhere";
$dbname = "yourdatabasenamehere";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, type, title FROM releases";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Type: " . $row["type"]. " - Title " . $row["title"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
I extended upon the above work by making the php script fetch all the entries in the database and create the previous html article I had once for each entry. In the SELECT statement I can control which types of entries are displayed (eg. For a certain category). Here was how I did it:
// make an html article based snippet (image, title, description, etc),
//once for each entry in the database table...
<?php
$servername = "127.0.0.1";
$username = "root";
$password = "somepassword";
$dbname = "somedatabasename";
// create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// check connection
if ($conn->connect_error) {
die("connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM releases ORDER BY id DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
while($row = $result->fetch_assoc())
{
echo '<section class="wrapper style1">';
echo '<div class="inner">';
echo '<header class="align-center">';
echo '<h2>'. $row["title"] . '</h2>';
echo '<div class="image fit">';
echo '<img src='. $row["imgurl"] .'>';
echo '</div> <p> RELEASE TITLE: ' . $row["title"] . '<br /> DATE POSTED: ' . $row["postdate"] . '<br /> DESCRIPTION: ' . $row["description"] . '</p>';
echo 'DOWNLOAD LINK: '.$row["link"].' <br />';
$NfoLink = $row["nfolink"];
if ($NfoLink != 'not found' && $NfoLink != '')
{
echo 'NFO LINK/MORE DOWNLOADS: '.$row["nfolink"].'';
}
echo '</header>';
echo '</div>';
echo '</section>';
}
}
else
{
echo "0 results";
}
$conn->close();
?>

Create a div on my website that shows database information with php

I need to create a div that only shows the information of the database, how should i connect the div to mysql so it only shows my database information?
I'm not sure I'm understanding your question... but I'll give it a try. The code below this will select whatever you tell it to from the database and echo it into a table.
<?php
$db = mysqli_connect('host', 'username', 'password', 'database');
$sql = "SELECT * FROM database"
mysqli_stmt_bind_param($db, "params here", your info here);
$res = mysqli_stmt_execute($sql);
mysqli_stmt_close($sql);
mysqli_close($db);
echo "<table>";
while($row = mysqli_fetch_array($res)){ //Creates a loop to loop through results
echo "<tr><td>" . $row[''] . "</td><td>" . $row[''] . "</td></tr>"; //$row['index'] the index here is a field name
}
echo "</table>";
?>

using mysqli instead of mysql accessing the DB

please find below a script in php that that fetch data from my database company, the database is in phpMyadmin
I understand that there are secure ways to fetch data. My question is
Can I improve the code below using mysqli to fetch instead of mysql_query.
<?php
get results from database. Can I improve the code below using mysqli to fetch instead of mysql_query in the following line?.
$result = mysql_query("SELECT * FROM customer")
or die(mysql_error());
tables begins here with the customer id and customer name
echo "<table >";
echo "<tr>
<th>ID</th> the customer id
<th>Name</th> the customer name
</tr>";
// loop through results of database query, displaying them in the table
the table below display the columns in html using mysql fetch array. all data is displayed.
while($row = mysql_fetch_array( $result )) {
// echo out the contents of each row into a table
my purpose is embed the php in html in another way using msqli
echo "<tr>";
echo '<td>' . $row['CustomerID'].'</td>';
echo '<td>' . $row['Name']. '</td>';
echo "</tr>";
}
echo "</table>";
?>
my purpose is embed the php in html in another way using msqli
Just replace all the mysql with mysqli
$result = mysqli_query("SELECT * FROM customer");
while($row = mysqli_fetch_array( $result )) {
If you wanna keep it simple
Or do it like this...
/*Your connection info goes here...*/
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT * FROM customer";
if ($result = $mysqli->query($query)) {
echo "<table ><tr><th>ID</th><th>Name</th></tr>";
/* fetch results by row */
while ($row = $result->fetch_row()) {
echo "<tr><td>' . $row['CustomerID'].'</td><td>' . $row['Name']. '</td></tr>';
}
/* free the result set */
$result->close();
}
/* close the connection */
$mysqli->close();

Displaying MYSQL data in a HTML table AFTER a search

I want to thank everyone here for the help I have recieved so far. My next question is a bit more complicated.
So I have a database set up on my server, and I have a form on my website where I am submitting data to my MYSQL database.
After I submit the data, I am having trouble searching for it, displaying possible results, and then making those results HYPERLINKED so that the user can find out more about they are looking for.
My "common.php" script is set up like this:
<?php
$username = "XXX";
$password = "XXX";
$hostname = "XXX"; 
$database = "XXX";
mysql_connect($hostname, $username, $password, $database) or die
("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
?>��
My "insertdata.php" script is set up like this:
<?php
require("common.php");
// connect with form
$name=$_POST['firstname'];
$lastname=$_POST['lastname'];
$city=$_POST['city'];
$state=$_POST['state'];
$zip=$_POST['zip'];
$phone=$_POST['phone'];
$email=$_POST['email'];
$various=$_POST['various'];
$other=$_POST['other'];
// insert data into mysql
$query="INSERT INTO datatable
(
firstname,
lastname,
city,
state,
zip,
phone,
email,
various,
other,
)
VALUES
(
'$firstname',
'$lastname',
'$city',
'$state',
'$zip',
'$phone',
'$email',
'$various',
'$other',
)";
$result=mysql_query($query);
// if successfull displays message "Data was successfully inserted into the database".
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='insert.php'>Back to main page</a>";
}
else {
echo "ERROR... data was not successfully insert into the database";
}
mysql_close();
?>��
From there, I want to make the inserted data searchable.
My problem is, when the search is completed, I want to only display the First Name and Last Name in two separate columns.
From there, I want a link displayed in a third separate column with a link in each row that says "View Record Details."
Finally, when "View Record Details" in clicked, it brings me to the correct record, formatted again in an HTML table.
The closest I have come to a solution is:
<?php
require("common.php");
$query="SELECT * FROM datatable";
$result=mysql_query($query);
$num=mysql_numrows($result);
$i=0;
while ($i < $num) {
$firstname=mysql_result($result,$i,"firstame");
$lastname=mysql_result($result,$i,"lastname");
$i++;}
?>
As an additional question, when I use PDO, does that change my HTML?
Switch to PDO. Your code will look something like this:
$conn = new PDO('mysql:host=db_host;dbname=test', $user, $pass);
$sql = 'SELECT * FROM datatable';
foreach ($conn->query($sql) as $row) {
print $row['firstname'] . "\t";
print $row['lastname'] . "\n";
}
EDIT:
To link back for details add this line after the 2nd print:
print "<a href='somephp.php?idx=" . $row[ 'idx' ] . "'>link here</a>";
You'll need another php file called 'somephp.php':
$conn = new PDO('mysql:host=db_host;dbname=test', $user, $pass);
$idx = $_REQUEST[ 'idx' ];
$sql = 'SELECT * FROM datatable where idx = ?';
$stmt = $conn->prepare( $sql );
$stmt->bindParam( 1, $idx );
$stmt->execute();
$row = $stmt->fetch();
// now print all the values...
print $row['firstname'] . "\t";
print $row['lastname'] . "\t";
print $row['address'] . "\t";
and so on...
NOTE: This depends on each record having a unique key 'idx'. I don't see this in your values above so you'll have to find a way to incorporate it if you want to use this code.
ALSO: You ask - does this change the HTML and does this handle table formatting - No to both. You do all the HTML formatting via the print statements. All PHP does it output lines to the browser.

How do I create a query that will insert large amounts of text special characters in MySQL

INFO: I am using [ MySQL 5.5.16 ]-[ PHP 5.3.8 ]-[ jqSajax 1.0.2 ]-[ JQuery 1.2.2 ]
PHP, MySQL, Apache and HTML header are charset UTF-8
I have been working on this for three days.
One database:
tblitems InnoDB
tblarticles MyISAM FULLTEXT
PROBLEM: I cannot insert a large amount of text with a simple insert because I have special characters ” “ ’ ‘ — …
QUESTION: How do I insert a large amount of text into a LONGTEXT field with special characters ” “ ’ ‘ — …
SEARCHED/TRIED: See code below and MySQL output at bottom
encode IN Javascript AJAX encodeURL()
decode IN PHP urldecode() to build SQL Query
CODE:
=========== create javascript ==========
<?php //calls php function through jqsajax
$strOnClickString = "$('#none').html(";
$strOnClickString .= '$.x_Add_Item(';
//$strOnClickString .= "$('#sel_txtPublication').val()"; //$strPublication
$strOnClickString .= "'Co-Worker Letters'"; //$strPublication
$strOnClickString .= ", $('#sel_txt_Title').val()"; //$strTitle
$strOnClickString .= ", $('#sel_txtMonth').val()"; //$strMonth
$strOnClickString .= ", $('#sel_txtYear').val()"; //$strYear
$strOnClickString .= ", $('#sel_txtAuthor').val()"; //$strAuthor
$strOnClickString .= ", encodeURI($('#strArticle').val())"; //$strArticle
$strOnClickString .= '))';
echo Create_Input('button','Submit Text','10','btnSubmit','btnSubmit','onclick',$strOnClickString); ?>
========= PHP create SQL ===========
//=========================================================================
function Add_Item($strPublication, $strTitle, $strMonth, $strYear, $strAuthor, $strArticle){
//=========================================================================
$sql1 = "";
$sql = "";
$sql1 = "INSERT INTO `hwa_archive`.`tblarticles`";
$sql1 .= "(`strArticle`)";
$sql1 .= " VALUES (";
if ($strArticle != 'ignore')
{
$sql1 .= '"' . urldecode($strArticle) . '", ';
}
$sql1 .= ")";
$sql1 = str_replace('", )','" )',$sql1);
$res2 = mysql_query($sql1);
//$res2 = mysql_query('SELECT LAST_INSERT_ID()');
$intArticle_ID = mysql_insert_id();
$sql = "INSERT INTO `hwa_archive`.`tblitems`";
$sql .= "(`strPublication`, `strTitle`, `strMonth`, `strYear`, `strAuthor`, `intArticle_ID`)";
$sql .= " VALUES (";
if ($strPublication != 'ignore')
{
$sql .= "'$strPublication', ";
}
if ($strTitle != 'ignore')
{
$sql .= "'$strTitle', ";
}
if ($strMonth != 'ignore')
{
$sql .= "'$strMonth', ";
}
if ($strYear != 'ignore')
{
$sql .= "'$strYear', ";
}
if ($strAuthor != 'ignore')
{
$sql .= "'" . $strAuthor . "', ";
}
$sql .= "'$intArticle_ID', ";
$sql .= ")";
$sql = str_replace("', )","' )",$sql);
$res2 = mysql_query($sql);
Return $intArticle_ID . "<br /><br />" . $sql . "<br /><br />" . $sql1;
}
MySQL OUTPUT:
YOUR Part in the World’s Network (’ needs to be a single curly quote ’)
YOUR Part in the World%u2019s Network (%u2019 needs to be a single curly quote ’) no decode in add_item
I hope this info is not overkill.