need to echo one row in json encode - php

I have this php file and I want to echo one array that ticketnumber is located in it with json encode, in meaning I want to print only one array when isset POST the ticketnumber for this array, but I tried and every time I get error how to slove this problem in addition I used the loop (while) so is it correct for one array?
<?php
define('HOST', 'localhost');
define('USER', 'root');
define('PASS','');
define('DB', 'ala');
$con = mysqli_connect(HOST,USER,PASS,DB) or die ('unable to connect');
if ($_SERVER ['REQUEST_METHOD']=='POST') {
$ticketnumber = $_POST['ticketnumber'];
$sql = " SELECT * FROM contact WHERE ticketnumber = '$ticketnumber' ";
$res = mysqli_query($con, $sql);
$result = array();
while($get = mysqli_fetch_array($res))
{
array_push($result,array('ticketnumber' =>$get[0], 'subject' =>$get[1],'response' =>$get[2]));
}
if(isset($get)){
echo json_encode(array("responseticket"=>$result));
} else {
echo " error";
}
}
?>

You don't define variable $get .
I thinks there should be..
if(!empty($result)){
echo json_encode(array("responseticket"=>$result));
} else {
echo " error";
}

Try like this..
if(!empty($get)){
echo json_encode(array("responseticket"=>$result));
} else {
echo " error";
}

Related

Value of output "any" when there are no rows in the SQL table

<?php
$successreturn[]=array(
"id"=>"any",
"firstname"=>"any",
"lastname"=>"any",
"dateofbirth"=>"any",
"city"=>"any",
"gender"=>"any"
);
header("Access-Control-Allow-Origin: *");
$servername="localhost";
$username="root";
$password="sandeepchetikam";
$dbase="mydb";
$conn=mysqli_connect($servername,$username,$password,$dbase);
if (!$conn) {
echo "Connection Problem".mysqli_connect_error($conn);
}
$sql= "SELECT * FROM Employees";
$result = mysqli_query($conn,$sql);
$count = mysqli_num_rows($result);
$value=0;
if(!$result){
echo "Connection Failed " .mysqli_connect_error($result);
}
while($row = mysqli_fetch_assoc($result)){
$successreturn[$value]['id'] =$row['id'];
$successreturn[$value]['firstname'] =$row['firstname'];
$successreturn[$value]['lastname'] =$row['lastname'];
$successreturn[$value]['dateofbirth'] =$row['dateofbirth'];
$successreturn[$value]['city'] =$row['city'];
$successreturn[$value]['gender'] =$row['gender'];
$value++;
};
echo json_encode($successreturn);
?>
output :
[{"id":"any","firstname":"any","lastname":"any","dateofbirth":"any","city":"any","gender":"any"}]
I am trying to return a JSON value into my angular service. But when there are no more rows in the table. Its returning the Colomn value as "any".
Why is it like that ?? How do i return a empty row with no value?
You need to control the program flow a little differently
<?php
header("Access-Control-Allow-Origin: *");
$servername="localhost";
$username="root";
$password="sandeepchetikam";
$dbase="mydb";
$conn=mysqli_connect($servername,$username,$password,$dbase);
if (!$conn) {
echo "Connection Problem".mysqli_connect_error($conn);
}
// first only select what you want to use from the row
$sql= "SELECT `id`,`firstname`,`lastname`,
`dateofbirth`,`city`,`gender`
FROM Employees";
$result = mysqli_query($conn,$sql);
if(!$result){
// you only use `mysqli_connect_error` to get connection errors
// use mysqli_error($result) for normal query errors
echo "Query failed " . mysqli_error($result);
echo json_encode(array('error'=>'Query failed'));
exit;
}
if ( mysqli_num_rows($result) > 0 ) {
while($row = mysqli_fetch_assoc($result)){
// now as you only have what you want in your assoc array
$rows[] = $row;
}
echo json_encode($rows);
} else {
// no data returned from query
// return something so the calling code knows what to do
echo json_encode(array('error'=>'No data in table'));
}
?>
When no row selected then the while loop is not executting and the initial $successreturn array is return with value 'any' to return no value if no row selected then
Just change this
$successreturn[]=array(
"id"=>"any",
"firstname"=>"any",
"lastname"=>"any",
"dateofbirth"=>"any",
"city"=>"any",
"gender"=>"any");
To
$successreturn[]=array(
"id"=>"",
"firstname"=>"",
"lastname"=>"",
"dateofbirth"=>"",
"city"=>"",
"gender"=>"");
Edit:-
$successreturn=[];
And check the value is empty or not in view and then display the data if it have otherwise don't display it.
I thing it will help you.

