Getting an associative array back from prepared statement












0














I'm trying to fetch an associative array from an SQL query using prepared statements. Nothing i'm trying works. I was able to get it work before I implemented the prepared statements. I'm reading a lot of conflicting information as well as OO and PDO information that doesn't seem to work with procedural code. I may switch over to one of those styles in the future, but I don't want to have to rewrite this whole website right now. Any help is greatly appreciated. I have an item_template.php that reads the data with rows['name'], rows['type'], etc. It was working mysqli_fetch_assoc...I just can't get fetch_assoc to work with the prepared statement.



  if ($searchname == NULL) {
echo 'You must enter something to search for!';
}else{
$sql = "SELECT * FROM itemdb WHERE name LIKE ?";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("Location: viewresults.php?error=sqlerror2");

exit();
}
else{
$searchname = "%".$searchname."%";
mysqli_stmt_bind_param($stmt,"s", $searchname);
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);

$resultcheck = mysqli_stmt_num_rows($stmt);
echo $resultcheck;
if ($resultcheck == 0) {
echo 'No results found! Try again! ' . $resultcheck;
exit();
}else{
$result = mysqli_stmt_get_result($stmt);
echo $result;

while ($row = mysqli_fetch_assoc($stmt)) {

include("item_template.php");
echo "SUCCESS";
}
}
}
}









share|improve this question






















  • What is $stmt? What parameter does mysqli_fetch_assoc expect? (Hint it is a mysqli_result). var_dump is your friend.
    – ficuscr
    Dec 28 '18 at 5:59












  • I tried using mysqli_fetch_assoc with mysqli_stmt_store_result with no luck either though. I thought the statement would have been the mysqli_result. I had no issues when I wasn't using prepared statements...Thats what's got me all out of wack. I don't know what i'm doing and I think im confused on something lol. I've been trying to figure this out forever. Most of the examples and help i'm finding are OO and PDO which have just enough differences to not be of help.
    – Bassex
    Dec 28 '18 at 6:05










  • replace while ($row = mysqli_fetch_assoc($stmt)) with WHILE( null !== ($row = mysqli_fetch_assoc($stmt)))
    – Umar Abdullah
    Dec 28 '18 at 6:13












  • Still doesn't work. That's almost the same as what I have just in a different format?
    – Bassex
    Dec 28 '18 at 6:19






  • 1




    Your trying to fetch the data from the statement and not the result - mysqli_fetch_assoc($stmt) should be mysqli_fetch_assoc($result)
    – Nigel Ren
    Dec 28 '18 at 7:14
















0














I'm trying to fetch an associative array from an SQL query using prepared statements. Nothing i'm trying works. I was able to get it work before I implemented the prepared statements. I'm reading a lot of conflicting information as well as OO and PDO information that doesn't seem to work with procedural code. I may switch over to one of those styles in the future, but I don't want to have to rewrite this whole website right now. Any help is greatly appreciated. I have an item_template.php that reads the data with rows['name'], rows['type'], etc. It was working mysqli_fetch_assoc...I just can't get fetch_assoc to work with the prepared statement.



  if ($searchname == NULL) {
echo 'You must enter something to search for!';
}else{
$sql = "SELECT * FROM itemdb WHERE name LIKE ?";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("Location: viewresults.php?error=sqlerror2");

exit();
}
else{
$searchname = "%".$searchname."%";
mysqli_stmt_bind_param($stmt,"s", $searchname);
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);

$resultcheck = mysqli_stmt_num_rows($stmt);
echo $resultcheck;
if ($resultcheck == 0) {
echo 'No results found! Try again! ' . $resultcheck;
exit();
}else{
$result = mysqli_stmt_get_result($stmt);
echo $result;

while ($row = mysqli_fetch_assoc($stmt)) {

include("item_template.php");
echo "SUCCESS";
}
}
}
}









share|improve this question






















  • What is $stmt? What parameter does mysqli_fetch_assoc expect? (Hint it is a mysqli_result). var_dump is your friend.
    – ficuscr
    Dec 28 '18 at 5:59












  • I tried using mysqli_fetch_assoc with mysqli_stmt_store_result with no luck either though. I thought the statement would have been the mysqli_result. I had no issues when I wasn't using prepared statements...Thats what's got me all out of wack. I don't know what i'm doing and I think im confused on something lol. I've been trying to figure this out forever. Most of the examples and help i'm finding are OO and PDO which have just enough differences to not be of help.
    – Bassex
    Dec 28 '18 at 6:05










  • replace while ($row = mysqli_fetch_assoc($stmt)) with WHILE( null !== ($row = mysqli_fetch_assoc($stmt)))
    – Umar Abdullah
    Dec 28 '18 at 6:13












  • Still doesn't work. That's almost the same as what I have just in a different format?
    – Bassex
    Dec 28 '18 at 6:19






  • 1




    Your trying to fetch the data from the statement and not the result - mysqli_fetch_assoc($stmt) should be mysqli_fetch_assoc($result)
    – Nigel Ren
    Dec 28 '18 at 7:14














