Insert data in a table - php

I have this code to select and output data from database
<?php
require('system/connect.php'); //load the connection file
$sql = ("SELECT * FROM `movie`"); // add mysql code to a variable. In this case it will select ALL columns from the database.
$query = mysql_query($sql); //run the query contained within the variable.
while ($row = mysql_fetch_array($query)) { //store each single row from the database in an array named $row. While there are any rows left, loop through and execute the following code:
$id = $row['movie_id']; //gets name from DB for a single row
$name = $row['movie_name']; //gets age from DB for a single row
$category = $row['movie_category']; //gets age from DB for a single row
//Following code outputs the data to the webpage:
echo $id;
echo $name;
echo $category;
};
?>
The page show: 1titanicromance2zoroaction3blood diamondsaction
I need a way to make a table or array and to insert data directly to it.

Adding in HTML for Table should do the trick. Although, it's crappy coding to mix PHP and HTML.
<?php
require('system/connect.php'); //load the connection file
$sql = ("SELECT * FROM `movie`"); // add mysql code to a variable. In this case it will select ALL columns from the database.
$query = mysql_query($sql); //run the query contained within the variable.
echo '<table>';
while ($row = mysql_fetch_array($query)) { //store each single row from the database in an array named $row. While there are any rows left, loop through and execute the following code:
$id = $row['movie_id']; //gets name from DB for a single row
$name = $row['movie_name']; //gets age from DB for a single row
$category = $row['movie_category']; //gets age from DB for a single row
//Following code outputs the data to the webpage:
echo '<tr>';
echo '<td>' . $id . '</td>';
echo '<td>' . $name . '</td>';
echo '<td>' . $category . '</td>';
echo '</tr>';
};
echo '</table>';
?>

Did you meant HTML table? Then it will looks like this
<?php
require('system/connect.php'); //load the connection file
$sql = ("SELECT * FROM `movie`"); // add mysql code to a variable. In this case it will select ALL columns from the database.
$query = mysql_query($sql); //run the query contained within the variable.
if (mysql_num_rows($query)) { // if there is some rows in result
echo "<table>"; // starting HTML table
while ($row = mysql_fetch_array($query)) { //loop through the result
echo "<tr>".
"<td>".$row['movie_id']."</td>".
"<td>".$row['movie_name']."</td>".
"<td>".$row['movie_category']."</td>".
"</tr>";
}
echo "</table>";// finishing HTML table
}
?>
NOTE: do not use mysql_* functions. They are deprecated. Use PDO or Mysqli instead.

Related

Display the information from a SQLite Query in the browser

So, I'm trying to use PHP in order to query an sqlite database, I have no problem with the connection or with the query itself, however, I don't know what I could do in order to display the data in a clean way, or even put it inside an HTML table. The code I'm working with right now is:
<?PHP
$connection = new SQLite3('my_db.db');
if($connection){
echo "Connected\n";
}
$results = $connection->query('SELECT * FROM Meter1');
while($row=$results->fetchArray()){
var_dump($row);
}
?>
After you have done a $row = fetchArray() the variable $row is a array containing the data returned from your query in the form of an Array. If you add SQLITE3_ASSOC it will be an Associative Array where the keys are the names of the database columns.
So lets assume your table has the columns id, name, dob then this would be how you get to that column data
<?php
$connection = new SQLite3('my_db.db');
if($connection){
echo "Connected\n";
}
$results = $connection->query('SELECT * FROM Meter1');
while($row=$results->fetchArray(SQLITE3_ASSOC)){
echo 'id = ' . $row['id'] . '<br>';
echo 'name = ' . $row['name'] . '<br>';
echo 'Date of Birth = ' . $row['dob'] . '<br>';
}
?>
So if you want the data in a table its just a case of wrapping the HTML around that while loop like this
echo '<table>';
echo '<tr><td>id</td><td>name</td><td>Date of Birth</td></tr>';
while($row=$results->fetchArray(SQLITE3_ASSOC)){
echo '<tr>';
echo "<td>$row[id]</td><td>$row[name]</td><td>$row[dob]</td>";
echo '</tr>';
}
echo '</table>';

How to print the whole database table from phpmyadmin using PDO in php file

I am trying to print the whole database in tabular format in a php file using PDO. i have this database stored in phpmyadmin. But there are a lot of rows in there like name,id, etc etc... I have this following php code. i already made a connection to the database and added require_once() in the page. But i dont know how to print all these values in a tabular method. like showing it in a way a normal database will look like.
$q="SELECT * FROM `employee`";
$sth = $odb->prepare($q);
$sth->execute();
while ($r = $sth->fetch(PDO::FETCH_ASSOC)){
// code here
}
Can someone help me to display the table properly. That is if i run it in browser, i should see a table instead of that ugly array format
Ideally each table should have his own format, but if you just want to pop all the data from the base inside a HTML table, you could do something like that :
$sql = 'SELECT * from page';
$result = $pdo->query($sql);
$rows = $result->fetchAll(PDO::FETCH_ASSOC);
if(count($result)) {
echo '<table><tr>';
foreach ($rows[0] as $columnName => $value) {
echo '<th>' . $columnName . '</th>';
}
echo '</tr>';
foreach ($rows as $row) {
echo '<tr>';
foreach ($row as $value) {
echo '<td>' . $value . '</td>';
}
echo '<tr>';
}
echo '</table>';
}
You could use this on each table you want to show.

