PHP script returning JSON not working - php

The below returns "\nQuery was empty" as I run it simply from my server with the URL in the Browser.
This is the PHP code:
<?
$databasehost = "server";
$databasename = "xxxx";
$databaseusername ="xxxx";
$databasepassword = "xxxx";
$query = "SELECT * FROM `Tailor`LIMIT 0 , 30";
$con = mysql_connect($databasehost,$databaseusername,$databasepassword) or die(mysql_error());
mysql_select_db($databasename) or die(mysql_error());
$query = file_get_contents("php://input");
$sth = mysql_query($query);
if (mysql_errno()) {
header("HTTP/1.1 500 Internal Server Error");
echo $query.'\n';
echo mysql_error();
}
else
{
$rows = array();
while($r = mysql_fetch_assoc($sth)) {
$rows[] = $r;
}
print json_encode($rows);
}
?>

No need to use file_get_content and you have to put one white space after table name.
<?php
$databasehost = "server";
$databasename = "xxxx";
$databaseusername ="xxxx";
$databasepassword = "xxxx";
$query = "SELECT * FROM `Tailor` LIMIT 0 , 30";
$con = mysql_connect($databasehost,$databaseusername,$databasepassword) or die(mysql_error());
mysql_select_db($databasename) or die(mysql_error());
$sth = mysql_query($query);
if (mysql_errno()) {
header("HTTP/1.1 500 Internal Server Error");
echo $query.'\n';
echo mysql_error();
}
else
{
$rows = array();
while($r = mysql_fetch_assoc($sth)) {
$rows[] = $r;
}
print json_encode($rows);
}
?>

Your query has a problem you need whitespace after tablename
SELECT * FROM `Tailor` LIMIT 0 , 30

first
$query = "SELECT * FROM `Tailor`LIMIT 0 , 30";
but you overwrite it
$query = file_get_contents("php://input");
// $query like aaa=ggg&bbb=kkk
then you
$sth = mysql_query($query);
// LIKE $sth = mysql_query('aaa=bbb&ccc=lll'); //it not sql query format

Related

android - php fetch mysql data by user id

This my PHP URL for fetching the data from MySQL. I need to make mysqli_fetch_array code saying if filled uid in the app-data table is the same with uid in users table fetch the data from all row in app-data to the uid like every user show his items from app-data table.
$host = "";
$user = "";
$pwd = "";
$db = "";
$con = mysqli_connect($host, $user, $pwd, $db);
if(mysqli_connect_errno($con)) {
die("Failed to connect to MySQL: " . mysqli_connect_error());
}
$sql = "SELECT * FROM app_data ORDER By id";
$result = mysqli_query($con, $sql);
$rows = array();
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$rows[] = $row;
}
mysqli_close($con);
echo json_encode($rows);
I think this is the correct answer:
<?php
$host = "";
$user = "";
$pwd = "";
$db = "";
$con = mysqli_connect($host, $user, $pwd, $db);
if(mysqli_connect_errno($con)) {
die("Failed to connect to MySQL: " . mysqli_connect_error());
}
$sql = "SELECT * FROM app_data WHERE u_id=".$_POST['posted_uid']." ORDER By id";
$result = mysqli_query($con, $sql);
$rows = array();
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$rows[] = $row;
}
mysqli_close($con);
echo json_encode($rows);
Little tip for the future:
Don't search exactly what you want, only search in parts.

nested if loop partially working