0












0








0







I'm trying to fetch an associative array from an SQL query using prepared statements. Nothing i'm trying works. I was able to get it work before I implemented the prepared statements. I'm reading a lot of conflicting information as well as OO and PDO information that doesn't seem to work with procedural code. I may switch over to one of those styles in the future, but I don't want to have to rewrite this whole website right now. Any help is greatly appreciated. I have an item_template.php that reads the data with rows['name'], rows['type'], etc. It was working mysqli_fetch_assoc...I just can't get fetch_assoc to work with the prepared statement.



  if ($searchname == NULL) {
echo 'You must enter something to search for!';
}else{
$sql = "SELECT * FROM itemdb WHERE name LIKE ?";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("Location: viewresults.php?error=sqlerror2");

exit();
}
else{
$searchname = "%".$searchname."%";
mysqli_stmt_bind_param($stmt,"s", $searchname);
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);

$resultcheck = mysqli_stmt_num_rows($stmt);
echo $resultcheck;
if ($resultcheck == 0) {
echo 'No results found! Try again! ' . $resultcheck;
exit();
}else{
$result = mysqli_stmt_get_result($stmt);
echo $result;

while ($row = mysqli_fetch_assoc($stmt)) {

include("item_template.php");
echo "SUCCESS";
}
}
}
}









share|improve this question













I'm trying to fetch an associative array from an SQL query using prepared statements. Nothing i'm trying works. I was able to get it work before I implemented the prepared statements. I'm reading a lot of conflicting information as well as OO and PDO information that doesn't seem to work with procedural code. I may switch over to one of those styles in the future, but I don't want to have to rewrite this whole website right now. Any help is greatly appreciated. I have an item_template.php that reads the data with rows['name'], rows['type'], etc. It was working mysqli_fetch_assoc...I just can't get fetch_assoc to work with the prepared statement.



  if ($searchname == NULL) {
echo 'You must enter something to search for!';
}else{
$sql = "SELECT * FROM itemdb WHERE name LIKE ?";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("Location: viewresults.php?error=sqlerror2");

exit();
}
else{
$searchname = "%".$searchname."%";
mysqli_stmt_bind_param($stmt,"s", $searchname);
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);

$resultcheck = mysqli_stmt_num_rows($stmt);
echo $resultcheck;
if ($resultcheck == 0) {
echo 'No results found! Try again! ' . $resultcheck;
exit();
}else{
$result = mysqli_stmt_get_result($stmt);
echo $result;

while ($row = mysqli_fetch_assoc($stmt)) {

include("item_template.php");
echo "SUCCESS";
}
}
}
}






php mysql arrays mysqli






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 28 '18 at 5:53









BassexBassex

264




264












  • What is $stmt? What parameter does mysqli_fetch_assoc expect? (Hint it is a mysqli_result). var_dump is your friend.
    – ficuscr
    Dec 28 '18 at 5:59












  • I tried using mysqli_fetch_assoc with mysqli_stmt_store_result with no luck either though. I thought the statement would have been the mysqli_result. I had no issues when I wasn't using prepared statements...Thats what's got me all out of wack. I don't know what i'm doing and I think im confused on something lol. I've been trying to figure this out forever. Most of the examples and help i'm finding are OO and PDO which have just enough differences to not be of help.
    – Bassex
    Dec 28 '18 at 6:05










  • replace while ($row = mysqli_fetch_assoc($stmt)) with WHILE( null !== ($row = mysqli_fetch_assoc($stmt)))
    – Umar Abdullah
    Dec 28 '18 at 6:13












  • Still doesn't work. That's almost the same as what I have just in a different format?
    – Bassex
    Dec 28 '18 at 6:19






  • 1




    Your trying to fetch the data from the statement and not the result - mysqli_fetch_assoc($stmt) should be mysqli_fetch_assoc($result)
    – Nigel Ren
    Dec 28 '18 at 7:14


















  • What is $stmt? What parameter does mysqli_fetch_assoc expect? (Hint it is a mysqli_result). var_dump is your friend.
    – ficuscr
    Dec 28 '18 at 5:59












  • I tried using mysqli_fetch_assoc with mysqli_stmt_store_result with no luck either though. I thought the statement would have been the mysqli_result. I had no issues when I wasn't using prepared statements...Thats what's got me all out of wack. I don't know what i'm doing and I think im confused on something lol. I've been trying to figure this out forever. Most of the examples and help i'm finding are OO and PDO which have just enough differences to not be of help.
    – Bassex
    Dec 28 '18 at 6:05










  • replace while ($row = mysqli_fetch_assoc($stmt)) with WHILE( null !== ($row = mysqli_fetch_assoc($stmt)))
    – Umar Abdullah
    Dec 28 '18 at 6:13












  • Still doesn't work. That's almost the same as what I have just in a different format?
    – Bassex
    Dec 28 '18 at 6:19






  • 1




    Your trying to fetch the data from the statement and not the result - mysqli_fetch_assoc($stmt) should be mysqli_fetch_assoc($result)
    – Nigel Ren
    Dec 28 '18 at 7:14
















