-2

I write this below function for Fetch fields from DATABASE but it does not work :

function Refresh_TBL_post() {
  global $conn;
  #DB Query Comment
  $stmt = $conn->prepare("SELECT * FROM post WHERE id=? ");
  $stmt->bind_param("s", $_REQUEST['editpostId']);
  #Run Query In DB
  $stmt->execute();
  #Get Count Of Rows    
  #Refrsh $stmt
  $stmt->get_result();
  $row = $stmt->fetch_assoc();
  return $row;
}

And i call this function like this Refresh_TBL_post(); but it does not works . How can i fix it ?

Nana Partykar
  • 10,556
  • 10
  • 48
  • 77

2 Answers2

0
#Refrsh $stmt
$stmt->get_result();
$row = $stmt->fetch_assoc();
return $row;

is not the same as what you did last. You should do this:

function Refresh_TBL_post() {
  global $conn;
  #DB Query Comment
  $stmt = $conn->prepare("SELECT * FROM post WHERE id=? ");
  $stmt->bind_param("s", $_REQUEST['editpostId']);
  #Run Query In DB
  $stmt->execute();
  #Get Count Of Rows    
  #Refrsh $stmt
  $res = $stmt->get_result();
  return $res->fetch_assoc();
}

Calling with:

$row = Refresh_TBL_post();

Which should work. You fetch from get_result and not from stmt in your last post.

AltShiftZero
  • 385
  • 1
  • 16
  • Did you remove the fetch_assoc from the function call in `$row=Refresh_TBL_post()->fetch_assoc();` ? Because this should work... As it is technically the same as below what you said did work. Maybe you can clarify what it is that doesn't work. How do you try to use this code? Do you get any error in the log or does it show you any errors when display_errors is turned on? – AltShiftZero Aug 16 '16 at 12:14
  • no errors are in my code . yes when i toke out fetch_assoc from my function . it works as well .. – Mohammadali Mirhamed Aug 16 '16 at 12:39
-1

I changed function to below :

function Refresh_TBL_post()
    {
            global $conn;
            #DB Query Comment
             $stmt = $conn->prepare("SELECT * FROM post WHERE id=? ");
             $stmt->bind_param("s",$_REQUEST['editpostId']);
            #Run Query In DB
             $stmt->execute();
            #Get Count Of Rows  
             #Refrsh $stmt
              $stmt = $stmt->get_result();
         return $stmt;
    }

and use like this

#Refresh
$stmt_Refresh_Tbl_post=Refresh_TBL_post();
$row=$stmt_Refresh_Tbl_post->fetch_assoc(); 

or like this

 #Refresh
    $row=Refresh_TBL_post()->fetch_assoc();