0

I'm having trouble getting the value out of an array

Array
(
    [0] => id
    [column_name] => id
)
Array
(
    [0] => businessType
    [column_name] => businessType
)
Array
(
    [0] => name
    [column_name] => name
)
Array
(
    [0] => city
    [column_name] => city
)
...

I'm getting those values from

mysql_query("
SELECT column_name 
FROM information_schema.columns 
WHERE table_name='businessUpgrade' 
")

And in my while loop

while($row = mysql_fetch_array($result)){
   while($column = mysql_fetch_array($colName)){
     if($row[$i] == 1){
             //here comes the missing part
     }
     $i++;
   }
}

I tried diffrent things, but according to print_r, the values I need to get have the same number of id (0). Is there any way, I can get the values out of this array. I found that I should do it with foreach, but somehow everything I try it fails.

7
  • 2
    Why do you have two while loops? What are $result and $colName? Do you have two SQL queries? Commented Mar 13, 2013 at 20:09
  • The first one, checks values in other table, and when the value in other table is true/1, it should print out the value from the "problematic" table (same count number) Commented Mar 13, 2013 at 20:11
  • Every row has a 0 index, so just always do $row[0]. No need for a counter. Commented Mar 13, 2013 at 20:12
  • 1
    What is the other SQL query? With your 2 loops like this, you are checking every row in the first query against every row in the second query. Is that really what you want? Commented Mar 13, 2013 at 20:13
  • 1
    Using two loops like this is not the correct way to do this. You need to get the second array at the correct position. Have a look at mysql_data_seek. Commented Mar 13, 2013 at 20:21

3 Answers 3

1

There is no need to use 2 while loops. The $row array is an associative array, so you can use...

$columns = array();
// {query}
while($row = mysql_fetch_array($result)) {
    $columns[] = $row['column_name'];
}

var_dump($columns);

Your $columns array now contains an index of all the columns. It's a zero-index, so these can be pulled individually using:

echo $columns[0]; // The first column from the query "id"
echo $columns[2]; // The third column from the query "name"

You can also loop this array as needed.

foreach ($columns as $id => $column) {
    if ($id == 0) {
        // Do something with the first column:
        echo $column;
    } elseif ($id == 2) {
        // Do something with the third column:
        echo $column;
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Is there a way, i can only get values from 1,3,6,7... array. This is why i need the second loop
@Sebastjan, There are no 1,3,6,7 or any id index fields in your SQL query. What are you trying to do?
@Sebastjan, If you want the 1st, 3rd, 6th and 7th columns, I can edit the answer to provide that solution for you.
That what I wanted to ask, sorry :)
1

try this

$res = mysql_query("
SELECT column_name 
FROM information_schema.columns 
WHERE table_name='businessUpgrade' 
");

while($r = mysql_fetch_row($res)){
$arr[]=$r;
}

NOTE . do not use mysql. Try mysqli or PDO instead.

Comments

1

And with this:

while($row = mysql_fetch_array($result)){
  while($column = mysql_fetch_assoc($colName)){
  if(($column['0'] == $row[$i]) && ($row[$i] == 1)){
         //something like this???
  }
}
  $i++;
}

Your question is a bit messy XD.....i hope this helps anyway.

Comments

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.