I have a question about PHP/MYSQL.
Here you see my php page in the browser:
And here you see my phpmyadmin database:
What I want is when I click on:"Laders" I only want to see on the next page everything in the "groep":"Laders". The problem is I don't know how to do that in a loop. Here is the code:
<?php
$link = mysqli_connect('localhost', 'root', 'root', 'producten');
$query = "SELECT *
FROM producten
WHERE Merk = '" . 'Apple' . "'
ORDER BY Artikelnummer, Artikelnaam";
/*echo $query;*/
$result = mysqli_query($link, $query);
?>
<table>
<?php
echo '<table style="width:100%">
<tr style="color:yellow; background-color:black;">
<th>Apple</th>
</tr>';
foreach ($result AS $rij)
{
echo '<tr style="background:blue"><td>' . $rij['Groep'] . ' </td></tr>';
}
?> </table>
foreach ($result AS $rij)
{
echo '<tr style="background:blue"><td>'.$rij['Groep'].'</td></tr>';
}
After this, create a php file "view.php" and fetch data using $_GET['variable']
Related
I use bootstrap 3 template and connect to a mysql database, sort the table and export some datas as HTML table in order to display it on bootstrap webpage.
Here is the mySQL look like :
The following code won't get expected results:
<div class="well">
<?php
//MySQL Database Connect
include '/includes/dbconnect.php';
$result = mysqli_query($conn,"SELECT * FROM webpilot ORDER BY NEXT_EVENT ASC");
if(mysqli_num_rows($result) >= 1) {
echo '<table class="table table-striped table-bordered table-hover">';
echo "<tr><th>ID</th><th>NAME:</th><th>EPOCH</th></tr>";
while($row = mysqli_fetch_array($results)) {
echo "<tr><td>";
echo $row['ID'];
echo "</td><td>";
echo $row['NAME'];
echo "</td><td>";
echo $row['NEXT_EVENT'];
echo "</td></tr>";
}
echo "</table>";
}
?>
</div> <!-- well -->
dbconnect.php :
<?php
$localhost="xxx.xx.xxx.com";
$username="dboxxxxxxx";
$password="xxxxxx";
$database="dbxxxxxx";
$conn = mysqli_connect($localhost,$username,$password,$database);
//test if connection failed
if(mysqli_connect_errno()){
die("connection failed: "
. mysqli_connect_error()
. " (" . mysqli_connect_errno()
. ")");
}
?>
original page link can be found here : http://s529471052.onlinehome.fr/bs3/gpio/dyntable.htm
Its just a simple typo
$result = mysqli_query($conn,"SELECT * FROM webpilot ORDER BY NEXT_EVENT ASC");
if(mysqli_num_rows($result) >= 1) {
echo '<table class="table table-striped table-bordered table-hover">';
echo "<tr><th>ID</th><th>NAME:</th><th>EPOCH</th></tr>";
//while($row = mysqli_fetch_array($results)) {
^^^^^^^^
while($row = mysqli_fetch_array($result)) {
While testing Add
ini_set('display_errors', 1);
ini_set('log_errors',1);
error_reporting(E_ALL);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); `
to the top of your script. This will force any mysqli_ errors to
generate an Exception that you can see on the browser and other errors will also be visible on your browser.
Answer for additional info provided in a comment
From your comment below, it appears you are trying to run PHP code from a web page with a .htm extension. That wont work unless you have configured Apache to do this
Change the web page file name to have a .php extension and then Apache will pass the PHP code to the PHP interpreter for compilation and execution.
$result = mysqli_query($conn,"SELECT ID, NAME, NEXT_EVENT FROM webpilot ORDER BY NEXT_EVENT ASC");
$table = '<table>
<tr>
<th>ID</th>
<th>NAME</th>
<th>EPOCH</th>
</tr>';
if($result ) {
while ($row = mysqli_fetch_assoc($result)) {
$table .= '<tr>
<td>'. $row['ID'] .'</td>
<td>'. $row['NAME'] .'</td>
<td>'. $row['NEXT_EVENT'] .'</td>
</tr>';
}
} else {
$table .= '<tr><td colspan="3">No date found</td></tr>';
}
$table .= '</table>';
echo $table;
Try this, This will help for your requirement.
I'm assuming that your connection to the database is OK (though the given page link suggests otherwise).
It seems the table is not exporting properly because $result was misspelled as $results in the code segment printing the data rows: $row = mysqli_fetch_array($results).
Try the modification below (also refactored for better readability):
<div class="well">
<?php
//MySQL Database Connect
include '/includes/dbconnect.php';
$result = mysqli_query($conn,"SELECT * FROM webpilot ORDER BY NEXT_EVENT ASC");
if(mysqli_num_rows($result) >= 1)
{
echo '<table class="table table-striped table-bordered table-hover">';
echo "<tr><th>ID</th><th>NAME:</th><th>EPOCH</th></tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>$row['ID']</td>";
echo "<td>$row['NAME']</td>";
echo "<td>$row['NEXT_EVENT']</td>";
echo "</tr>";
}
echo "</table>";
}
?>
</div> <!-- well -->
Also, you may want to check that $results and $row contain the expected data in the expected formats using print_r($results) and print_r($row).
I am doing a project and would be eternally grateful for help in getting my URl's to link. I have tried looking around to no avail. I have a database (4columns). The last one (link1) should link to videos with the specified URL.When the table comes up the URL's are not clickable (is there a way to simplify this say "click me"?). Here is my code. I've also attached an image of the table. This is really busting my brains, thanks.
<?php
$con = mysqli_connect("localhost","feedb933_charles","pass100","feedb933_test");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "SELECT * FROM videos";
$result = mysqli_query($con, $sql);
echo "<table>";
echo "<tr>
<th>topic1</th>
<th>subject1</th>
<th>link1</th>
</tr>";
while( $row = mysqli_fetch_array( $result)) {
$topic1 = $row["topic1"];
$subject1 = $row["subject1"];
$link1 = $row["link1"];
echo "<tr>
<td>$topic1</td>
<td>$subject1</td>
<td>$link1</td>
</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Table output
Try this:
<?php
$sql = "SELECT * FROM `videos`";
$result = mysqli_query($con, $sql);
?>
<table>
<?php
while($row = mysqli_fetch_assoc( $result)) {
?>
<tr>
<td><?php echo $row['topic1'];?></td>
<td><?Php echo $row['subject1'];?></td>
<td><a href="<?php echo $row['link1']; ?>" target="_blank">Click me</td>
</tr>
<?php } ?>
<table>
Or you can also use do while loop:
do{
echo '<tr>';
echo '<td>'.$row['topic1'].'</td>';
echo '<td>'.$row['subject1'].'</td>';
echo '<td><a href="'.$row['link1'].'" target="_blank">Click me</td>';
echo '</tr>';
} while($row = mysqli_fetch_assoc( $result);
I added the target attribute to open the link in a new window.
I looked at your code and i found a couple errors.
change $con = mysqli_connect("localhost","feedb933_charles","pass100","feedb933_test"); to $con = new mysqli("localhost", "feedb933_charles", "pass100", "feedb933_test");
Then change if (mysqli_connect_errno()) to if (mysqli_connect_error()) {
Then change
$sql = "SELECT * FROM videos";
to
$sql = "SELECT topic1, subject1, link1 FROM videos";
or if you want to select one row
$differentvalue = ""; // value to run
$sql = "SELECT topic1, subject1, link1 FROM videos WHERE difvalue = ?";
difvalue is the value different frm the rest so the php code knows what to grab.
Using stmt means no sql injection
Add:
$stmt = $con->stmt_init();
if (!$stmt->prepare($sql))
{
print "Failed to prepare statement\n";
}
else
{
}
Then in side if (something) { } else { IN HERE }
(if you have WHERE diffvalue) Add:
$stmt->bind_param("s", $differentvalue); // $stmt->bind_param("s", $differentvalue); if text, $stmt->bind_param("i", $differentvalue); if integer
Then
Add:
$stmt->execute();
$list = $stmt->fetchAll();
then outside the if (something) { code } else { code }
Add:
echo "<table><tr><th>topic1</th><th>subject1</th><th>link1</th></tr><tr>";
foreach ($list as $row => $new) {
$html = '<td>' . $new['topic1'] . '</td>';
$html .= '<td>' . $new['subject1'] . '</td>';
$html .= '<td>' . $new['link1'] . '</td>';
echo $html;
}
echo "</tr></table>";
If you are still having problems goto the links listed
http://php.net/manual/en/pdostatement.fetchall.php
http://php.net/manual/en/mysqli-stmt.get-result.php
http://php.net/manual/en/mysqli-result.fetch-all.php
Hope this helps
<?php
$con=mysqli_connect("localhost","feedb933_charles","pass100","feedb933_test");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "SELECT * FROM videos";
$result = mysqli_query($con, $sql);
echo "<table>";
echo "<tr>
<th>topic1</th>
<th>subject1</th>
<th>link1</th>
</tr>";
while( $row = mysqli_fetch_array( $result)) {
$topic1 = $row["topic1"];
$subject1 = $row["subject1"];
$link1 = $row["link1"];
echo "<tr>
<td>$topic1</td>
<td>$subject1</td>
<td>$link1</td>
*//this href will give u the link as u asked. //be sure you store proper url. In case of exact video name saved in column thn can make the url for your web output. //ex:<a href="http://example.com/'.$link.'.extension">*
</tr>";
}
echo "</table>";
mysqli_close($con);
?>
At the moment I have the below script which auto generates the table names and row data automatically by looking at a sqlite table. So regardless of if you have 2 or 10 columns this script works.
At the moment the script outputs the results like this:
Output currently appears as a Row
I have tried altering the script so that it outputs the results like below. Can someone assist or guide me in the right direction to achieve this?
Is it possible to output the results of the query in the below format: going down in a column rather than across as a row ?
Output should appear as a Column
<?
$ED = $_GET['ED'];
$ID = $_GET['ID'];
$table_name = $_GET['table'];
?>
<table border="1">
<tr>
<td>
<table>
<?php // Display all sqlite column names for chosen table
$tablesquery = $db->query("PRAGMA table_info($table_name)");
while ($table = $tablesquery->fetchArray(SQLITE3_ASSOC)) {
if ($table['name'] == "ID") {
echo "<tr><td>" . $table['name'] . "</td></tr>";
} else {
$table_name_header = ucwords(strtolower(str_replace('_', ' ', $table['name'])));
echo "<tr><td>" . $table_name_header . "</td></tr>";
}
}
?>
</table>
</td>
<td>
<table>
<?
// Display all sqlite data for chosen table
$tablesquery = $db->query("PRAGMA table_info($table_name)");
$columns = array();
while ($table = $tablesquery->fetchArray(SQLITE3_ASSOC)) {
$columns[] = $table['name'];
}
// Display * from USERS
// $results = $db->query('SELECT * FROM ADMIN_LOGIN WHERE ID = "57"');
$results = $db->query('SELECT * FROM ' . $table_name . ' WHERE ID = "' . $ID . '"');
while ($row = $results->fetchArray()) {
// echo "<tr>";
$test = $row[0];
foreach ($columns as $col)
echo "<tr><td>" . $row[$col] . "</td></tr>";
}
// echo "</tr>";
?>
</table>
</td>
</tr>
</table>
Modifying the code to the below by putting the data into an combined array and then pulling it back via a loop it will display as required:
<?
// Display all sqlite data for chosen table
$tablesquery = $db->query("PRAGMA table_info($table_name)");
$columns = array();
while ($table = $tablesquery->fetchArray(SQLITE3_ASSOC)) {
$columns[] = $table['name'];
}
// Display * from USERS
// $results = $db->query('SELECT * FROM ADMIN_LOGIN WHERE ID = "57"');
$results = $db->query('SELECT * FROM ' . $table_name . ' WHERE ID = "' . $ID . '"');
while ($row = $results->fetchArray()) {
// echo "<tr>";
$test = $row[0];
foreach ($columns as $col)
$column_data[] = $row[$col];
// echo "<tr><td>" . $row[$col] . "</td></tr>";
}
// echo "</tr>";
?>
<?
$array = $columns;
$array2 = $column_data;
$result = array_combine($array, $array2);
//print_r($result);
?><br><br>
<center>
<table border="0" cellpadding="2" cellspacing="2" color="#4B708D">
<thead>
<?
foreach($result as $key => $value) {
echo "<tr><td bgcolor='#c6d5e1'>$key</td><td bgcolor='#FFFFFF'>$value</td></tr>";
}
?>
</thead>
</table>
I would like to have the freedom to place a row entry from my database wherever i' prefer in the page. Right now, the php code that I use is as follows (it is clean working code):
<html><head></head>
<body>
<?php
$db = mysql_connect("xxx","xxx","xxx") or die("Database Error");
mysql_select_db("caisafety",$db);
$id = $_GET['id'];
$id = mysql_real_escape_string($id);
$query = "SELECT * FROM `cert_rr` WHERE `id`='" . $id . "'";
$result = mysql_query($query);
echo $row['id']; while($row = mysql_fetch_array( $result )) {
echo "<br><br>";
echo $row['basic3'];
echo $row['basic2'];
echo $row['basic1'];
}
?>
</body>
</html>
I call id through the browser Eg. http://site.com/getid.php?id=10 . But I do not have the freedom to place my row entry within my html. For eg. like this:
<table><tr>
<td align="center">BASIC INFO 1: <?php echo $row['basic1']; ?></td>
<td align="center">BASIC INFO 2: <?php echo $row['basic2']; ?></td>
</tr></table>
I can place html within echo PHP tags but then I have to clean up my html and thats a lot of work. Retaining HTML formatting would be preferred. Any help or guidelines on this would be much appreciated.
<?php
$db = mysql_connect("xxx","xxx","xxx") or die("Database Error");
mysql_select_db("caisafety",$db);
$id = $_GET['id'];
$id = mysql_real_escape_string($id);
$query = "SELECT * FROM `cert_rr` WHERE `id`='" . $id . "'";
$result = mysql_query($query);
//you need to retrieve every row and save to an array for later access
for($rows = array(); $tmp = mysql_fetch_array($result);)
{
$rows[] = $tmp;
}
//now you can use the $rows array where every you want e.g. with the code from Zhube
?>
....
<table><?php foreach($rows as $r):
<td><?php echo $r['id'] ?></td><?php endforeach ?>
</table>
By
while($row = mysql_fetch_array( $result )) {
echo "<br><br>";
echo $row['basic3'];
echo $row['basic2'];
echo $row['basic1'];
}
you save only the last row
Instead of having your HTML tags as part of the PHP variable, do something like this instead:
<table><?php foreach($row as $r):?>
<td><?php echo $r['id'] ?></td><?php endforeach ?>
</table>
I am trying to populate a Drop down box from results of a mySQL Query, in Php. I've looked up examples online and I've tried them on my webpage, but for some reason they just don't populate my drop down box at all. I've tried to debug the code, but on the websites I looked at it wasn't really explained, and I couldn't figure out what each line of code. Any help would be great :)
Here's my Query: Select PcID from PC;
You will need to make sure that if you're using a test environment like WAMP set your username as root.
Here is an example which connects to a MySQL database, issues your query, and outputs <option> tags for a <select> box from each row in the table.
<?php
mysql_connect('hostname', 'username', 'password');
mysql_select_db('database-name');
$sql = "SELECT PcID FROM PC";
$result = mysql_query($sql);
echo "<select name='PcID'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['PcID'] . "'>" . $row['PcID'] . "</option>";
}
echo "</select>";
?>
Below is the code for drop down using MySql and PHP:
<?
$sql="Select PcID from PC"
$q=mysql_query($sql)
echo "<select name=\"pcid\">";
echo "<option size =30 ></option>";
while($row = mysql_fetch_array($q))
{
echo "<option value='".$row['PcID']."'>".$row['PcID']."</option>";
}
echo "</select>";
?>
Since mysql_connect has been deprecated, connect and query instead with mysqli:
$mysqli = new mysqli("hostname","username","password","database_name");
$sqlSelect="SELECT your_fieldname FROM your_table";
$result = $mysqli -> query ($sqlSelect);
And then, if you have more than one option list with the same values on the same page, put the values in an array:
while ($row = mysqli_fetch_array($result)) {
$rows[] = $row;
}
And then you can loop the array multiple times on the same page:
foreach ($rows as $row) {
print "<option value='" . $row['your_fieldname'] . "'>" . $row['your_fieldname'] . "</option>";
}
No need to do this:
while ($row = mysqli_fetch_array($result)) {
$rows[] = $row;
}
You can directly do this:
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['value'] . "'>" . $row['value'] . "</option>";
}
At the top first set up database connection as follow:
<?php
$mysqli = new mysqli("localhost", "username", "password", "database") or die($this->mysqli->error);
$query= $mysqli->query("SELECT PcID from PC");
?>
Then include the following code in HTML inside form
<select name="selected_pcid" id='selected_pcid'>
<?php
while ($rows = $query->fetch_array(MYSQLI_ASSOC)) {
$value= $rows['id'];
?>
<option value="<?= $value?>"><?= $value?></option>
<?php } ?>
</select>
However, if you are using materialize css or any other out of the box css, make sure that select field is not hidden or disabled.
After a while of research and disappointments....I was able to make this up
<?php $conn = new mysqli('hostname', 'username', 'password','dbname') or die ('Cannot connect to db') $result = $conn->query("select * from table");?>
//insert the below code in the body
<table id="myTable"> <tr class="header"> <th style="width:20%;">Name</th>
<th style="width:20%;">Email</th>
<th style="width:10%;">City/ Region</th>
<th style="width:30%;">Details</th>
</tr>
<?php
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>".$row['username']."</td>";
echo "<td>".$row['city']."</td>";
echo "<td>".$row['details']."</td>";
echo "</tr>";
}
?>
</table>
Trust me it works :)
What if you want to use both id and name in the dropdown? Here is the code for that:
$mysqli = new mysqli($servername, $username, $password, $dbname);
$sqlSelect = "SELECT BrandID, BrandName FROM BrandMaster";
$result = $mysqli -> query ($sqlSelect);
echo "<select id='brandId' name='brandName'>";
while ($row = mysqli_fetch_array($result)) {
unset($id, $name);
$id = $row['BrandID'];
$name = $row['BrandName'];
echo '<option value="'.$id.'">'.$name.'</option>';
}
echo "</select>";