PHP mysqli_fetch_assoc() Function returning NULL while the same PHP file works












0















i have spent a day now but i am not able to solve the following problem.
I have this code to access my database and i want to get some values.
The weird thing is, that the same code works for the same php file on the same server in the same directory.
I wanted to create a new php file with a different sql selection, but this one always returns null instead of a json array.



Any helpful advice would be great!
I am running a wordpress site, maybe thats an important information.



<?php
if( isset( $_POST[user_id] ) )
{
$user_id = $_POST[user_id];

$servername = "****";
$username = "****";
$password = "****";
$dbname = "****";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//echo "Connected successfully";
//echo $user_id;


$sql = "SELECT *
FROM felderkonfig
WHERE felderkonfig_user_id = '$user_id' ";

$result = mysqli_query($conn, $sql); //sql abfrage in array speichern
$myArray = array();
$myArray =mysqli_fetch_assoc($result); //umwandeln in assoziatives array

echo json_encode($myArray); //echo als json array


$conn->close();
exit();
}else{
echo "Fehler bei if isset";
}
?>









share|improve this question

























  • Just for starters, $user_id should not be enclosed in single apostrophes. It wil make it literal used that way in a string.

    – Gavin Simpson
    Nov 24 '18 at 9:22











  • Your query is prone to SQL injections. You should learn about prepared statements. Now for the error you're getting, it either means your query has an error (but you should see a PHP warning, due to $result being false), or it doesn't return any row.

    – Jeto
    Nov 24 '18 at 9:22













  • suggest you to var_dump you query and then execute that in your database invironment and check if you have result or not.

    – FatemehNB
    Nov 24 '18 at 9:27











  • Hi guys. Thanks a lot for the quick replies. I have already tried different queries. Also directly in my database and they work fine. As i mentioned above, exactly the same code & query works fine and that confuses me a lot.

    – Michael H.
    Nov 24 '18 at 9:40











  • do you get Fehler bei if isset message too or not?

    – FatemehNB
    Nov 24 '18 at 9:42
















0















i have spent a day now but i am not able to solve the following problem.
I have this code to access my database and i want to get some values.
The weird thing is, that the same code works for the same php file on the same server in the same directory.
I wanted to create a new php file with a different sql selection, but this one always returns null instead of a json array.



Any helpful advice would be great!
I am running a wordpress site, maybe thats an important information.



<?php
if( isset( $_POST[user_id] ) )
{
$user_id = $_POST[user_id];

$servername = "****";
$username = "****";
$password = "****";
$dbname = "****";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//echo "Connected successfully";
//echo $user_id;


$sql = "SELECT *
FROM felderkonfig
WHERE felderkonfig_user_id = '$user_id' ";

$result = mysqli_query($conn, $sql); //sql abfrage in array speichern
$myArray = array();
$myArray =mysqli_fetch_assoc($result); //umwandeln in assoziatives array

echo json_encode($myArray); //echo als json array


$conn->close();
exit();
}else{
echo "Fehler bei if isset";
}
?>









share|improve this question

























  • Just for starters, $user_id should not be enclosed in single apostrophes. It wil make it literal used that way in a string.

    – Gavin Simpson
    Nov 24 '18 at 9:22











  • Your query is prone to SQL injections. You should learn about prepared statements. Now for the error you're getting, it either means your query has an error (but you should see a PHP warning, due to $result being false), or it doesn't return any row.

    – Jeto
    Nov 24 '18 at 9:22













  • suggest you to var_dump you query and then execute that in your database invironment and check if you have result or not.

    – FatemehNB
    Nov 24 '18 at 9:27











  • Hi guys. Thanks a lot for the quick replies. I have already tried different queries. Also directly in my database and they work fine. As i mentioned above, exactly the same code & query works fine and that confuses me a lot.

    – Michael H.
    Nov 24 '18 at 9:40











  • do you get Fehler bei if isset message too or not?

    – FatemehNB
    Nov 24 '18 at 9:42














0












0








0








i have spent a day now but i am not able to solve the following problem.
I have this code to access my database and i want to get some values.
The weird thing is, that the same code works for the same php file on the same server in the same directory.
I wanted to create a new php file with a different sql selection, but this one always returns null instead of a json array.



Any helpful advice would be great!
I am running a wordpress site, maybe thats an important information.