Mysql check for value in mysql database fails?

I have a very simple use case where I am checking if a certain value is present in the table and it always seems to fail.This is my php code.
<?php
include "config.php";
$con = mysqli_connect($server_name,$mysql_user,$mysql_pass,$dbname);
if(!$con)
{
echo "Connection Error".mysqli_connect_error();
}
else{
//echo "";
}
$device_id = $_POST["device_id"];
$check = "SELECT magazine_id FROM registered_buyers WHERE device_id = $device_id";
$rs = mysqli_query($con,$check);
if(mysqli_num_rows($con,$rs) == 0)
{
$jsonarray = $_POST["jsonarray"];
echo "This will be inserted".$jsonarray;
}else
{
echo "User already registered";
}
?>
Can anyone please point out my mistake.Any help or suggestion is welcome.Thank you.
You can try to follow this code.
<?php
include "config.php";
$con = mysqli_connect($server_name,$mysql_user,$mysql_pass,$dbname);
if(!$con){
echo "Connection Error".mysqli_connect_error();
}
$device_id = $_POST["device_id"];
$check = "SELECT magazine_id FROM registered_buyers WHERE device_id = ".$device_id;
$rs = mysqli_query($con, $check);
if(mysqli_num_rows($rs) == 0){
$jsonarray = $_POST["jsonarray"];
echo "This will be inserted".$jsonarray;
}else{
echo "User already registered";
}
?>
since i dont have enough rep to add a comment, i will consider the device_id is string, if so try something like this:
"SELECT magazine_id FROM registered_buyers WHERE device_id = '$device_id'";
add '

Not able to insert the data from url to database in php

final.php
Here I am trying to get the data from the url using GET method and trying to insert into the database. I was able to insert the data for first few rows after that the data is not inserted. Can anyone help me regarding this?
when I try to run the url: www.myii.com/app/final.php?name=123&glucose=3232...
the data is not inserting.
<?php
include("query_connect.php");
$name = $_GET['name'];
$glucose = $_GET['glucose'];
$temp = $_GET['temp'];
$battery = $_GET['battery'];
$tgs_a = $_GET['tgs_a'];
$tgs_g = $_GET['tgs_g'];
$heartrate = $_GET['heartrate'];
$spo2 = $_GET['spo2'];
$rr = $_GET['rr'];
$hb = $_GET['hb'];
$ina22 = $_GET['ina22'];
$accucheck = $_GET['accucheck'];
$isactive = $_GET['isactive'];
$address = $_GET['address'];
$deviceno = $_GET['deviceno'];
$sql_insert = "insert into query (name,glucose,temp,battery,tgs_a,tgs_g,heartrate,spo2,rr,hb,ina22,accucheck,isactive,address,deviceno) values ('$name','$glucose','$temp',$battery','$tgs_a','$tgs_g','$heartrate','$spo2','$rr','$hb','$ina22','$accucheck','$isactive','$address','$deviceno')";
mysqli_query($sql_insert);
if($sql_insert)
{
echo "Saving succeed";
//echo $date_time;
}
else{
echo "Error occured";
}
?>
Query_connect.php
This is my database config php file.
<?php
$user = "m33root";
$password = "me3i434";
$host = "localhost";
$connection = mysqli_connect($host,$user,$password);
$select = mysqli_select_db('miiyy',$connection);
if($connection)
{
echo "connection succesfull<br>";
}
else {
echo "Error";
}
?>
Make sure that all columns can contain NULL so that not filled fields will stay NULL instead of throwing an error.
Try below to see mysql error:
mysql_query($sql_insert);
echo mysql_error()
Try these
In SQL
Change the table name of query to some other name. Because "query" is reserved in SQL
In code
if (!mysqli_query($con,$sql_insert ))
{
echo("Error description: " . mysqli_error($con));
}
else
{
echo "Success";
}
Use mysqli_error() function

