Fetching row from database using ajax and php

user8790423

I have been trying to echo users profile photo using ajax and php but i get nothing but a blank page. Below is my ajax and php code which I have written myself.

Ajax

$('#profile_photo').html('<img src="loading.gif" alt="loading..." width="20px" height="20px" >');

$(document).ready(function() {
    $.ajax( {    
        type: "GET",
        url: "profile_photo.php",             
        dataType: "html",   
        success: function(d) {                    
             $("#profile_photo").html(d); 
        }
    });
});

PHP (profile_photo.php)

<?php  
    // connect to db
    include 'db.php';

    // start session
    session_start();

    // user id 
    $id = $_SESSION['id'];
    $user = $_SESSION['name'];

    // variables

    // profile photo
    $profile_photo_query = mysqli_query($db_var, "SELECT * FROM users_profile_photo WHERE id = '$id'"); 
    while ($row = mysqli_fetch_array($profile_photo_query, MYSQLI_ASSOC)) {
        $set_profile_photo = $row["image"];
    }

        // for profile photo (encode with base64)
        if ($set_profile_photo == null) {
            echo "<img src='data:image/png;base64,".base64_encode(file_get_contents("profile_photo/default.png"))."' alt='".$user."' title='".$user."' onContextMenu='return false;'";
        } else {
            echo "<img src='data:image/png;base64,".base64_encode(file_get_contents("profile_photo/$set_profile_photo"))."' alt='".$user."' title='".$user."' onContextMenu='return false;'";
        }
?>

Html

<div class="prev">
   <span id="profile_photo"></span>
</div>

Css (prev)

.prev {
    width: 120px;
    height: 120px;
    border: 5px solid #fff;
    border-radius: 50%;
    -webkit-border-radius: 50%;
    -moz-border-radius: 50%;
    overflow: hidden;
    margin-left: 10px;
    background-color: white;
    position: absolute;
    margin-top: -80px;
    z-index: 1000;
}
.prev img {
    width: 100%;
    height: 100%;
}

The ajax loads the loading image (.gif) successfully but shows a blank page when loading image is done loading. I will like to know why the profile photo is not been shown because my php code has no errors.

Magnus Andersson

Its not the Ajax that loads the .gif. Its done before with this code:

$('#profile_photo').html('<img src="loading.gif" alt="loading..." width="20px" height="20px" >');

First of all, you are missing an ">" at he end of your code here:

echo "<img src='data:image/png;base64,".base64_encode(file_get_contents("profile_photo/default.png"))."' alt='".$user."' title='".$user."' onContextMenu='return false;'";

Check if anything is set to the img src attribute..

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Actions links not working while fetching data from database using ajax in php

From Dev

Fetching one single row from database using Perl

From Dev

Fetching one single row from database using Perl

From Dev

Fetching data from MySQL database using PHP, Displaying it in a form for editing

From Dev

Fetching data from Database and display it in Form using php

From Dev

Fetching images from mysql database using php and displaying in gridview in android

From Dev

Using PHP, Ajax and JavaScript to put data from every row in my database into a separate div

From Dev

Using PHP, Ajax and JavaScript to put data from every row in my database into a separate div

From Dev

PHP Script fetching only first row of address field from the MySQL database

From Dev

PHP Fetching Data From Specific MySQL Row

From Dev

Fetching data from mysql database with php

From Dev

Getting undefined in innerHTML when fetching and displaying data from database using AJAX and JSON

From Dev

Row not being deleted in database when using AJAX and PHP

From Dev

Fetching a blob from database as a byte[] using myBatis

From Dev

PHP Ajax return more than one row from database

From Dev

html Table taking extra blank values while fetching the row from mysql using php

From Dev

Deleting database row using Ajax

From Dev

Using php Convert timestamp to Unix timestamp while fetching from Oracle database

From Dev

Can not avoid duplicate data while fetching from database using PHP and MySQL

From Dev

fetching rows from ajax and append in existing last row

From Dev

fetching rows from ajax and append in existing last row

From Dev

Fetching limited data from sql using PHP

From Dev

fetching data from phpmyadmin using php

From Dev

fetching database results in PHP

From Dev

PDO fetching the next row of database row by row

From Dev

How to extract desired row from Mysql database using PHP script

From Dev

Select row from database using PHP with Android interface

From Dev

How to extract desired row from Mysql database using PHP script

From Dev

Using TOP, Limit while fetching data using ID from database

Related Related

  1. 1

    Actions links not working while fetching data from database using ajax in php

  2. 2

    Fetching one single row from database using Perl

  3. 3

    Fetching one single row from database using Perl

  4. 4

    Fetching data from MySQL database using PHP, Displaying it in a form for editing

  5. 5

    Fetching data from Database and display it in Form using php

  6. 6

    Fetching images from mysql database using php and displaying in gridview in android

  7. 7

    Using PHP, Ajax and JavaScript to put data from every row in my database into a separate div

  8. 8

    Using PHP, Ajax and JavaScript to put data from every row in my database into a separate div

  9. 9

    PHP Script fetching only first row of address field from the MySQL database

  10. 10

    PHP Fetching Data From Specific MySQL Row

  11. 11

    Fetching data from mysql database with php

  12. 12

    Getting undefined in innerHTML when fetching and displaying data from database using AJAX and JSON

  13. 13

    Row not being deleted in database when using AJAX and PHP

  14. 14

    Fetching a blob from database as a byte[] using myBatis

  15. 15

    PHP Ajax return more than one row from database

  16. 16

    html Table taking extra blank values while fetching the row from mysql using php

  17. 17

    Deleting database row using Ajax

  18. 18

    Using php Convert timestamp to Unix timestamp while fetching from Oracle database

  19. 19

    Can not avoid duplicate data while fetching from database using PHP and MySQL

  20. 20

    fetching rows from ajax and append in existing last row

  21. 21

    fetching rows from ajax and append in existing last row

  22. 22

    Fetching limited data from sql using PHP

  23. 23

    fetching data from phpmyadmin using php

  24. 24

    fetching database results in PHP

  25. 25

    PDO fetching the next row of database row by row

  26. 26

    How to extract desired row from Mysql database using PHP script

  27. 27

    Select row from database using PHP with Android interface

  28. 28

    How to extract desired row from Mysql database using PHP script

  29. 29

    Using TOP, Limit while fetching data using ID from database

HotTag

Archive