<?php
if( isset( $_POST[user_id] ) )
{
$user_id = $_POST[user_id];

$servername = "****";
$username = "****";
$password = "****";
$dbname = "****";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//echo "Connected successfully";
//echo $user_id;


$sql = "SELECT *
FROM felderkonfig
WHERE felderkonfig_user_id = '$user_id' ";

$result = mysqli_query($conn, $sql); //sql abfrage in array speichern
$myArray = array();
$myArray =mysqli_fetch_assoc($result); //umwandeln in assoziatives array

echo json_encode($myArray); //echo als json array


$conn->close();
exit();
}else{
echo "Fehler bei if isset";
}
?>









share|improve this question
















i have spent a day now but i am not able to solve the following problem.
I have this code to access my database and i want to get some values.
The weird thing is, that the same code works for the same php file on the same server in the same directory.
I wanted to create a new php file with a different sql selection, but this one always returns null instead of a json array.



Any helpful advice would be great!
I am running a wordpress site, maybe thats an important information.



<?php
if( isset( $_POST[user_id] ) )
{
$user_id = $_POST[user_id];

$servername = "****";
$username = "****";
$password = "****";
$dbname = "****";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//echo "Connected successfully";
//echo $user_id;


$sql = "SELECT *
FROM felderkonfig
WHERE felderkonfig_user_id = '$user_id' ";

$result = mysqli_query($conn, $sql); //sql abfrage in array speichern
$myArray = array();
$myArray =mysqli_fetch_assoc($result); //umwandeln in assoziatives array

echo json_encode($myArray); //echo als json array


$conn->close();
exit();
}else{
echo "Fehler bei if isset";
}
?>






php mysql json mysqli null






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 9:55







Michael H.

















asked Nov 24 '18 at 9:19









Michael H.Michael H.

11




11













  • Just for starters, $user_id should not be enclosed in single apostrophes. It wil make it literal used that way in a string.

    – Gavin Simpson
    Nov 24 '18 at 9:22











  • Your query is prone to SQL injections. You should learn about prepared statements. Now for the error you're getting, it either means your query has an error (but you should see a PHP warning, due to $result being false), or it doesn't return any row.

    – Jeto
    Nov 24 '18 at 9:22













  • suggest you to var_dump you query and then execute that in your database invironment and check if you have result or not.

    – FatemehNB
    Nov 24 '18 at 9:27











  • Hi guys. Thanks a lot for the quick replies. I have already tried different queries. Also directly in my database and they work fine. As i mentioned above, exactly the same code & query works fine and that confuses me a lot.

    – Michael H.
    Nov 24 '18 at 9:40











  • do you get Fehler bei if isset message too or not?

    – FatemehNB
    Nov 24 '18 at 9:42



















  • Just for starters, $user_id should not be enclosed in single apostrophes. It wil make it literal used that way in a string.

    – Gavin Simpson
    Nov 24 '18 at 9:22











  • Your query is prone to SQL injections. You should learn about prepared statements. Now for the error you're getting, it either means your query has an error (but you should see a PHP warning, due to $result being false), or it doesn't return any row.

    – Jeto
    Nov 24 '18 at 9:22













  • suggest you to var_dump you query and then execute that in your database invironment and check if you have result or not.

    – FatemehNB
    Nov 24 '18 at 9:27











  • Hi guys. Thanks a lot for the quick replies. I have already tried different queries. Also directly in my database and they work fine. As i mentioned above, exactly the same code & query works fine and that confuses me a lot.

    – Michael H.
    Nov 24 '18 at 9:40











  • do you get Fehler bei if isset message too or not?

    – FatemehNB
    Nov 24 '18 at 9:42

















Just for starters, $user_id should not be enclosed in single apostrophes. It wil make it literal used that way in a string.

– Gavin Simpson
Nov 24 '18 at 9:22





Just for starters, $user_id should not be enclosed in single apostrophes. It wil make it literal used that way in a string.

– Gavin Simpson
Nov 24 '18 at 9:22













Your query is prone to SQL injections. You should learn about prepared statements. Now for the error you're getting, it either means your query has an error (but you should see a PHP warning, due to $result being false), or it doesn't return any row.

– Jeto
Nov 24 '18 at 9:22







Your query is prone to SQL injections. You should learn about prepared statements. Now for the error you're getting, it either means your query has an error (but you should see a PHP warning, due to $result being false), or it doesn't return any row.

– Jeto
Nov 24 '18 at 9:22















suggest you to var_dump you query and then execute that in your database invironment and check if you have result or not.

– FatemehNB
Nov 24 '18 at 9:27





suggest you to var_dump you query and then execute that in your database invironment and check if you have result or not.

– FatemehNB
Nov 24 '18 at 9:27













Hi guys. Thanks a lot for the quick replies. I have already tried different queries. Also directly in my database and they work fine. As i mentioned above, exactly the same code & query works fine and that confuses me a lot.