What I am trying to do is simply display the row values. Now suppose if the field 'head_office' dont have the value 'H.O' then I want to display the values of the last row. I tried but cant find any solution. Here is my code: (I have only blocked the php part)
<?php
$mysql_host = 'localhost';
$mysql_user = 'root';
$mysql_password = '123';
$mysql_database = 'sdbms';
$setup_page = './myinstitute.php';
$db = mysql_connect($mysql_host, $mysql_user, $mysql_password);
mysql_select_db($mysql_database, $db);
if(isset($_REQUEST['id'])){
$id=$_REQUEST['id'];
$sql = "SELECT * FROM institute WHERE id =$id";
$result = mysql_query($sql, $db);
$row = mysql_fetch_array($result);
}
else if(!isset($_REQUEST['id'])){
$sql = 'SELECT * FROM institute WHERE head_office ="H.O"';
$result = mysql_query($sql, $db);
$row = mysql_fetch_array($result);
}
else{
$sql="SELECT * FROM institute";
$result = mysql_query($sql, $db);
$n = mysql_num_rows($result); //counting number of rows
if($n==0){
header('Location: '.$setup_page);
}
else{
$sql = 'SELECT * FROM institute ORDER BY id DESC LIMIT 1';
$result = mysql_query($sql, $db);
$row = mysql_fetch_array($result);
}
}
?>
<?php
$mysql_host = 'localhost';
$mysql_user = 'root';
$mysql_password = '123';
$mysql_database = 'sdbms';
$setup_page = './myinstitute.php';
$db = mysql_connect($mysql_host, $mysql_user, $mysql_password);
mysql_select_db($mysql_database, $db);
$row = array();
if(isset($_REQUEST['id'])) {
$id = (int) $_REQUEST['id'];
if(!empty($id)) {
$sql = "SELECT * FROM institute WHERE id =$id";
$result = mysql_query($sql, $db);
$row = mysql_fetch_array($result);
}
} else {
$sql = 'SELECT * FROM institute WHERE head_office = "H.O"';
$result = mysql_query($sql, $db);
$row = mysql_fetch_array($result);
}
if(!isset($_REQUEST['id']) && empty($row))
$sql = "SELECT * FROM institute";
$result = mysql_query($sql, $db);
$n = mysql_num_rows($result); //counting number of rows
if($n == 0) {
header('Location: ' . $setup_page);
} else {
$sql = 'SELECT * FROM institute ORDER BY id DESC LIMIT 1';
$result = mysql_query($sql, $db);
$row = mysql_fetch_array($result);
}
}
?>
As $_REQUEST['id'] can only have 2 status, isset and !isset, the else statement will never be used.
I don't understand very well how do you want to do, but it's illogic: the three step don't execute ever. Try it:
if(isset($_REQUEST['id'])){
$id=$_REQUEST['id'];
$sql = "SELECT * FROM institute WHERE id =$id";
$result = mysql_query($sql, $db);
$row = mysql_fetch_array($result);
}
else if(!isset($_REQUEST['id'])){
$sql = 'SELECT * FROM institute WHERE head_office ="H.O"';
$result = mysql_query($sql, $db);
$row = mysql_fetch_array($result);
}
if(count($row)<=0) {
$sql="SELECT * FROM institute";
$result = mysql_query($sql, $db);
$n = mysql_num_rows($result); //counting number of rows
if($n==0){
header('Location: '.$setup_page);
}
else{
$sql = 'SELECT * FROM institute ORDER BY id DESC LIMIT 1';
$result = mysql_query($sql, $db);
$row = mysql_fetch_array($result);
}
}
Enjoy your code.

to retrieve a mysql data in php and echo the retrieved data

<?php
$username = "root";
$password = "password";
$database = "xxxxxx";
$link = mysql_connect("localhost", $username, $password);
$query = "SELECT rollno FROM users where username = '".$_SESSION['MM_Username']."'";
mysql_select_db('xxxxxx', $link);
$result = mysql_query($query) or die(mysql_error($link));
$num = mysql_num_rows($result);
mysql_close();
$rows = array();
$result = mysql_query($query) or die(mysql_error());
$rows = array();
while($r = mysql_fetch_row($result))
{
$rows[] = $r[0];
}
print_r($rows);
?>
This is my code i want to display the roll number of the currently logged in user and
when i run this code i get no database selected.
First of all, you should code properly, means database connection and database selection should be on top:
<?php
$username = "root";
$password = "password";
$database = "xxxx";
$link = mysql_connect("localhost", $username, $password);
mysql_select_db('xxxx', $link);
$query = "SELECT rollno FROM users where username = '".$_SESSION['MM_Username']."'";
$result = mysql_query($query) or die(mysql_error($link));
$num = mysql_num_rows($result);
$rows = array();
$result = mysql_query($query) or die(mysql_error());
$rows = array();
while($r = mysql_fetch_row($result))
{
$rows[] = $r[0];
}
print_r($rows);
mysql_close();
?>
Also moved mysql_close(); on last
One other main point was, now mysql_ is deprecated, please use mysqli_
Remove this statement from line number 10
mysql_close();
just remove these two lines before while that will solve ur problem
$result = mysql_query($query) or die(mysql_error());
$rows = array();
Use this code and use mysql_close function in the last.
<?php
$username = "root";
$password = "password";
$database = "xxxxxx";
$link = mysql_connect("localhost", $username, $password);
$query = "SELECT rollno FROM users where username = '".$_SESSION['MM_Username']."'";
mysql_select_db($database, $link);
$result = mysql_query($query) or die(mysql_error($link));
$num = mysql_num_rows($result);
$rows = array();
$result = mysql_query($query) or die(mysql_error());
$rows = array();
while($r = mysql_fetch_row($result))
{
$rows[] = $r[0];
}
print_r($rows);
mysql_close();
?>
You have closed the connection from MySQL before the mysql_query mysql_close();
try this
<?php
$username = "root";
$password = "password";
$database = "dfsdftwsdgdfgdfsgsdf";
$link = mysql_connect("localhost", $username, $password);
$query = "SELECT rollno FROM users where username = '".$_SESSION['MM_Username']."'";
mysql_select_db('xxxxxxx', $link);
$result = mysql_query($query) or die(mysql_error($link));
$num = mysql_num_rows($result);
$rows = array();
$result = mysql_query($query) or die(mysql_error());
$rows = array();
while($r = mysql_fetch_row($result))
{
$rows[] = $r[0];
}
mysql_close();
print_r($rows);
?>
Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
The order should be like this
mysql_select_db('meipolytechnic', $link);
$query = "SELECT rollno FROM users where username = '".$_SESSION['MM_Username']."'";
mysql_select_db('meipolytechnic', $link);

Multi URL paramaters with PHP and SQL

<?php
$username = "username";
$password = "password";
$hostname = "localhost";
$database = "database";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
$selected = mysql_select_db($database,$dbhandle)
or die("Could not select database");
$id = 0;
if(isset($_GET['Day'])){ $id = (int)$_GET['Day']; }
if(!$id){
$query = "SELECT * FROM `TimeTable`";
} else {
$query = "SELECT * FROM `TimeTable` WHERE `Day`='".$id."'";
}
$result = mysql_query($query);
$rows = array();
while($r = mysql_fetch_assoc($result)) {
$rows[] = $r;
}
or die(mysql_error());
print json_encode($rows);
?>
This code worked previously, but has now stopped, and is producing Parse error: syntax error, unexpected T_LOGICAL_OR in /Directory/TimeTable.php on line 27
I am also looking to add more parameters, (eg: Where Day = $id and Year = $Year )
while($r = mysql_fetch_assoc($result)) {
$rows[] = $r;
}
or die(mysql_error());
This is a syntax error, doesn't know what to do with that or die() statement. Change to this:
$result = mysql_query($query);
if (!$result) {
die('Error');
}
while(...) {
}
I have looked up the new mysql functions an have changed to mysqli.
<?php
$username = "username";
$password = "password";
$hostname = "localhost";
$database = "database";
$link = mysqli_connect($hostname, $username, $password, $database);
if(mysqli_connect_errno()){
echo mysqli_connect_error();
}
$id= 0;
if(isset($_GET['Day'])){ $id=(int)$_GET['Day']; }
$year = 0;
if(isset($_GET['Year'])){ $year=(int)$_GET['Year'];}
if(!$id){
$query = "SELECT * FROM TimeTable";
} else {
if (!year) {
$query = "Select * FROM TimeTable";
} else {
$query = "SELECT * FROM TimeTable WHERE Day=$id AND Year=$year";
}
}
$rows = array();
//Perform JSON encode
if($result = mysqli_query($link, $query)){
while($r = mysqli_fetch_assoc($result)){
$rows[] = $r;
}
}
print json_encode($rows);
?>

Handling error report

I am new in PHP so I have to apologize for such a dumb question, but I am not sure how to find the right answer. I should check if my final result is empty (if $sql found anything). If it didnt find I would like to get some notification example "The list is empty". That message will also be visible from Android app when I call the url?
<?php
$host = "";
$user = "";
$pwd = "";
$db = "";
$con = mysqli_connect($host, $user, $pwd, $db);
if(mysqli_connect_errno($con)) {
die("Failed to connect to MySQL: " . mysqli_connect_error());
}
// query the application data
$sql = "SELECT * FROM lista WHERE Grad='".$_GET['grad']."' AND Predmet='".$_GET['predmet']."'";
$result = mysqli_query($con, $sql);
$rows = array();
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$rows[] = $row;
}
mysqli_close($con);
echo json_encode($rows);
If mysqli_num_rows returns 0, you have no records.
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) == 0) {
$rows = "no rows found";
} else {
$rows = array();
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$rows[] = $row;
}
}
mysqli_close($con);
echo json_encode($rows);

Categories