Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a database table called 'partners'. I want to echo this table into a html/php table which has 5 columns id, channel, email, paypal, and network. I also wan't a form that adds new people to the table through the website.
<?php
mysql_connect("localhost", "mysql_user", "mysql_password"); //your parameters here
mysql_select_db("mydb"); //you DB name here
$q = "SELECT id, channel, email, paypal, network FROM partners;";
$result = mysql_query($q);
echo "<table>";
echo "<tr><td>id</td><td>channel</td><td>email</td><td>paypal</td><td>network </td></tr>";
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "<tr>";
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
echo "<td>".$row[4]."</td>";
echo "</tr>";
}
echo "</table>";
mysql_free_result($result);
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a material record database.
material_records
poly canvass
metal
washer
knot
how to call this entries into a dropdown list.. please help.. i don't know how to call the entries into a dropdown. Happy new year.. :)
Create a query
$handle = mysql_connect("127.0.0.1", "username", "password");
if($handle) {
#mysql_select_db("database_name", $handle);
$query = "SELECT * FROM material_records";
$result = mysql_query($query, $handle);
}
Fetch and loop the results and print a select
if(isset($result)) {
if(mysql_num_rows($result) > 0) {
echo '<select name="mydropdrown">';
while($row = mysql_fetch_object($handle)) {
echo '<option value="blub">'.htmlspecialchars($row["poly_canvass"]).'</option>';
echo '<option value="blub">'.htmlspecialchars($row["metal"]).'</option>';
echo '<option value="blub">'.htmlspecialchars($row["washer"]).'</option>';
echo '<option value="blub">'.htmlspecialchars($row["knot"]).'</option>';
}
echo '</select'>;
}
}
$query = "SELECT description FROM material_records";
$result = $mysqli->query($query);
while($row = $result->fetch_array())
{
echo "<option>" . $row['description'] . "</option>";
}
This is one of the simplest ways to get the values for your dropdown.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
As mentioned in the title, I would like to know how can I make the queried data become a link to another php file. For example, referring to the pic attached below, I would like to show all the cities of United States by just simply click on it and same goes with United Kingdom. I want to make like when I click on any of the state, the php file link to it will automatically know which state I've selected and generate its corresponding cities.
The following is my code:
<html>
<head><title>State</title></head>
<body>
<?php
$dbh = pg_connect("host=localhost dbname=state user=postgres");
if (!$dbh) {
die("Error in connection: " . pg_last_error());
}
$sql = "SELECT * FROM state ";
echo "<table>";
echo "<table border=\"1\" align=\"center\">";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>State</th>";
echo "</tr>";
$result = pg_query($dbh, $sql);
if (!$result) {
die("Error in SQL query: " . pg_last_error());
}
while ($column = pg_fetch_array($result)) {
echo "<tr>";
echo "<td>".$column[0]."</td>";
echo "<td>".$column[1]."</td>";
echo "</tr>";
}
echo "</table>";
pg_free_result($result);
pg_close($dbh);
?>
</body>
</html>
in your current php file:
// Replace your while statement in your code above with this...
$row_counter = 0;
while ($row = pg_fetch_array($result,$row_counter,PGSQL_ASSOC)) {
echo "<tr>";
echo "<td>".$row['id']."</td>";
echo "<td><a href='state.php?id=".$row['id']."'>".htmlentities($row['state'])."</a></td>";
echo "</tr>";
$row_counter++;
}
And in another php file, let's say state.php, you could run something like this:
$state_id = $_GET['state_id'];
// connect to database...
$sql = "SELECT * FROM some_table WHERE state_id = '".pg_escape_string($state_id)."'";
// run your query...
Note: htmlentities will prevent XSS problems and pg_escape_string will help prevent SQL injection (but research prepared statements for a better approach).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I am trying to make a simple function that creates an HTML table that first gets the headers in th-tags and then dumps all the rows into html table rows.
Here's what I got so far.
function dumpMysqlTable($tblName) {
$result=mysql_query("SELECT * FROM `$tblName`");
echo "<table>";
echo "<tr>";
$i=0;
while($i<mysql_num_fields($result)) {
$fields=mysql_fetch_field($result, $i);
echo "<th>".$fields->name."</th>";
$i++;
}
echo "</tr>";
echo "<tr>";
$j=0;
while($j<mysql_num_rows($result)) {
$k=0;
while($row=mysql_fetch_array($result)) {
echo "<td>".$row[$k]."</td>";
$k++;
}
$j++;
}
echo "</tr>";
echo "</table>";
}
However, it messes up badly, and picks rows from the table its not supposed to and I just can't get it to work. Please help :)
You are making several mistakes.
while($row=mysql_fetch_array($result)) {
will return a whole row (or line) of the table. So $row[$k] references a row (one line in the table), not a cell value.
Then you should know that mysql_fetch_array by default will return an array with both numerical keys and associative indices. So you should either use mysql_fetch_array( $result, MYSQL_NUM ) or simply mysql_fetch_row(). Then you can loop through the row:
while( $row = mysql_fetch_row( $result ) ) {
echo "<tr>";
foreach( $row as $cell )
{
echo "<td>".$cell."</td>";
}
echo "</tr>";
}
Notice you also forgot to open a new tr for each line.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a form and when a user submits the form his ip is logged. I need a way when 2 or more ips match using tables at output to color only the rows with the same ip
<?php
// Connects to your Database
mysql_connect("localhost", "pp", "mygas13") or die(mysql_error());
mysql_select_db("pp") or die(mysql_error());
$data = mysql_query("SELECT * FROM userTable")
or die(mysql_error());
Print "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data ))
{
Print "<tr>";
Print "<th>Name:</th> <td>".$info['nick'] . "</td> ";
Print "<th>Ip:</th> <td>".$info['ip'] . " </td></tr>";
}
Print "</table>";
?>
Like this?
if($ip = $sameip) {
echo "<td style='background: #fffff;'>$ip</td>";
} else {
echo "<td>$ip</td>";
}
This is a bit of a guess due to no code being shown but I think that's what your after?
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 have 2 php files that retrieve a BLOB image from mysql DB. My DB has stored a couple of different images, as i try to display them on the browser, only the first image is displayed multiple times, for example: The DB table has stored 5 images, the browser is going to display the first image 5 times.
here is a snippet from my main php file:
$strSQL = "SELECT * FROM images";
$rs = mysql_query($strSQL) or die (mysql_error());
echo "<table>";
while($row = mysql_fetch_array($rs)) {
echo "<tr><td>";
echo " <img src=load_pic.php?id=".$row["id"]." id='img' width='100' height='100'></a>";
echo "</td></tr>";
}
echo "</table>"
and the php file that gets the images "load_pic.php"
$q="select * from images";
$rec=mysql_fetch_array(mysql_query($q));
$data=$rec['image'];
header('Content-Length: '.strlen($data));
header("Content-type: image/".$rec['type']);
echo $data;
Your load_pic.php script is not using the id parameter. It should be:
$q = "select * from images where id = " . mysql_real_escape_string($_GET['id']);