1

Before I begin, I want to point out that I can solve my problem. I've rehearsed enough in PHP to be able to get a workaround to what I'm trying to do. However I want to make it modular; without going too much into detail to further confuse my problem, I will simplify what I am trying to do so that way it does not detract from the purpose of what I'm doing. Keep that in mind.

I am developing a simple CMS to manage a user database and edit their information. It features pagination (which works), and a button to the left that you click to open up a form to edit their information and submit it to the database (which also works).

What does not work is displaying each row from MySQL in a table using a very basic script which I won't get into too much detail on how it works. But it basically does a database query with this:

SELECT * FROM users OFFSET (insert offset here) LIMIT (insert limit here)

Essentially, with pagination, it tells what number to offset, and the limit is how many users to display per page. These are set, defined, and tested to be accurate and they do work. However, I am not too familiar how to handle these results.

Here is an example query on page 2 for this CMS:

SELECT * FROM users OFFSET 10 LIMIT 10

This should return 10 rows, 10 users down in the database. And it does, when I try this command in command prompt, it gives me what I need: enter image description here

But when I try to handle this data in PHP like this:

<?php
    while ($row = $db->query($pagination->get_content(), "row")) {
        print_r($row);

    }
?>

$db->query method is:

public function query($sql, $type = "assoc") {
    $this->last_query = $sql;
    $result = mysql_query($sql, $this->connection);
    $this->confirm_query($result);

    if ($type == "row") {
        return mysql_fetch_row($result);
    } elseif ($type == "assoc" || true) {
        return mysql_fetch_assoc($result);
    } elseif ($type == "array") {
        return mysql_fetch_array($result);
    } elseif ($type == false) {
        return $result;
    }
}

$pagination->get_content method is:

    public function get_content() {
        global $db;

        $query = $this->base_sql;

        if (!isset($_GET["page"])) {
            $query .= " LIMIT {$this->default_limit}";
            return $query;
        } elseif (isset($_GET["page"]) && $_GET["page"] == 1) {
            $query .= " LIMIT {$this->default_limit}";
            return $query;
        } elseif (isset($_GET["page"])) {
            $query .= " LIMIT {$this->default_limit}";
            $query .= " OFFSET " . (($_GET["page"] * $this->default_limit) - 10);
            return $query;
        }
    }

And my results from the while loop (which should print out each row of the database, no?) gives me the same row everytime, continuously until PHP hits the memory limit/timeout limit.

enter image description here

Forgive me if its something simple. I rarely ever handle database data in this manner. What I want it to do is show the 10 users I requested. Feel free to ask any questions.

AFTER SOME COMMENTS, I'VE DECIDED TO SWITCH TO MYSQLI FUNCTIONS AND IT WORKS

    // performs a query, does a number of actions dependant on $type
public function query($sql, $type = false) {
    $sql = $this->escape($sql);

    if ($result = $this->db->query($sql)) {
        if ($type == false) {
            return $result;
        } elseif ($type == true || "assoc") {
            if ($result->num_rows >= 2) {
                $array;
                $i = 1;

                while ($row = $result->fetch_assoc()) {
                    $array[$i] = $row;
                    $i++;
                }

                return $array;
            } elseif ($result->num_rows == 1) {
                return $result->fetch_assoc();
            }
        } elseif ($type == "array") {
            if ($result->num_rows >= 2) {
                $array;
                $i = 1;

                while ($row = $result->fetch_array()) {
                    $array[$i] = $row;
                    $i++;
                }

                return $array;
            } elseif ($result->num_rows == 1) {
                return $result->fetch_array();
            }
        }
    } else {
        die("There was an error running the query, throwing error: " . $this->db->error);
    }
}

Basically, in short, I took my entire database, deleted it, and remade another one based on the OOD mysqli (using the class mysqli) and reformatted it into a class that extends mysqli. A better look at the full script can be found here:

http://pastebin.com/Bc00hESn

And yes, it does what I want it to. It queries multiple rows, and I can handle them however I wish using the very same methods I planned to do them in. Thank you for the help.

  • mysql_* functions again.... come on people :( – steve Jun 18 '15 at 21:36
  • I'm assuming your commenting on my use of "mysql_fetch_row" and others? Well if you're going to say that, at least tell me that I should use mysqli version if it truly makes the difference. From what I've been doing so far, I never really needed to use the newer functions. – Christopher 'Solidus' DeJong Jun 18 '15 at 21:39
  • 1
    `limit start_rows,rows_to_fetch`. you just keep running the SAME query, fetching the first `$this->default_limit` rows. you need to INCREMENT that value, so you're fetching `limit $current_page,$this->default_limit`. – Marc B Jun 18 '15 at 21:39
  • but then your still going to run one query for every row - which will be every inefficient –  Jun 18 '15 at 21:42
  • If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jun 18 '15 at 21:43
  • you should avoid using those functions, not because you may not need further functionality, but rather they will one day (in the fairly near future) suddenly cease to exist! – steve Jun 18 '15 at 21:43
  • The way I did it before was individually querying a single row, outputting, and then re-querying for the next user. Perhaps what I was doing before was correct and I'm just an idiot. Before you all start flaming me, I'm going to do some refactoring and exchange the mysql_ functions to mysqli_ instead. – Christopher 'Solidus' DeJong Jun 18 '15 at 21:43
  • not at all, it's just harder for me to picture the mistake because I havent used the mysql_functions in forever.. but mysql_fetch_row only ever returns one row.. ask for an array :D – steve Jun 18 '15 at 21:48
  • before jumping to mysqli, make the leap to PDO instead and make your life easier http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers – steve Jun 18 '15 at 21:50
  • I have no need to use PDO nor do I use any resources that requires me to use PDO. The business I work for uses strictly WAM servers with extensions to other web-based programming languages such as PHP which I develop in. If there is ever any project that uses any other kind of database that isn't MySQL based, then I'll use PDO. No sense constructing a framework that I won't use the functionality for, no? – Christopher 'Solidus' DeJong Jun 22 '15 at 20:40

1 Answers1

1

I think you should be using mysql_fetch_assoc():

<?php
    while ($row = $db->query($pagination->get_content())) {
        print_r($row);
    }
?>
  • billy is correct, as commented above, mysql_fetch_row only returns one row – steve Jun 18 '15 at 21:51
  • Yeah, I remember trying to fetch_assoc, no luck there. Give me a moment, I'll retest in a moment. – Christopher 'Solidus' DeJong Jun 18 '15 at 21:52
  • Yes, I have tried to use fetch_assoc, but puts the page on load. Not exactly sure why this is, but I think my best shot is to refactor how my database works entirely. From what everyone says about using mysql_ functions, apparently mysqli or PDO is the way to go. As for why I use mysql_ functions, its mostly due to habit? – Christopher 'Solidus' DeJong Jun 18 '15 at 21:57
  • I can't imagine this not working. To iterate over rows you need to either use `mysql_fetch_assoc()` or `mysql_fetch_array()`. The former gives you field names - the only disadvantage being duplicate column names don't work. – But those new buttons though.. Jun 18 '15 at 21:59
  • What does "puts the page on load" mean? – But those new buttons though.. Jun 18 '15 at 22:00
  • After doing some testing with mysqli and its uses in OOP, I've completely redid my database framework to better suit this new practice (and might I say works 10/10). I edited my original post with the code I used to fix how my database gets queries using mysqli and it does output results exactly how I want them to. Thank you all for the help, and yes, the while loop was exactly what I needed. – Christopher 'Solidus' DeJong Jun 22 '15 at 17:19