What is $stmt? What parameter does mysqli_fetch_assoc expect? (Hint it is a mysqli_result). var_dump is your friend.
– ficuscr
Dec 28 '18 at 5:59






What is $stmt? What parameter does mysqli_fetch_assoc expect? (Hint it is a mysqli_result). var_dump is your friend.
– ficuscr
Dec 28 '18 at 5:59














I tried using mysqli_fetch_assoc with mysqli_stmt_store_result with no luck either though. I thought the statement would have been the mysqli_result. I had no issues when I wasn't using prepared statements...Thats what's got me all out of wack. I don't know what i'm doing and I think im confused on something lol. I've been trying to figure this out forever. Most of the examples and help i'm finding are OO and PDO which have just enough differences to not be of help.
– Bassex
Dec 28 '18 at 6:05




I tried using mysqli_fetch_assoc with mysqli_stmt_store_result with no luck either though. I thought the statement would have been the mysqli_result. I had no issues when I wasn't using prepared statements...Thats what's got me all out of wack. I don't know what i'm doing and I think im confused on something lol. I've been trying to figure this out forever. Most of the examples and help i'm finding are OO and PDO which have just enough differences to not be of help.
– Bassex
Dec 28 '18 at 6:05












replace while ($row = mysqli_fetch_assoc($stmt)) with WHILE( null !== ($row = mysqli_fetch_assoc($stmt)))
– Umar Abdullah
Dec 28 '18 at 6:13






replace while ($row = mysqli_fetch_assoc($stmt)) with WHILE( null !== ($row = mysqli_fetch_assoc($stmt)))
– Umar Abdullah
Dec 28 '18 at 6:13














Still doesn't work. That's almost the same as what I have just in a different format?
– Bassex
Dec 28 '18 at 6:19




Still doesn't work. That's almost the same as what I have just in a different format?
– Bassex
Dec 28 '18 at 6:19




1




1




Your trying to fetch the data from the statement and not the result - mysqli_fetch_assoc($stmt) should be mysqli_fetch_assoc($result)
– Nigel Ren
Dec 28 '18 at 7:14




Your trying to fetch the data from the statement and not the result - mysqli_fetch_assoc($stmt) should be mysqli_fetch_assoc($result)
– Nigel Ren
Dec 28 '18 at 7:14












1 Answer
1






active

oldest

votes


















0














What encoding are you using in your DB? I use utf8_general_ci and cyrillic. So



mysqli_set_charset($conn, "utf8");


right after $conn=mysqli_connect(); helps me






share|improve this answer





















    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%2f53954220%2fgetting-an-associative-array-back-from-prepared-statement%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    What encoding are you using in your DB? I use utf8_general_ci and cyrillic. So



    mysqli_set_charset($conn, "utf8");


    right after $conn=mysqli_connect(); helps me






    share|improve this answer


























      0














      What encoding are you using in your DB? I use utf8_general_ci and cyrillic. So



      mysqli_set_charset($conn, "utf8");


      right after $conn=mysqli_connect(); helps me






      share|improve this answer
























        0












        0








        0






        What encoding are you using in your DB? I use utf8_general_ci and cyrillic. So



        mysqli_set_charset($conn, "utf8");


        right after $conn=mysqli_connect(); helps me






        share|improve this answer












        What encoding are you using in your DB? I use utf8_general_ci and cyrillic. So



        mysqli_set_charset($conn, "utf8");


        right after $conn=mysqli_connect(); helps me







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 28 '18 at 9:12









        vladkrasvladkras

        10k22642




        10k22642






























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53954220%2fgetting-an-associative-array-back-from-prepared-statement%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

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas