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";
}
?>
Related
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 6 years ago.
Improve this question
this my code but unable to display image from db in php.i think this is not pick up the path.any one can help in this regard.
<?php
include('connect.php');
$result = $db->prepare("SELECT image FROM info WHERE empid= '". $empid ."'");
$result->bindParam('. $empid .', $empid);
$result->execute();
for($i=0; $rows = $result->fetch(); $i++){
echo '<img src="images/".$row["image"]." ">';
echo '<img src="images/".$row["image"]. > ' ;
}
?>
You either concatenate the id onto the text string containing the query or use a parameter place holder and then bind a value to it. Not both, as you were doing.
The most secure way is to use parameters.
<?php
include('connect.php');
// I assume you have set $empid somewhere in the missing code here
$result = $db->prepare("SELECT image FROM info WHERE empid= :empid");
$result->bindParam(':empid', $empid, , PDO::PARAM_INT);
$result->execute();
while ($row = $result->fetch(PDO::FETCH_ASSOC)){
// also changed these 2 rows to correct the concatenation
echo '<img src="images/"' . $row["image"] . '">';
echo '<img src="images/"' . $row["image"] . '">';
}
?>
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
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.
Improve this question
So I have the following query to show all users from my database:
<ul class="names">
<table>
<tr>
<th>Navn</th>
<th>Email</th>
<th>Score</th>
</tr>
<?php
$connection = mysql_connect('localhost', 'users', 'password'); //The Blank string is the password
mysql_select_db('users');
$query = ("SELECT * FROM oneusers"); //You don't need a ; like you do in SQL
$result = mysql_query($query);
$row=mysql_fetch_array($result);
while($row = mysql_fetch_array($result)){ //Creates a loop to loop through results
echo "<tr><td>" . $row['iUserName'] . "</td><td>" . $row['iUserEmail'] . "</td><td>" . $row['iUserCash'] . " DKK</td></tr>"; //$row['index'] the index here is a field name
}
mysql_close(); //Make sure to close out the database connection
?>
</table>
</ul>
But for some reason it doesnt show all the data.
In only shows the user with iUserId 2 and not one.
Does anyone have an idea of what might be wrong?
I can log in with iUserId 1's credentials, and it shows fine the info on the login page.
But not here :S
Remove line $row = mysql_fetch_array($result);
Because this line starts to fetching records from your query results. First fetched record is record with id 1 and you do nothing with it.
Then you start echoing other records, but record with id 1 is already skipped.
You fetch the first row before your while loop is defined; once you enter the while loop it fetches the next row, which is the second. Remove the first mysql_fetch_array($result) operation.
Incidentally, also, the original mysql api in PHP is deprecated, it is recommended to use mysqli instead.
I think you should put it this way though
<?php
$connection = mysql_connect('localhost', 'users', 'password'); //The Blank string is the password
mysql_select_db('users');
let all the connection be outside the "ul" tag.
$query = ("SELECT * FROM oneusers"); //You don't need a ; like you do in SQL
$result = mysql_query($query);
$query = ("SELECT * FROM oneusers"); //You don't need a ; like you do in SQL
$result = mysql_query($query);
$row=mysql_fetch_array($result);
?>
<ul class="names">
<table>
<tr>
<th>Navn</th>
<th>Email</th>
<th>Score</th>
</tr>
<?php
while($row){ //Creates a loop to loop through results
echo "<tr><td>" . $row['iUserName'] . "</td><td>" . $row['iUserEmail'] . "</td><td>" . $row['iUserCash'] . " DKK</td></tr>"; //$row['index'] the index here is a field name
}
mysql_close(); //Make sure to close out the database connection
?>
</table>
</ul>
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 needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
At bottom of INSERT php/sql code, i use echo to print out query result to fix errors, in this way:
... code ...
$result = MYSQL_QUERY($sqlQuery);
echo $sqlQuery
Exist a solution to print out UPDATE and DELETE queries like above?
TKS All
Based on the comments to OP, you need to change the way you run the query if you want this to work. Below is how I would've done it.
$update = "UPDATE mod_document_images SET image_os_res = '" . $checkbox_os . "', image_ne_aut = '" . $checkbox_ne . "', image_ge_cat = '" . $image_gen_cat . "' WHERE image_id = " . $image_id;
echo "Query: " . $query; // Echo the query containing the variables supplied
$result = mysql_query($update); // Run the update and store the result in $result
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