– Michael H.
Nov 24 '18 at 9:40





Hi guys. Thanks a lot for the quick replies. I have already tried different queries. Also directly in my database and they work fine. As i mentioned above, exactly the same code & query works fine and that confuses me a lot.

– Michael H.
Nov 24 '18 at 9:40













do you get Fehler bei if isset message too or not?

– FatemehNB
Nov 24 '18 at 9:42





do you get Fehler bei if isset message too or not?

– FatemehNB
Nov 24 '18 at 9:42












2 Answers
2






active

oldest

votes


















0














You should include this file to the prefered php file like where ever you want to connect the database



<?php include 'connect.php' ?>


in connect.php file should have the connection settings like what you metioned in the question exluded selct query






share|improve this answer
























  • Hi. Thanks for the advice but what is the advantage if i am going to do that?

    – Michael H.
    Nov 24 '18 at 13:45



















0














I dont' know why but the code is working know. The behaviour was quite weird, maybe there were some server-side problems, but i don't know. It would be great if someone coulde tell me how i can make the folllwing code safer. Thanks a lot.



<?php
if( isset( $_POST[virtuellesdepot_id] ) )
{
$virtuellesdepot_id = $_POST[virtuellesdepot_id];

$servername = "****";
$username = "****";
$password = "****";
$dbname = "****";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//echo "Connected successfully";

//echo $user_id;


$sql = "SELECT * FROM virtuelledepots ";

$result = mysqli_query($conn, $sql); //sql abfrage in array speichern
$myArray = array();
$myArray = mysqli_fetch_assoc($result); //umwandeln in assoziatives array


echo json_encode($myArray); //echo als json array


$conn->close();
exit();
}else{
echo "Fehler bei if isset";
}
?>





share|improve this answer
























  • Your $sql is most certainly not the same as the first one.

    – Gavin Simpson
    Nov 26 '18 at 11:28











  • i know. But the sql now is much bigger and quite different. It wasn't a problem by the sql statement.

    – Michael H.
    Nov 27 '18 at 12:02











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456791%2fphp-mysqli-fetch-assoc-function-returning-null-while-the-same-php-file-works%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You should include this file to the prefered php file like where ever you want to connect the database



<?php include 'connect.php' ?>


in connect.php file should have the connection settings like what you metioned in the question exluded selct query






share|improve this answer
























  • Hi. Thanks for the advice but what is the advantage if i am going to do that?

    – Michael H.
    Nov 24 '18 at 13:45
















0














You should include this file to the prefered php file like where ever you want to connect the database



<?php include 'connect.php' ?>


in connect.php file should have the connection settings like what you metioned in the question exluded selct query






share|improve this answer
























  • Hi. Thanks for the advice but what is the advantage if i am going to do that?

    – Michael H.
    Nov 24 '18 at 13:45














0












0








0







You should include this file to the prefered php file like where ever you want to connect the database



<?php include 'connect.php' ?>


in connect.php file should have the connection settings like what you metioned in the question exluded selct query






share|improve this answer













You should include this file to the prefered php file like where ever you want to connect the database



<?php include 'connect.php' ?>


in connect.php file should have the connection settings like what you metioned in the question exluded selct query







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 24 '18 at 9:24









Gss VenkateshGss Venkatesh

12010




12010













  • Hi. Thanks for the advice but what is the advantage if i am going to do that?

    – Michael H.
    Nov 24 '18 at 13:45



















  • Hi. Thanks for the advice but what is the advantage if i am going to do that?

    – Michael H.
    Nov 24 '18 at 13:45

















Hi. Thanks for the advice but what is the advantage if i am going to do that?

– Michael H.
Nov 24 '18 at 13:45





Hi. Thanks for the advice but what is the advantage if i am going to do that?

– Michael H.
Nov 24 '18 at 13:45













0














I dont' know why but the code is working know. The behaviour was quite weird, maybe there were some server-side problems, but i don't know. It would be great if someone coulde tell me how i can make the folllwing code safer. Thanks a lot.



<?php
if( isset( $_POST[virtuellesdepot_id] ) )
{
$virtuellesdepot_id = $_POST[virtuellesdepot_id];

$servername = "****";
$username = "****";
$password = "****";
$dbname = "****";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//echo "Connected successfully";

//echo $user_id;


$sql = "SELECT * FROM virtuelledepots ";

$result = mysqli_query($conn, $sql); //sql abfrage in array speichern
$myArray = array();
$myArray = mysqli_fetch_assoc($result); //umwandeln in assoziatives array


echo json_encode($myArray); //echo als json array


$conn->close();
exit();
}else{
echo "Fehler bei if isset";
}
?>





