From below PHP web services code I am able to get the link from first to last which is saved on server database.....
Now I want to get the saved link from last to first.....
<?php
$con=mysqli_connect("XX.XX.XX.X:XXXX","root","root","db_");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM images");
$images = array();
if(mysqli_num_rows($result)) {
while($post = mysqli_fetch_assoc($result)) {
$images[] = $post;
}
}
header('Content-type: application/json');
echo json_encode(array('images'=>$images));
mysqli_close($con);
?>
The output ob above code is:
{"images":[{"image":"http://demo.in/vk/vehicles/v_1.jpg"},{"image":"http://demo.in/vk/vehicles/v_2.jpg"},{"image":"http://demo.in/vk/vehicles/v_3.jpg"},{"image":"http://demo.in/vk/vehicles/v_4.jpg"}]}
but I want It to be come like this:
{"images":[{"image":"http://demo.in/vk/vehicles/v_4.jpg"},{"image":"http://demo.in/vk/vehicles/v_3.jpg"},{"image":"http://demo.in/vk/vehicles/v_2.jpg"},{"image":"http://demo.in/vk/vehicles/v_1.jpg"}]}
Change your query to this
$result = mysqli_query($con,"SELECT * FROM images ORDER BY DESC");
Related
I am creating a result page using php to pull data from database.
I have been able to connect the database to my webspace and displayed the data.
Now , I would like to have the page not show all the data at once. I want to add an input field example "Origen= Los Angeles" "Destination = London" and then show the results based on that criteria.
This is my first time doing something like this so if this sounds like I should know this I'm sorry. I hope I can get some help.
<?php
// include connection settings
require_once "connect.php";
// display a list of flights
if(!empty($_GET) && !empty($_GET['name'])){
$query = "SELECT * FROM Flight_Information WHERE name LIKE ''".
$_GET['name']."".$_GET['name']."'";
} else {
$query = "SELECT ID, airlineName, departureAirport, departureDate, destinationAirport FROM Flight_Information";
} // END if
$result = $db->query($query);
if ($result) {
// ... display results in while loop
while ($row = $result->fetch_assoc()) {
echo '<li>'.$row['airlineName'].' '.$row['departureAirport'].''.$row['departureDate'].''.$row['destinationAirport'].'</li>';
} // END while
} else {
// if there was an error with your query
// this will display it
echo "SQL Error: " . $db->error;
} // END if
?>
</ul>
<?php
if (!empty($_GET) && !empty($_GET['Flight_Information_ID'])) {
$Flight_Information_ID = $_GET['Flight_Information_ID'];
$query = "SELECT * FROM Flight_Information WHERE ID =" . $Flight_Information_ID;
// perform the query
if ($result = $db->query($query)) {
// check we have a result
if ($result->num_rows > 0) {
// loop through and print out the results
while ($row = $result->fetch_assoc()) {
// output a title for each result
echo '<hr/><p>You have selected FriendShipper' .$Flight_Information_ID. ':</p> <ul>';
// loop through and output details for each item
foreach ($row as $key => $value) {
echo '<li><em>'.$key.'</em> = <strong>'.$value.'</strong></li>';
}
// output a horizontal rule under the result
echo '</ul><hr/>';
}
// num_rows = 0, no results found
} else {
echo "<p>No Flights Found " .$Flight_Information_ID. "</p>";
}
// if there was an error with your query, this will display it
} else {
echo "SQL Error: " . $db->error;
}
}
?>
<?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.
I'm trying to display data from my SQL database on a HTML page through a php query.
I've done a similar query before in the same way and that worked, however this php query doesn't display any results on my html page and I have no idea why.
<?php
$con=mysqli_connect("localhost","root","password","registration");
if (mysqli_connect_errno())
{
die("Failed to connect to MySQL: " . mysqli_connect_error());
}
$query = "SELECT * FROM goal WHERE id = $_SESSION[userid]";
$result = mysqli_query($con, $query);
if($result == false) {
die("Query failed: ".mysqli_error($con).PHP_EOL.$query);
}
while($row = mysqli_fetch_assoc($result))
{
echo "Weight Lost:"."<p>".$row['weightlost']."</p>\n";
echo "Weight Unit:"."<p>".$row['weightunit']."</p>\n";
echo "Date set for goal to be reached:"."<p>".$row['whenby']."</p>\n";
}
mysqli_close($con);
?>
It's not a duplicate question about UTF-8 Unicode.
I am new to php and I am trying to create a json response.
I added data into my database properly as follows.
Then I tried to connect to DB and i did it successfully .
but after that when I tried to create a json response by using the following code, it doesn't shows any json response .
My PHP code is :
<?php
define('HOST','localhost');
define('USER','root');
define('PASS','qwerty');
define('DB','carol');
$con = mysqli_connect(HOST,USER,PASS,DB);
if (!$con)
{
echo "Please try later..";
}
else
{
echo "DB Connected..";
}
$sql = "SELECT * from songs";
$res = mysqli_query($con,$sql);
if (!$res)
{
echo "query failed..";
}
else
{
echo "Query success..";
echo (mysqli_num_rows($res));
}
$result = array();
while($row = mysqli_fetch_array($res)){
array_push($result,
array('title'=>$row[0]),
array('url'=>$row[1]),
array('lyrics'=>$row[2])
);
}
echo json_encode(array("result"=>$result));
mysqli_close($con);
?>
I'm getting only echo of DB Connected , Query Success and 14 (no of rows)
I'm trying PHP for the first time by using some online tutuorials.
if I did any mistake in my code,please help me to find my mistake.
Thank you in advance.
After I added echo var_dump($res);
I got
I want to retrieve data from my SQL database and use it on my HTML page.
I have a php script get_score.php like this:
<?php
$con = mysqli_connect( "localhost", "xxx1", "xxx2", "xxx3");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "SELECT `name`, `score` FROM crossstreet ORDER BY `score` DESC LIMIT 5";
if (!mysqli_query($con, $sql))
{
die('Error: ' . mysqli_error($con));
}
$result = mysqli_query($con, $sql);
while ($row = mysqli_fetch_assoc($result)) {
$output = ($row["name"]);
}
mysqli_free_result($result);
echo json_encode($result);
mysqli_close($con);
?>
And then I want to retrieve the data I via JQuery. I have read about the cross-origin issue but the data I retrieve and the html/Jquery are on the same server.
$.get("get_score.php", function( data ) {
console.log(window[data]);
}, "json" );
This returns undefined. If I make it $(data) it returns an object like the one on this screenshot.
Where is my mistake; how can I just get the data from the server to use in html?
You need to make $output an array so you get all the rows.
$output = array();
while ($row = mysqli_fetch_assoc($result)) {
$output[] = $row['name'];
}
Then you should echo this:
echo json_encode($output);
And in your jQuery code, you should do console.log(data);
mysqli_free_result($result);
echo json_encode($result);
i think this is the problem.
Try to reverse the order. You are making the result free before using it.
Do like this, instead of the above .
echo json_encode($result);
mysqli_free_result($result);