HELPMEPLEASE
HELPMEPLEASE

Reputation: 51

SQL query returning fieldnames but not values

enter image description here

I am trying to return the Field values. I tried it in PHP and it works but returns the same field over and over. So tested by doing a direct query and this is what I get...

Here is the function that I use to grab the data:

<?php

function display_orders( $user_id, $limit ) {

    $data = array();

    $user_id = (int)$user_id;
    $limit = (int)$limit;

    $func_num_args = func_num_args();
    $func_get_args = func_get_args();

    //print_r($func_get_args);

    if ($func_num_args > 1) {

        unset($func_get_args[0]);
        unset($func_get_args[1]);


        $fields = '`' . implode('`, `', $func_get_args) . '`';

            for($x = 0; $x < $limit; $x++) {    

                $data[] = mysql_fetch_assoc( mysql_query("SELECT $fields
                                                          FROM  `users` ,  `vendor` 
                                                          WHERE $user_id = users.id 
                                                          AND $user_id = vendor.user_id 
                                                          ORDER BY vendor.DateRequested DESC") );

            }

        return $data;
    }

}

?>

This will return the following and it will just loop the newegg entry over and over.

enter image description here

Thanks for all your help I love this community!

Upvotes: 0

Views: 166

Answers (2)

pilcrow
pilcrow

Reputation: 58534

SELECT 'field' (single quotes, as in your phpmyadmin query) selects the literal string field.

SELECT `field` (backticks, as in your PHP query) selects the values from the field named field.

mysql_fetch_assoc() returns a single row from a query result handle. You are meant to capture the return of mysql_query(), which is a result handle, and repeatedly "fetch" rows from that. Instead you call mysql_query() a number of times, returning a new result set each time, and fetch the first row from the result set each time.

UPDATE

You want something like this:

$results = mysql_query(...);
for ($x = 0; $x < $limit; $x++) {
  $data[] = mysql_fetch_assoc($results);
}

It would perhaps be a better idea to put that $limit inside the query itself (LIMIT ...).

Upvotes: 1

andrewb
andrewb

Reputation: 3095

The issue is you quote the column names which means for every matching row from the result of your query, that value will be returned as a column.

Simply remove the quotes around your columns. Try it for one column first, verify it works and then do the same for the rest.

Upvotes: 1

Related Questions