Fields only echoed when value

To avoid open rows on the screen, how can I reach that the 5 vip fields, only are echoed when there is a value in it?
Thanks, Benny
$db = new mysqli('host', 'user', 'pass', 'dbase');
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "
SELECT
user.FName,
user.LName,
user.HerbalifeID,
user.UplineS,
registratie.PartnerFName as pfn,
registratie.PartnerLName as pln,
registratie.NaamVIP1 as vip1,
registratie.NaamVIP2 as vip2,
rgistratie.NaamVIP3 as vip3,
registratie.NaamVIP4 as vip4,
registratie.NaamVIP5 as vip5
FROM
registratie
INNER JOIN user
ON registratie.userID = user.UserID
AND registratie.eventID =127;
";
$result = $db->query($query) or die($db->error.__LINE__);
if ($result->num_rows) {
while ($row = $result->fetch_object()) {
echo "{$row->FName} {$row->LName} {$row->HerbalifeID} {$row->pfn} {$row->pln}<br>
{$row->vip1}{$row->vip2}{$row->vip3}{$row->vip4}{$row->vip5})<br><br>"; //only
}
} else {
echo 'No Results';
}
if(!empty({$row->vip1}) and !empty({$row->vip2}) and !empty({$row->vip3}) and !empty({$row->vip4}) and !empty({$row->vip5}) ){
echo "{$row->FName} {$row->LName} {$row->HerbalifeID} {$row->pfn} {$row->pln}<br>{$row->vip1}{$row->vip2}{$row->vip3}{$row->vip4}{$row->vip5})<br><br>";
}else{
echo "{$row->FName} {$row->LName} {$row->HerbalifeID} {$row->pfn} {$row->pln})<br><br>";
}
I don't know if I get it.
If you want to print only when all vips are filled, use the following code:
//...
$range = range(1,5);
while ($row = $result->fetch_object()) {
//This will be printed in every iteration
echo "{$row->FName} {$row->LName} {$row->HerbalifeID} {$row->pfn} {$row->pln}";
//if one of the vips has no value, go to the next $row
foreach($range as $r){
$vipColumn = "vip$r";
if(empty($row->$vipColumn)){
continue 2; //breaks both 'foreach' and 'while' loops
}
}
//This only echo when all vips has values
echo "<br>{$row->vip1}{$row->vip2}{$row->vip3}{$row->vip4}{$row->vip5})<br><br>";
}
//...

Connect to mysql db but no data from table is echo to screen

The script below connects to the db (I get the connected successfully echo) but none of the data from the query is shown onscreen.
I assume the data must be somewhere as I do not get the error message.
Question: Where is the error in the script?
<?php
//connectdb();
$con = mysqli_connect("localhost","UN","PW");
if ( $con == "" ) { echo " DB Connection error...\r\n"; exit(); }
echo 'Connected successfully';
$result = mysqli_query($con, "SELECT graduation_year FROM wp_gfsept2013");
while($row = mysql_fetch_array($result))
if ($result === "") {echo "An error occurred.";}
{
echo $row['graduation_year'];
echo "<br>";
}
?>
Appreciate any help that can be sent my way, I'm a real newbie at this stuff.
Roger
Try adding an opening brace after while($row = mysql_fetch_array($result)) and a closing brace before the end of the script.
Is this not a syntax issue?? Why is there an IF clause after a WHILE clause but before the opening bracket for the WHILE loop block?
Additionally, you are trying to use mysql_fetch_array() instead of mysqli_fetch_array().
<?php
//connectdb();
$con = mysqli_connect("localhost","UN","PW");
if ( $con == "" ) { echo " DB Connection error...\r\n"; exit(); }
echo 'Connected successfully';
$result = mysqli_query($con, "SELECT graduation_year FROM wp_gfsept2013");
if ($result !== FALSE && mysqli_num_rows($result) > 0) { // Proper way to test for results
while($row = mysqli_fetch_assoc($result))
{
echo $row['graduation_year'];
echo "<br/>";
}
}
else {
die("Query Returned 0 rows...");
}
?>
Documentation: mysqli_result::$num_rows

Categories