So my 1st target is to convert expecting data from mySQL as JSON before use XML.
Things i trying to solve- Connect two tables from database for specific user and display it on screen.
Everything is in one PHP file:
<?php
include 'db.php';
session_start();
ob_start();
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM users
INNER JOIN user_quiz ON users.user_uid = user_quiz.user_uid
WHERE users.user_uid = '".$_SESSION['u_uid'] ."'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$outp = array();
$outp = $result->fetch_all(MYSQLI_ASSOC);
// for check if record exist
echo "<br> id: ". $row["id"]. " - Name: ". $row["user_first"]. " " . $row["user_last"] . " id:" . $row["user_uid"] . " Selected qustion " . $row["q_topic"] . " ". $row["q_id"] . "<br>";
}
} else {
echo "0 results";
}
$myJSON = json_encode($outp);
echo $myJSON;
?>
So now where is a issue.. When i do SQL like that:
SQL:
$sql = "SELECT * FROM users";
PHP is convert data as JSON data... but when i trying to do that for specific user i have empty []... That's why i use echo to see if i have output or i have error but... i have displays value... Had no idea what's going on... need advice.
Related
I have an MySQL data base that has a cell in a record called balance. The db shows it is stored as a decimal(16,2). When I try to display the vale of the record with echo $row["balance"]; In PHP, it display nothing at all. Can you please point me in the right direction. Thanks you.
$sql = "SELECT id, email, username FROM ppb_users WHERE id = '$USERIDX' ";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
//// output data of each row
while($row = $result->fetch_assoc())
{
echo "<br> id: " . $row["id"] . "<br> email: " . $row["email"] . "<br> username: " . $row["username"] . "<br> Ballance: " . $row["balance"] ."<br>";
$UserEmail = $row["email"];
$balancex = $row["balance"];
}
}
else
{
echo " 0 results <br><br>";
}
$conn->close();
Sorry folks, it would seem I had one of those moments in where when it is over and you realize what you did, how small you feel. I did not add balance to the SQL se3lect line. Uggg! Soryy.
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();
?>
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
I Currently have this code that displays all entries rows from a mysql table and displays them on a web page;
<?php
$servername = "localhost";
$username = "appuser1";
$password = "*****";
$dbname = "acmefg_app";
$row = mysql_fetch_array(mysql_query("SELECT jobnumber FROM appdata WHERE id = '5608' LIMIT 1"));
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, jobnumber, assetnumber FROM appdata";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> id: ". $row["id"]. "<br>";
echo "<br> Job number: ". $row["jobnumber"]. "<br>";
echo "<br> Asset number: " . $row["assetnumber"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
How can I change this so it only displays one result? Lets say in this case I wanted to display the row with an id of 5611? Many Thanks
If you want to show only one row then no need to use while() loop. Remove loop
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
echo "<br> id: ". $row["id"]. "<br>";
echo "<br> Job number: ". $row["jobnumber"]. "<br>";
echo "<br> Asset number: " . $row["assetnumber"] . "<br>";
}
Or can set LIMIT 1 in your query string.
SELECT id, jobnumber, assetnumber FROM appdata LIMIT 1