Jump to content

Quickie - returning specific info from a Select * request


spenceddd

Recommended Posts

Hello,

 

Could anyone please show me how to access specific items from the array returned from this request:

 

//Request all info from table
$query = "SELECT * FROM PortfolioItems ";   
$wholePortfolioArray = mysql_query($query) or die(mysql_error()); 

 

...where the table looks like:

 

ID MN OX DD

01 YY NN XX

02 YY MM XX

03 NN MM XX

04 NN MN XO

 

For instance I would like to return just the 'ID' and 'OX' colomns of each row.

 

I would also like to know how to return the value at ID = 03, colomn 'MN'.

 

The reason I am doing it like this is because I only want to make the request to the server before the page loads. After that I want to use javascript to sort through the information.

 

Maybe there is a way to convert the entire array to a javascript array for client side access...?

 

Thanks very mcuh for any help.

 

Spencer

 

 

 

 

Link to comment
Share on other sites

<?php
/*how to echo each returned row :: the rest is up to you to figure out */
$ar = mysql_query($query);
while($info = mysql_fetch_assoc($ar))
{
  foreach($info as $key=>$i)
  {
     echo "$key - $i ::";
  }
     echo "<br />
    ";
     
/*to echo a specific column*/
     echo $info['id'];
}
?>

 

This should echo out each key and data for each row. mysql_fetch_assoc() returns the first row the first time you call it and the 2nd row the second time etc...

 

 

my code has not been tested

The reason I am doing it like this is because I only want to make the request to the server before the page loads. After that I want to use javascript to sort through the information.

 

Maybe there is a way to convert the entire array to a javascript array for client side access...?

 

Php only works before the page loads. you could use php to populate the javascript array

<?php
/*how to echo each returned row :: the rest is up to you to figure out */
$ar = mysql_query($query);
$row=1;
while($info = mysql_fetch_assoc($ar))
{
  
      echo "<script type='text/javascript>
          var ar$row = new array();
       ";
  foreach($info as $key=>$i)
  {
     echo "ar.$key = $i
";
  }
$row++;
}
?>

 

my js is a little rusty so you might have to correct mistakes but you get the picture.

 

Have fun exploring.

Link to comment
Share on other sites

Okay I have used the json object here (I think succesfully as it prints ok):

 


//Request all info from database
$query = "SELECT * FROM PortfolioItems ";   
$wholePortfolioArray = mysql_query($query) or die(mysql_error()); 
   // iterate over every row
    while ($row = mysql_fetch_assoc($wholePortfolioArray)) {
        // for every field in the result..
        for ($i=0; $i < mysql_num_fields($wholePortfolioArray); $i++) {
            $info = mysql_fetch_field($wholePortfolioArray, $i);
            $type = $info->type;

            // cast for real
            if ($type == 'real')
                $row[$info->name] = doubleval($row[$info->name]);
            // cast for int
            if ($type == 'int')
                $row[$info->name] = intval($row[$info->name]);
        }

        $rows[] = $row;
    }

    // JSON-ify all rows together as one big array
    echo json_encode($rows);
    
   

 

Now I am trying to recall the info in javascript.

 

Could anyone help me print the array in javascript?

 

Thanks again for any help on this.

 

Spence

Link to comment
Share on other sites

If you're populating a javascript array with PHP code. Then PHP has a handy function called json_encode which will do all the hard work for you.

you would think with all the php i do i would have discovered that long ago.... Learn somethin new everyday.

 

Now I am trying to recall the info in javascript.

 

Could anyone help me print the array in javascript?

 

Thanks again for any help on this.

 

Spence

You might want to check out the w3schools tutorial on js loops. Here is a link for the for each equivalent in js

 

http://pietschsoft.com/post/2008/02/28/JavaScript-ForEach-Equivalent.aspx

 

havnt tested it though

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.