Unable to retrieve data from database on mysqli_qurey

I am able to connect and store data to my database but whenever i try to retrieve the saved data in db using php mysqli_query request in form of json it dose not show any data or result.

Hi, Please include your code or the URL so we can do some checking.

@Khushpal said:
I am able to connect and store data to my database but whenever i try to retrieve the saved data in db using php mysqli_query request in form of json it dose not show any data or result.

There are many, many different reasons why that could happen, and it’s impossible to say what happens if you don’t share your code or an URL to the page.

Is there anything else you can see or have tested? Does the connection return any error message (and mysqli_query is returning false)? Does the exact query you send with PHP return the desired results when running it manually through phpMyAdmin?

appmakey.epizy.com/datainserting/php/retriveUrldata.php
Above is an url to retrive my json data from my website. I am using your free hosting. This code works perfect on wamp server local host.

Code as follows:-

`<?php

// $option = $_GET[‘option’];

/* Attempt MySQL server connection. Assuming you are running MySQL

server with default setting (user ‘root’ with no password) */

$link = mysqli_connect(“sql211.epizy.com”, “epiz_ID”, “Password”, “db_name”);

// Check connection

if($link === false){

die("ERROR: Could not connect. " . mysqli_connect_error());

}

// Attempt select query execution

$sql = “SELECT * FROM videos”;

if($result = mysqli_query($link, $sql)){

if(mysqli_num_rows($result) > 0){

    $json_array = array();
    while($row = mysqli_fetch_assoc($result)){
        $json_array[] = $row;        	
    }

    $output = json_encode(array('allVideos' => $json_array));

    header('Content-type: application/json');
    echo $output;
    // Free result set
    mysqli_free_result($result);

} else{

    echo "No records matching your query were found.";

}

} else{

echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);

}

?>`

@admin please help soon!!

`<?php

// $option = $_GET[‘option’];

/* Attempt MySQL server connection. Assuming you are running MySQL

server with default setting (user ‘root’ with no password) */

$link = mysqli_connect(“sql211.epizy.com”, “epiz_ID”, “Password”, “db_name”);

// Check connection

if($link === false){

die("ERROR: Could not connect. " . mysqli_connect_error());

}

// Attempt select query execution

$sql = “SELECT * FROM videos”;

if($result = mysqli_query($link, $sql)){

if(mysqli_num_rows($result) > 0){

    $json_array = array();
    while($row = mysqli_fetch_assoc($result)){
        $json_array[] = $row;        	
    }

    $output = json_encode(array('allVideos' => $json_array));

    header('Content-type: application/json');
    echo $output;
    // Free result set
    mysqli_free_result($result);

} else{

    echo "No records matching your query were found.";

}

} else{

echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);

}
?>`

Please anyone can help!!

I’m no fan of procedural PHP, but I’ll try to suggest some things:

  1. Have you try to print_r($json_array) without encoding them to see if there’s any value in it? Ref: PHP: array_push - Manual

  2. CMIIW. But I’d prefer to close connection at the very end of code rather than free-ing the result set.

@Khushpal said:
Please anyone can help!!
Spamming theese won’t get your problem solved quickly!.

I’ve checked your code and it seems to be working.

Some checks for you :

  • Is the database empty?
  • Is the database table name correct?
  • Is the content inside the database are empty/null?

I’ve tested your code and also modify it to my own prefrences (<?php$link = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME); - Pastebin.com) because you don’t have to check
if ($result = mysqli_query($link, $sql)) {} anymore if you’ve tested it.

I just checked your script and it looks like the database results are returned properly, but the script breaks at the JSON encoding. This is the error being raised:

Malformed UTF-8 characters, possibly incorrectly encoded

You can check if json_encode() returns false and then use json_last_error_msg() to figure out why it went wrong.

And as @Joo_Nath said, you can see the database returned it’s results by var_dump-ing or print_r-ing the results before being encoded. That’s usually the best way to debug issues like this: add print statements to the code to figure out the intermediary values so you can figure out where it goes wrong.

Thanks alot @Joo_Nath
I have tested that it gives the data proper when I use print_r but when i encode it with json enode it give no output.

@admin as u said its the json_encode error is correct its all because of utf8 encoding error, but i have a question i am using MyISAM as storage engine it converts to Swedish collation even though i select utf8_general_ci. Please help for the same and Thank You for the help.

@PlanetGamingGG Thanks for your help too whatever you did was perfect and useful but i have figured out the issue yesterday, but it will be grateful if u help me in encoding the $row or $json_array to utf8 in php. And I don’t had intend of spamming.

@Khushpal try theese :

@PlanetGamingGG Thanks alot for the solution u have provided,
@Admin now another issue has came I am not able to connect to database from cpanel its shows > The page isn’t redirecting properly.
Please help!

@Khushpal said:
@PlanetGamingGG Thanks alot for the solution u have provided,
@Admin now another issue has came I am not able to connect to database from cpanel its shows > The page isn’t redirecting properly.
Please help!

Logout and Login again.

@Khushpal said:
Thanks alot @Joo_Nath
I have tested that it gives the data proper when I use print_r but when i encode it with json enode it give no output.

@admin as u said its the json_encode error is correct its all because of utf8 encoding error, but i have a question i am using MyISAM as storage engine it converts to Swedish collation even though i select utf8_general_ci. Please help for the same and Thank You for the help.

@PlanetGamingGG Thanks for your help too whatever you did was perfect and useful but i have figured out the issue yesterday, but it will be grateful if u help me in encoding the $row or $json_array to utf8 in php. And I don’t had intend of spamming.

Did you create the tables through phpMyAdmin? phpMyAdmin defaults to a weird collation and character encoding, where in most cases utf8 or utf8mb4 is the collation you want.

Maybe you can fix the encoding and collation through phpMyAdmin.

@Admin @PlanetGamingGG and @Joo_Nath Thanks alot guys u helped me alot your help and solution helped me in resolving all my issue related to epizy. Its was really pleasant talking to you and sorry for the late reply. And one more thing u have added me to your future premium client as i am thinking of buying your Premium Plan of yearly once my project gets approved. But, I need a promise i need better communication method than this forum site, Please!. Looking ahead to be your permanent client.

@Khushpal said:
@Admin @PlanetGamingGG and @Joo_Nath Thanks alot guys u helped me alot your help and solution helped me in resolving all my issue related to epizy. Its was really pleasant talking to you and sorry for the late reply. And one more thing u have added me to your future premium client as i am thinking of buying your Premium Plan of yearly once my project gets approved. But, I need a promise i need better communication method than this forum site, Please!. Looking ahead to be your permanent client.

Well, InfinityFree, like the name suggests, only provides free hosting. We don’t operate the premium hosting ourselves. iFastNet provides the premium hosting and they also have a support system for their paying customers.