How can i display my data better. I'm having problems [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
$sql = "SELECT moduleCode, moduleTitle FROM TIMETABLE";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table>";
echo "<tr><td>Module Code</td><td>Module Title</td></tr></br>";
while($row = $result->fetch_assoc()) {
echo "<tr><td>". $row["moduleCode"]. "</td><td>". $row["moduleTitle"]. "</td></tr></br>";
echo "</table>";
}
}
else {
echo "0 results";
}
I am having a problem with the layout of the data once its displayed from the database. Currently, the first record will be displayed in a clear table format, then any other record after that is just 'bunched up'. Any idea on how to solve this?

Just like you started your <table> before the loop, you need to close it after the loop, not inside it. Also, no HTML (like <br>) is allowed between the cells, so remove the <br>. Try this:
$sql = "SELECT moduleCode, moduleTitle FROM TIMETABLE";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table>";
echo "<tr><td>Module Code</td><td>Module Title</td></tr>";
while($row = $result->fetch_assoc()) {
echo "<tr><td>". $row["moduleCode"]. "</td><td>". $row["moduleTitle"]. "</td></tr>";
}
echo "</table>";
}
else {
echo "0 results";
}

Related

How do I show only the column name of the SQL table [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
Currently my code consists of
$sql = "SHOW columns FROM tblexercise";
$result = mysqli_query($conn,$sql);
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
foreach ($row as $field => $value) {
echo "<td>" . $value . "</td>";
}
echo "</tr>";
}
This allows me to show the column names but it also includes all the attributes and types etc.
is there any way to show just the column name?
You don't need a loop inside a loop. You only need one foreach loop and then you can access the key Field which holds the name of the column. The query SHOW COLUMNS is explained in the MySQL doc. You can check in that link what are the results of this query and their sample values. Then you can decide which values you want to access.
$result = $conn->query("SHOW columns FROM tblexercise");
foreach ($result as $row) {
echo "<tr>";
echo "<td>" . $row['Field'] . "</td>";
echo "</tr>";
}

how to display uploaded picture . php ,mysql [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
hey guys i am stuck at very end point of my code , my aim was to Store image path to database and store the image to a directory. and than i want to show those images.
i am successfully done uploading part but now i am stuck how can i show those images on my webpage, here is my code
<?php
$sql = "SELECT * FROM pictures";
$result = mysqli_query($conn, $sql);
$data = mysqli_fetch_array($result,MYSQLI_ASSOC);
while($row = mysqli_fetch_array($result))
{
$image_path=$row["folder_name"];
$image_name=$row["picture_name"];
echo "img src=".$image_path."/".$image_name." width=100 height=100";
}
?>
If you want to fetch associative arrays, use fetch_assoc instead, your $data is not needed here, it's simpler at the end. Also, only select the fields you need in your query. To fix your problem, You are simply missing the HTML , try this:
<?php
$sql = "SELECT folder_name, picture_name FROM pictures";
$result = mysqli_query($conn, $sql);
if(!$result) {
die("Error with your Query: " . mysqli_error($conn));
}
while($row = mysqli_fetch_assoc($result)) {
$image_path = $row["folder_name"];
$image_name = $row["picture_name"];
echo "<img src=\"" . $image_path . "/" . $image_name . "\" width=\"100\" height=\"100\"></img>\n";
}
?>

Entries from database to dropdown list simple php code [closed]

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.

mysql_fetch_array, not valid - combining tables in one php page [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
i've got the following code
$data = mysql_query(
"SELECT md.*, qr.*
FROM moduleDetails md
LEFT JOIN qResponses qr
ON qr.userno = md.userno");
print "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data ))
{
print "<tr>";
print "<th>Faculty</th> <td>".$info['faculty'] . "</td>";
print "<th>Module Code</th> <td>".$info['moduleCode'] . "</td>";
print "<th>Date Started</th> <td>".$info['dateStarted'] . "</td>";
print "<th>Module Title</th> <td>".$info['moduleTitle'] . "</td>";
print "<th>School</th> <td>".$info['school'] . "</td></tr>";
}
print "</table>";
it's giving me a error on line 31, which is the $info = mysql_fetch.... etc
What have i done wrong?.. i can't see a fix for this, it wasnt working fine before before i involved two tables.. any help would be great - cant see whats wrong - new to joining tables.
Probably you get no results from the query. Execute the query to verify it gives result and check you have results before trying to fetch them
if (mysql_num_rows($data) == 0)
die("No Records");
elseif (mysql_num_rows($data) > 0)
{
while($info = mysql_fetch_array( $data ))
{
...............
}
}
else { die("Something else went wrong"); }
Or even better wrap it in a try/catch

Make Query Data Become Link [closed]

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).

Categories