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
Related
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>";
}
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";
}
?>
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";
}
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 needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I cannot figure out how to get my code to create a new HTML column for results, in my table, in a while ($row = mysql_fetch_array($result)) { - when the results returned reach 10, I want a new table column to be created in the same HTML table.
How can I do this? The code I'm using is:
while ($row = mysql_fetch_array
($result, MYSQL_ASSOC)) {
$row_color = ($row_count % 2) ? $color1 : $color2;
echo '<tr><td align="left" bgcolor=' . $row_color . '> <b>' . $row['manufacturer'] . '</b>: <a href=view_inventory.php?mdl_key=' . $row['mdl_key'] . '&man_key=' . $row['man_key'] . '&cls_key=' . $row['cls_key'] . '&sub_cls_key=' . $row['sub_cls_key'] . '> ' . $row['model'] . '</a></b></td></tr>';
$row_count++;
}
You should really use the new PDO interface as the mysql extension is deprecated long ago, read this documentation if you have a choice.
Still, with the old extension, simply do this:
$rowCount = mysql_num_rows($result);
if ($rowCount >= 10) {
while ($row = mysql_fetch_array($result)) {
// Do your extra column stuff here.
}
}
else {
while ($row = mysql_fetch_array($result)) {
// Do your normal stuff here.
}
}
// It is a good practise to remove variables after
// loops, this helps releasing memory in large scripts.
unset($rowCount, $row, $result);