Heading come in all the rows

Heading come in all the rows need in first row
Need help with where i am going wrong
thank you
below is the code
<?php
$connection = mysql_connect('localhost', 'aria2', 'osvOSUxlY6wYLZzC'); //The Blank string is the password
mysql_select_db('torres');
$query = "SELECT * FROM reports"; //You don't need a ; like you do in SQL
$result = mysql_query($query) or die(mysql_error());;
echo "<table>"; // start a table tag in the HTML
while($row = mysql_fetch_array($result)){ //Creates a loop to loop through results
echo "<table><tr><th>CSQ_Name</th>
<th>CSQ_ID</th>.......
$row..............
//$row['index'] the index here is a field name
}
echo "</table>"; //Close the table in HTML
mysql_close(); //Make sure to close out the database connection
You only need to move the header portion out of the loop:
echo '<table><tr><th>CSQ_Name</th><th>CSQ_ID</th></tr>'; // start a table tag in the HTML
while ($row = mysql_fetch_array($result)) { //Creates a loop to loop
echo '<tr>';
// output data here
echo '<td>...</td><td>...</td>';
echo '</tr>';
}
echo "</table>"; //Close the table in HTML

List all table contents from generated list of table column names

I have got one piece of code which gives me a list of all the column names for a specific table. Can i use this list as my $row['VARIABLE'] instead of specifying every row?
The current output of the table names can be seen here this is the second paragraph of text, the first ones are my table names (USERS, ADMIN_LOGIN)
I am working on making a small script which will list all the table contents of a table but it is likely the table will change often so i want a script which can auto generate the table of contents without me having to manually re-enter it all as i have done in the second piece of code below?
Thanks guys
<?php
// Display all sqlite column names for chosen table
$db = new SQLite3('data.db');
$tablesquery = $db->query("PRAGMA table_info(USERS)");
while ($table = $tablesquery->fetchArray(SQLITE3_ASSOC)) {
echo $table['name'] . '<br />';
}
// Display * from USERS
$db = new SQLite3('data.db');
$results = $db->query('SELECT * FROM USERS');
while ($row = $results->fetchArray()) {
echo "ID = ". $row['ID'] . "\n";
echo "USERNAME = ". $row['USERNAME'] ."\n";
echo "AGE = ". $row['AGE'] ."\n";
echo "LOCATION = ".$row['LOCATION'] ."\n\n";
echo "ANYTHING_ELSE = ". $row['ANYTHING_ELSE'] . "\n";
echo "EMAIL = ". $row['EMAIL'] ."\n";
echo "PROFILE_APPROVED = ". $row['PROFILE_APPROVED'] ."\n";
echo "NUMBER_OF_BATTLES = ".$row['NUMBER_OF_BATTLES'] ."\n\n";
echo "TOTAL_WINS = ".$row['TOTAL_WINS'] ."\n\n";
}
?>
Yes, you can use variables for the index values of an array. For example,
$row = array('col_name' => 'column value');
$index = 'col_name';
echo $row[$index]; // Equivalent to $row['col_name']. Prints "column value"
You should be able to accomplish what you want pretty easily using the above logic. Basically just store the column names from the first query into an array, then loop through that array to get the column names each time you print a row.
Give this a try:
// Display all sqlite column names for chosen table
$db = new SQLite3('data.db');
$tablesquery = $db->query("PRAGMA table_info(USERS)");
$columns = array();
while ($table = $tablesquery->fetchArray(SQLITE3_ASSOC)) {
$columns[] = $table['name'];
}
// Display * from USERS
$db = new SQLite3('data.db');
$results = $db->query('SELECT * FROM USERS');
while ($row = $results->fetchArray()) {
foreach ($columns as $col)
echo $col . " = " . $row[$col] . "\n";
}
I use the following in order to get a list of columns names in the order they are defined in the table. I use it to populate an insert/select when preserving data.
sel columnname
from dbc.columns
where databasename='databasename'
and tablename='tablename'
order by columnid;

Display SQL database query result as link

I have a search form on a previous page where I allow users to search for $q. I then query the database for 'keys' LIKE $q. The while loop displays the 'name' and 'weblink' of each matching database entry.
This all works correctly. However, I would like the 'weblink' to display as a clickable link. Ideally it would read as if it were HTML: 'weblink'. I cannot figure out the correct combo of php and html to make both the while loop, and the HTML work.
Any help would be appreciated.
Thanks in advance.
// query database
$query = mysql_query("SELECT * FROM `forumlist` WHERE `keys` LIKE '%$q%'");
// display query results
while($row = mysql_fetch_array($query))
{
echo $row['name'];
echo "<br/>";
echo $row['weblink'];
}
while($row = mysql_fetch_array($query))
{
echo $row['name'];
echo "<br/>";
echo '' . $row['weblink'] . '';
}

Categories