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>
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 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 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
im trying to create a drop down many and populate the options from fields in a database that i have.
So far i have got the following code:
<?php
session_start();
include_once("config.php");
$query = "SELECT Category FROM books";
$result = mysqli_query ($mysqli, $query);
echo "<select name=dropdown value=''>Dropdown</option>";
while($row = mysqli_fetch_array($result))
{
echo "<option value=$row[Category]>$row[Category]</option>";
}
echo "</select>";
?>
</div>
</body>
</html>
However when i try to view the page nothing seems to happen.
My page remains blank and all i see is the color that i gave to the body in the css file.
Was wondering if anyone knew wh this was happening and if they can sort it out?
Thanks!
Assuming you are getting results from your query. Can find out by print_r($result). Think you are getting death screen because of quotes and missing opening for first option in dropdown.
echo '<select name="dropdown" value=""><option value="">Dropdown</option>';
while($row = mysqli_fetch_array($result))
{
echo '<option value="' . $row['Category'] . '">' . $row['Category'] . '"</option>';
}
echo "</select>";
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 8 years ago.
Improve this question
I have a form using a POST method, upon enter a name and clicking search, it is supposed to display the information from the database, with the term you used.
index.php
<form action="search.php" method="POST">
<input type="text" name="search" /><br/><br/>
<input type="Submit" value="Search" />
</form>
Search.php
<?php
if (!$_POST) {
include('index.php');
} else {
?>
<h1>Server name<br />Official ItemDB!</h1>
<br />
<?php
$search = $_POST["search"];
MySQL_connect("localhost", "pernix_items", "#");
MySQL_select_db("pernix_items");
$result = mysql_query("SELECT * FROM items WHERE name LIKE '%" . mysql_real_escape_string($search) . "%'");
while($row = mysql_fetch_array($result));
{
?>
<table border="1">
<?
echo '<tr>';
echo '<td>'.$row['id'] . '</td>';
echo '<td>' . $row['name'] . '</td>';
echo '<td>' . $row['desc'] . '</td>';
echo '</tr>';
}
}
?>
</tr>
</table>
I added the mysql_error(); function to dertime where I was going wrong, to this
$result = mysql_query("SELECT * FROM items WHERE name LIKE '%" . mysql_real_escape_string($search) . "%'" or die(mysql_error()));
and gives me this
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home1/pernix/public_html/tools/item-list/search.php on line 21
My line 21 of search.php
while($row = mysql_fetch_array($result));
So I added another or die print error.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1
Any ideas?
Although resolved in the comments, reposting it here as a whole answer.
First the or die() line is wrong, as the die statement is executed inside the mysql_query and not as supposed to when and if the query fails, as it should be:
$result = mysql_query("SELECT * FROM items WHERE name LIKE '%" . mysql_real_escape_string($search) . "%'") or die(mysql_error());
Secondly you have an extra semicolon right after the while() line making, which should be removed to look like:
while($row = mysql_fetch_array($result))
{}
Last and most importantly you should really convert this code to mysqli or pdo as the mysql extension is deprecated as of PHP 5.5.0, and will be removed in the future.
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
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).