0

I want to create an array based on the results of my SELECT query.

The below code works somewhat -- it only adds the last row to the array. I tried declaring the array beforehand and then using array_push but that function doesn't allow me to assign a key, just a value.

    $sql = "SELECT setKey,setValue FROM tblsettings WHERE setApp = '".$data->app."' AND setIP = '".gethostbyname(trim(gethostname()))."'";
    $result = mysql_query($sql);
    if(!$result) die(mysql_error());

    if(mysql_num_rows($result) == 0){
        echo "null";
        exit;
    }

    while($datAssArr = mysql_fetch_assoc($result)){
        $datArr = array($datAssArr["setKey"] => $datAssArr["setValue"]);
    }

    print_r($datArr);
1
  • Don't use mysql_* it's deprecated. Use mysqli or pdo. Commented Oct 28, 2013 at 0:53

1 Answer 1

1

Instead of:

$datArr = array($datAssArr["setKey"] => $datAssArr["setValue"]);

Try:

$datArr[] = array($datAssArr["setKey"] => $datAssArr["setValue"]);
// ----^

Also, don't use the mysql_* functions. They're deprecated. Try PDO instead.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, with your help I ended up using $datArr[$datAssArr["setKey"]] = $datAssArr["setValue"];
I've switched to PDO since this discussion, but am having issues... see new question here: http://stackoverflow.com/questions/19627423/inserting-multiple-rows-with-a-single-query-using-pdo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.