I kinda messed up the title, but i will try to explain my problem. i have a html page called leden.html and have a PHP script on it which gets data from my database and creates a table on the html page. Now the part where i get stuck is showing if a member is online and if someone is online the $sql1= "ja" else $sql1= "nee", but i messed up somewhere because when two people are online, the last person who came online shows online and the first dude goes back to "nee". Here is the code, i think something goes wrong at the array part.
<?php
$conn = mysqli_connect("******", "******", "******", "******");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, username, email FROM register";
$sessie_username = "SELECT username FROM sessie";
$result = $conn->query($sql);
$result1 = $conn->query($sessie_username);
$row1 = $result1->fetch_assoc();
$nameninsessie = array($row1["username"]);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
if (in_array($row["username"], $nameninsessie)) {
$sql1 = "Ja";
} else {
$sql1 = "Nee";
}
echo "<tr><td>" . $row["id"]. "</td><td>" . $row["username"] . "</td>
<td>". $row["email"]. "</td><td>" . $sql1 . "</td></tr>";
}
echo "</table>";
} else { echo "0 resultaten"; }
$conn->close();
?>
You are only getting ONE of the logged in users from the sessie_username query. And also building the array of logged in users incorrectly. See below
<?php
$conn = mysqli_connect("******", "******", "******", "******");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, username, email FROM register";
$sessie_username = "SELECT username FROM sessie";
$result = $conn->query($sql);
$result1 = $conn->query($sessie_username);
// initialise the array
$nameninsessie = array();
// loop over all logged in users
while ( $row1 = $result1->fetch_assoc() ) {
// add thir names to an array
$nameninsessie[] = $row1["username"];
}
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
if (in_array($row["username"], $nameninsessie)) {
$sql1 = "Ja";
} else {
$sql1 = "Nee";
}
echo "<tr><td>" . $row["id"]. "</td><td>" . $row["username"] . "</td>
<td>". $row["email"]. "</td><td>" . $sql1 . "</td></tr>";
}
echo "</table>";
} else { echo "0 resultaten"; }
$conn->close();
?>
There is a problem in your data fetching. See the returning array using print_rcommand. You only get the first value of the sql query. So try this. Furthermore you have to initialize the array if not when there is 0 results for the mysql query it will give an error.
<?php
$conn = mysqli_connect("localhost", "root", "*****", "*****");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, username, email FROM register";
$sessie_username = "SELECT username FROM sessie";
$result = $conn->query($sql);
$result1 = $conn->query($sessie_username);
$nameninsessie = array();
$i=0;
while($row1 = $result1->fetch_assoc()) {
$nameninsessie[$i] = $row1["username"];
$i++;
}
print_r($nameninsessie); //thi is to chek the array when problem solved please comment this line
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
if (in_array($row["username"], $nameninsessie)) {
$sql1 = "Ja";
} else {
$sql1 = "Nee";
}
echo "<tr><td>" . $row["id"]. "</td><td> " . $row["username"] . "</td>
<td>". $row["email"]. "</td><td> " . $sql1 . "<br></td></tr>";
}
echo "</table>";
} else { echo "0 resultaten"; }
$conn->close();
?>
Related
So I am trying to display an array from a database that I have. When I run the script the script, I get an internal server error. Now I am not sure if this has to do with my config script or if I am not cycling through my array properly.
include 'config.php';
$conn = name2;
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Feild FROM Season 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Feild " . $row["Feild"]. " "<br>";
}
} else {
echo "0 results";
}
The syntax you have used to display the results was incorrect, replace:
echo "Feild " . $row["Feild"]. " "<br>";
With:
echo 'Feild '.$row["Feild"].'<br>';
You have not terminated your string properly.
Replace
echo "Feild " . $row["Feild"]. " "<br>";
With
echo "Feild " . $row["Feild"]. "<br>";
If you had errors turned on you would be getting an error stating a line number.
We need to know which column you're selecting from, as "SELECT Feild FROM Season 1" isn't valid. It should be something like "SELECT Feild FROM Season WHERE column = '1'"
With that in mind, this gets you closer to a solution:
include 'config.php';
$conn = name2;
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Feild FROM Season 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Feild ". $row["Feild"] ."<br>";
}
} else {
echo "0 results";
}
Use correct syntax
echo "Feild ".$row['Feild']."<br>";
I'm trying to query MySQL DB and print a single result on the page using PHP.
It's always going to be a single result, so I'm not sure if I need to loop
In any case, would anyone mind advising why the below doesn't work?
Thank you!!
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
else {
echo "success";
}
$sql = "SELECT sum(Discounted_Value) as id FROM Orders WHERE Year = 2017";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Total 2017 " $row["id"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
If there will be only one result you can simplify code to:
$sql = "SELECT sum(Discounted_Value) as id FROM Orders WHERE Year = 2017";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
echo "Total 2017 " . $row["id"] . "<br>"; // Don't forget dots for concatenation
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 have this problem with this while loop. I want to make a table where an administrator can see users and delete them. So I want this into a table. I have tried some echo's like:
echo <table>, echo <td>, echo </td>, echo </table>.
Unfortunately nothing of this al works.
Can anybody help me?
<?php
$account = 'Account:';
$password1 = 'Password:';
//check db connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Take everything from table and fill in $result
$sql = "SELECT * FROM login";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Take all data
while($row = $result->fetch_assoc()) {
//
echo $account. " " .$row["username"]. "<br> " . $password1 . " " . $row["password"]. "<br><br>";
}
} else {
// nothing in DB is 0 results
echo "0 results";
}
$conn->close();
?>
Simply print a table -
<table>
<tr>
<td>Account</td>
<td>Password</td>
</tr>
<?php
while($row = $result->fetch_assoc()) {
echo "<tr><td>" .$row["username"]. "</td><td>" . $row["password"]. "</td></tr>";
}
?>
</table>
<?php
$account = 'Account:';
$password1 = 'Password:';
//check db connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Take everything from table and fill in $result
$sql = "SELECT * FROM login";
$result = $conn->query($sql);
echo"<table><tr><td>Username</td><td>Password</td><td>Action</td></tr>";
if ($result->num_rows > 0) {
// Take all data
while($row = $result->fetch_assoc()) {
echo"<tr><td>".$row['username']."</td><td>".$row['password']."</td><td> modify | delete </td></tr>";
}
} else {
// nothing in DB is 0 results
echo "0 results";
}
echo"</table>";
$conn->close();
?>
$sql = "SELECT * FROM login";
$result = $conn->query($sql);
echo "<table>";
echo "<tr>";
echo '<td>Username</td>';
echo '<td>Password</td>';
echo '<td>Action</td>';
echo "</tr>";
if ($result->num_rows > 0) {
// Take all data
while($row = $result->fetch_assoc()) {
//
echo $account. " " .$row["username"]. "<br> " . $password1 . " " . $row["password"]. "<br><br>";
echo '<tr>';
echo '<td>'.$row["username"].'</td>';
echo '<td>'.$password1.'</td>';
echo '<td>Delete</td>';
echo '</tr>';
}
} else {
// nothing in DB is 0 results
echo "0 results";
echo '<tr><td colspan="3" rowspan="" headers="">No records Found</td></tr>';
}
echo "</table>";
$conn->close();
Replace your while loop with :
echo "<table><tr>
<td>Account</td>
<td>Username</td>
<td>Password</td>
</tr>"
while($row = $result->fetch_assoc())
{
echo "<tr><td>".$account."</td><td>" .$row["username"]. "</td><td>" . $password1 . "</td><td>" . $row["password"]. "</td></tr>";
}
echo "</table>";
Basically what I want is to assign the value of a field in my database to an variable. Can it be done in an effective way?
I was thinking something like:
$sql2 = mysql_query("SELECT * FROM rom WHERE idrom = 101");
while ($row = mysql_fetch_array($sql2)) {
$rom1 = $row['idrom'];
$status = $row['status'];
echo $rom1;
echo $status;
}
But this doesn't echo anything.
Edit:
I have gotten a bit longer on the way, now I am looking for a simpler way to assign the values to variables. As we speak I only need 4 values, but this still doesn't look like a very good way to accomplish what I want. Any better suggestions?
Heres what I got now:
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM rom WHERE idrom = 101";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$room101 = $row["idrom"];
$status101 = $row["status"];
echo "This is roomnumber ". $room101 . "!<br >";
echo "And the status of roomnumber ". $room101 ." is ". $status101 ."<br><br>";
}
} else {
echo "0 results";
}
$sql2 = "SELECT * FROM rom WHERE idrom = 102";
$result2 = $conn->query($sql2);
if ($result2->num_rows > 0) {
// output data of each row
while($row = $result2->fetch_assoc()) {
$room102 = $row["idrom"];
$status102 = $row["status"];
echo "This is roomnumber ". $room102 . "!<br >";
echo "And the status of roomnumber ". $room102 ." is ". $status102 ."<br><br>";
}
} else {
echo "0 results";
}
You can use bind_result to do that.
Please try this simple example, hope it run well :
$mysqli = mysqli_connect('host', 'user', 'pass','dbase')or die('Could not connect: ' . mysqli_error());
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Because you provide an Id in where clause, you can use it for the new variable
$output = array();
$id = 101;
$sql = "select idrom,status from rom where idrom=?";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param('i',$id);
$stmt->execute();
if ($stmt->errno == 0) {
$stmt->store_result();
// $stmt->bind_result($idrom,$status); // way 1
$stmt->bind_result($output[$id],$output["status".$id]); // way 2
while ($stmt->fetch()) {
echo $output[$id]." -> ".$output["status".$id]."<br />"; // So, your output variable will be like $output[101] for way 2
// or you defined it here like :
// $output[$idrom] = $idrom;
// $output["status".$idrom] = $status; // For way 1
}
} else {
return "Error: " . $sql . "<br>" . $stmt->error;
}
$stmt->close();
$mysqli->close();
When I print my code it only prints the question and description of id = 1 but not the rest of the table.
here is my code.
Please show me how to print my entire table which has like 20 questions or so...and also please show me how to make it so that the questions stay on the browser (even when I refresh the page) because currently the data does not stay on the browser when i refresh the page.
Thanks So Much!
<?php
require_once "connection.php";
if(isset($_POST['submit'])) {
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME );
if($conn->connect_error) {
die("connection error: " . $conn->connect_error);
} else {
echo "Submit button connected to database!";
}
$question = $_POST['question'];
$description = $_POST['description'];
$sql = " INSERT INTO `ask` (question_id, question, description) VALUES
(NULL, '{$question}', '{$description}' ) ";
if($conn->query($sql)) {
echo "it worked";
} else {
echo "error: " . $conn->error;
exit();
}
$query = "SELECT * FROM `ask` ";
if( $result = $conn->query($query)) {
$fetch = $result->fetch_assoc();
echo "<p>{$fetch['question']}</p>";
echo "<p>{$fetch['description']}</p>";
} else {
echo "failed to fetch array";
}
}
?>
You need a for each loop:
<?php
require_once "connection.php";
if(isset($_POST['submit'])) {
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME );
if($conn->connect_error) {
die("connection error: " . $conn->connect_error);
} else {
echo "Submit button connected to database!";
}
$question = $_POST['question'];
$description = $_POST['description'];
$sql = " INSERT INTO `ask` (question_id, question, description) VALUES
(NULL, '{$question}', '{$description}' ) ";
if($conn->query($sql)) {
echo "it worked";
} else {
echo "error: " . $conn->error;
exit();
}
$query = "SELECT * FROM `ask` ";
if( $result = $conn->query($query)) {
$fetch = mysql_fetch_array($result, MYSQL_ASSOC);
foreach($fetch as $ques) {
echo "<p>" . $ques['question'] . "</p>";
echo "<p>" . $ques['description'] . "</p>";
}
} else {
echo "failed to fetch array";
}
}
?>
All I've done there is change:
$fetch = $result->fetch_assoc();
echo "<p>{$fetch['question']}</p>";
echo "<p>{$fetch['description']}</p>";
to:
$fetch = mysql_fetch_array($result, MYSQL_ASSOC);
foreach($fetch as $ques) {
echo "<p>" . $ques['question'] . "</p>";
echo "<p>" . $ques['description'] . "</p>";
}
fetch_assoc() — Fetch a result row as an associative array
so it gets only 1 row you need to loop through the rest of the rows check the examples reference from php docs