share|improve this answer
























  • Your $sql is most certainly not the same as the first one.

    – Gavin Simpson
    Nov 26 '18 at 11:28











  • i know. But the sql now is much bigger and quite different. It wasn't a problem by the sql statement.

    – Michael H.
    Nov 27 '18 at 12:02
















0














I dont' know why but the code is working know. The behaviour was quite weird, maybe there were some server-side problems, but i don't know. It would be great if someone coulde tell me how i can make the folllwing code safer. Thanks a lot.



<?php
if( isset( $_POST[virtuellesdepot_id] ) )
{
$virtuellesdepot_id = $_POST[virtuellesdepot_id];

$servername = "****";
$username = "****";
$password = "****";
$dbname = "****";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//echo "Connected successfully";

//echo $user_id;


$sql = "SELECT * FROM virtuelledepots ";

$result = mysqli_query($conn, $sql); //sql abfrage in array speichern
$myArray = array();
$myArray = mysqli_fetch_assoc($result); //umwandeln in assoziatives array


echo json_encode($myArray); //echo als json array


$conn->close();
exit();
}else{
echo "Fehler bei if isset";
}
?>





share|improve this answer
























  • Your $sql is most certainly not the same as the first one.

    – Gavin Simpson
    Nov 26 '18 at 11:28











  • i know. But the sql now is much bigger and quite different. It wasn't a problem by the sql statement.

    – Michael H.
    Nov 27 '18 at 12:02














0












0








0







I dont' know why but the code is working know. The behaviour was quite weird, maybe there were some server-side problems, but i don't know. It would be great if someone coulde tell me how i can make the folllwing code safer. Thanks a lot.



<?php
if( isset( $_POST[virtuellesdepot_id] ) )
{
$virtuellesdepot_id = $_POST[virtuellesdepot_id];

$servername = "****";
$username = "****";
$password = "****";
$dbname = "****";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//echo "Connected successfully";

//echo $user_id;


$sql = "SELECT * FROM virtuelledepots ";

$result = mysqli_query($conn, $sql); //sql abfrage in array speichern
$myArray = array();
$myArray = mysqli_fetch_assoc($result); //umwandeln in assoziatives array


echo json_encode($myArray); //echo als json array


$conn->close();
exit();
}else{
echo "Fehler bei if isset";
}
?>





share|improve this answer













I dont' know why but the code is working know. The behaviour was quite weird, maybe there were some server-side problems, but i don't know. It would be great if someone coulde tell me how i can make the folllwing code safer. Thanks a lot.



<?php
if( isset( $_POST[virtuellesdepot_id] ) )
{
$virtuellesdepot_id = $_POST[virtuellesdepot_id];

$servername = "****";
$username = "****";
$password = "****";
$dbname = "****";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//echo "Connected successfully";

//echo $user_id;


$sql = "SELECT * FROM virtuelledepots ";

$result = mysqli_query($conn, $sql); //sql abfrage in array speichern
$myArray = array();
$myArray = mysqli_fetch_assoc($result); //umwandeln in assoziatives array


echo json_encode($myArray); //echo als json array


$conn->close();
exit();
}else{
echo "Fehler bei if isset";
}
?>






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 24 '18 at 13:36









Michael H.Michael H.

11




11













  • Your $sql is most certainly not the same as the first one.

    – Gavin Simpson
    Nov 26 '18 at 11:28











  • i know. But the sql now is much bigger and quite different. It wasn't a problem by the sql statement.

    – Michael H.
    Nov 27 '18 at 12:02



















  • Your $sql is most certainly not the same as the first one.

    – Gavin Simpson
    Nov 26 '18 at 11:28











  • i know. But the sql now is much bigger and quite different. It wasn't a problem by the sql statement.

    – Michael H.
    Nov 27 '18 at 12:02

















Your $sql is most certainly not the same as the first one.

– Gavin Simpson
Nov 26 '18 at 11:28





Your $sql is most certainly not the same as the first one.

– Gavin Simpson
Nov 26 '18 at 11:28













i know. But the sql now is much bigger and quite different. It wasn't a problem by the sql statement.

– Michael H.
Nov 27 '18 at 12:02





i know. But the sql now is much bigger and quite different. It wasn't a problem by the sql statement.

– Michael H.
Nov 27 '18 at 12:02


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456791%2fphp-mysqli-fetch-assoc-function-returning-null-while-the-same-php-file-works%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks