php mysql issue with printing value on html page - php

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

Related

Creating a PHP API (Convert data from mySQL to JSON);

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.

Error when trying to display my array?

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>";

Is it possible to assign sql table value to a variable in php?

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();

Getting only one row of a mysql table displayed using PHP

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

Struggling with php SELECT

I have used the below code in my website,
<?php
$con= mysqli_connect("*******","******","*****", "catalejo_articles");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result= mysqli_query($con, "SELECT * FROM baul");
while($row = mysqli_fetch_array($result))
{
echo $row['title'] . " " . $row['date'];
}
mysqli_close($con);
?>
and it is not working. What am i doing wrong??
I am new to php and mysql, any help will be appreciated.
UPDATE:
I want to thank and apologize to all of you who spent precious time trying to help me. I just solved the problem, the original code was OK. The problem was I didn't change the file extension to PHP.
Make sure display error is enabled. add this line at the top of your page and see if it display any error :
error_reporting(-1);
And try inside while loop :
var_dump($row)
Does table contains data?
Try this way,
if ($result = mysqli_query($con, "SELECT * FROM baul", MYSQLI_USE_RESULT)) {
while($row = mysqli_fetch_array($result))
{
echo $row['title'] . " " . $row['date'];
}
mysqli_free_result($result);
}
Check if u have any records in your table using mysqli_num_rows
$con= mysqli_connect("*******","******","*****", "catalejo_articles");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result= mysqli_query($con, "SELECT * FROM baul");
$count = mysqli_num_rows($result);
if($count > 0)
{
while($row = mysqli_fetch_array($result))
{
echo $row['title'] . " " . $row['date'];
}
}else{
echo "No Records found in the table";
}
mysqli_close($con);
From what I can make out of your code, since you were attempting to reference by association instead of by numerical index, you weren't seeing anything because you were missing MYSQLI_ASSOC in your while loop:
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo $row['title']." ".$row['date'];
}
Otherwise you need to know where "title" and "date" columns are located and reference them by numerical value:
while($row = mysqli_fetch_array($result)) {
echo $row[0]." ".$row[1];
}
Alternatively, if mysqli_fetch_array is not working (due to PHP version), try using:
while($row = mysqli_fetch_assoc($result)) {
echo $row['title'].' '.$row['date'];
}

Categories