0

I have an object that I get from a "SHOW tables" query. Unfortunately, I get an array inside an object has a name that changes based on what user is logged in (eg. data[1].Tables_in_kazura). How can I either access what user is logged in or even better rename the result from PHP, from Tables_in_xxx to whatever I want?

$.getJSON("php/loadCategories.php", function(data) {
    for (let i = 0; i < data.length; i++) {
      console.log(data[i]);
    }
});

This is my PHP

$sql = "SHOW tables";

    $rows = $db->query($sql);
    $length = $rows->num_rows;



    $result = array();
    for ($i = 0; $i < $length; $i++) {
        $row = $rows->fetch_assoc();
        array_push($result, $row);
    }

    echo json_encode($result); 

The echo $result gives me:
Object { Tables_in_kazura: "drinks" }
Object { Tables_in_kazura: "food" }

and what ever else the user (user here is kazura) has in his tables.

Since it is based on username, Tables_in_USERNAME will vary and I don't know how I can access the data inside it.

The optimal solution would be that echo json_encode($result); returns Object { categoryname: "drinks" } and so on no matter what user is logged in

PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
Kazura
  • 83
  • 1
  • 1
  • 7
  • 5
    Do you have any _relevant_ code to share? (Please see [mcve].) – evolutionxbox May 15 '17 at 14:09
  • You are already putting the results of the data in output variable so you can name it whatever you want. So, instead of console.log(data[i]), you can do console.log('Name_whatever_you_want' + output.categories[i]). Let me know if I misunderstood your question. – vabii May 15 '17 at 14:30
  • Sorry, the code snippet was very poorly pasted. I now removed all the unnecessary parts that doesn't work. – Kazura May 15 '17 at 14:34
  • Are you having trouble accessing the categories since you object key is dynamic? If that is the case take a look at this - http://stackoverflow.com/questions/675231/how-do-i-access-properties-of-a-javascript-object-if-i-dont-know-the-names. Otherwise, please provide us expected output so that what you are having trouble with. – vabii May 15 '17 at 14:56

2 Answers2

0

If the tables all have the same structure then you can just use raw SQL although this is really not advised as it leaves you open to attack. The problem seems to be that you're using a table per user rather than following a relational convention. But as a short answer, you cant do what you're looking for within a sanitized SQL query as dynamic table names aren't supported.

Kieran Devlin
  • 1,373
  • 1
  • 12
  • 28
0

I figured out a way to get my wanted result in javascript. It's not pretty, but it works.

let propertyName = Object.keys(data[1]).toString();

//Rename the query from Tables_in_USERNAME to categoryname
data = data.map(function(obj) {
  obj['categoryname'] = obj[propertyName];
  delete obj[propertyName];
  return obj;
});

I rename all the object keys to categoryname instead of changing the SQL query.

Kazura
  • 83
  • 1
  • 1
  • 7