logo
down
shadow

PHP fetch_assoc close in a returning function - Scope


PHP fetch_assoc close in a returning function - Scope

By : user2955582
Date : November 22 2020, 10:40 AM
I wish did fix the issue. Let's say we have a PHP file that calls a returning function , Why do you need to persist $result? Just close it right away:
code :
function foo(){
    ...
    $row = $result->fetch_assoc();
    $result->close();
    return $row;
}


Share : facebook icon twitter icon
PHP PDO Fetch_Assoc not returning correctly

PHP PDO Fetch_Assoc not returning correctly


By : Karolis Bartkus
Date : March 29 2020, 07:55 AM
like below fixes the issue I have searched, and maybe I am searching for the wrong thing, but I need help with the below code. I must be missing something...
code :
SELECT `group` FROM users WHERE username='ryan'
Why is fetch_assoc() not returning a value?

Why is fetch_assoc() not returning a value?


By : DreamWork
Date : March 29 2020, 07:55 AM
this one helps. You haven't shown the code for the LG_User constructor, but if it's filling in $user from the database, it must be calling a fetch function itself. So when it returns, all the rows of the result have been fetched, and there's nothing left for your loop to read.
You can go back to the beginning of the result set with the data_seek method.
code :
if (!$user = new LG_User($result))
    print("Failed to create user!<br>");
$result->data_seek(0);
while($row = $result -> fetch_assoc()) {
    $rows[] = $row;
}
var_dump($rows);
Fatal error to Call to a member function fetch_assoc() - why is fetch_assoc() is not working?

Fatal error to Call to a member function fetch_assoc() - why is fetch_assoc() is not working?


By : Sagar Saab
Date : March 29 2020, 07:55 AM
this will help You must execute a query before trying to get results. Usually it is done with execute:
code :
public function getUserByUserid($user_id){
    $stmt = $this->con->prepare("SELECT * FROM mydb.User WHERE user_id = ?" );
    $stmt->bind_param("s", $user_id);
    // Executing a query
    $stmt->execute();

    return $stmt->get_result()->fetch_assoc();
}
fetch_assoc() not returning data

fetch_assoc() not returning data


By : JX.W
Date : March 29 2020, 07:55 AM
With these it helps for everyone for their prompt response. As suggested by @the.salman.a, I cant say it a bug but I have a alphanumeric data that was creating a problem in call from ajax. I corrected it to alphabetic and now it is working fine.
fetch_assoc() returning only first row

fetch_assoc() returning only first row


By : user3135559
Date : March 29 2020, 07:55 AM
it helps some times That's because you return the value of $data immediately after you retrieve one row. return will return that value and stop execution of that method. You need to return that value after you are done retrieving your values.
code :
class User {

    public $connect;
    public function get_users() {
        $sql = "SELECT * from users";
        $result = $this->connect->query($sql);
        $num_rows = $result->num_rows;

        $data = [];
        if ($num_rows > 0) {
            while ($row = $result->fetch_assoc()) {
                $data[] = $row;
            }
        }
        return $data;
    }
}
class User {

    public $connect;
    public function get_users() {
        $sql = "SELECT * from users";
        $result = $this->connect->query($sql);
        $num_rows = $result->num_rows;

        $data = [];
        if ($num_rows > 0) {
            $data = $result->fetch_all(MYSQLI_ASSOC);
        }
        return $data;
    }
}
Related Posts Related Posts :
  • How to make a sitemap using zend framework 1
  • Laravel: array to Model with relationship tree
  • $_SERVER['DOCUMENT_ROOT'] vs $path = $_SERVER['DOCUMENT_ROOT']
  • What is the best way to store multiple checkbox values in the database and work with it later on
  • Why does foreach copy the array when we did not modify it in the loop?
  • Receive Jquery/Ajax Post Request in PHP
  • How to better use the if(!empty()){ //codes} statement
  • PHP error mysql_close() boolean given
  • Move an image file from one server to another using PHP
  • PHP: Encoding non ISO-8859-1 charactrers into utf8
  • Handling MySQL errors in PHP
  • Object orientated mysql num_rows query in one line
  • Remove not closed html elements from end of text
  • Generate random ENUM value [PHP, MySQL]
  • Deleting data from table using jQuery and SQL
  • How to make sure the constructor is executed when I'm trying to reach a static property?
  • Trying to understand arrow next to PHP function
  • Form using external php file to show "Sucess"
  • WooCommerce - new shipping method - how to query individual product weights
  • Common integer representation over variable casting?
  • PHP - Prevent client from tampering the ID of a form
  • unexpected T_PUBLIC error
  • Types of testcases in PHPUnit
  • keeping a LDAP user logged in with PHP session
  • Detect if browser supports WebP format? (server side)
  • real_escape_string not cleaning up entered text
  • E-commerce products filter
  • Why having more than one test method in a file using Laravel raises error?
  • PHP Escaped special characters to html
  • Basic prepared statement not INSERTING
  • PHP combobox not showing in HTML table
  • MySql PHP row request to variable
  • Join two tables with one table has multiple rows matching
  • Looping through users with Instagram API requests is REALLY slow
  • Unable to add 30 days to date within loop
  • Return from mysqli_query() into array
  • php rename() function returns "No such file or directory" error
  • form submit inside navigation menu
  • PHP: reasons to use GET and POST request at the same time?
  • Is break; required after die() php
  • little depth of session in php
  • Authentication Logon page not working using PHP and Oracle on Apache / Windows 2008
  • Magento reindexing error product flat data
  • Laravel 4: Running composer when deploying to Heroku
  • Long Polling or WebSockets
  • How to use mysqli_stmt_bind_param for integers, boolean, etc
  • Determining the content of a link
  • MYSQL and PHP update events?
  • PHP explode all square bracket
  • Transparent hexagon-masked collage using PHP GD
  • Plugins or text do NOT show up on custom wordpress template
  • inserting data into mysqli database
  • Two arguments to closure function laravel
  • UnClear image exported by ffmpeg from mp4 file
  • is the time of computer and localhost same?
  • change custom data- value after ajax post success
  • Can We Restrict PHP Variables to accept only certain type of values
  • csv file is inserting a blank row in the last of other rows into mysql table
  • How to convert special characters in xml using php?
  • Mcrypt are very confusing
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org