Solved

why i key in 10 element to an array, only 2 shown on the screen

Posted on 2008-06-16
10
227 Views
Last Modified: 2008-06-23
PHP
why i key in 10 element to an array, only 2 shown on the screen

i create one function to deal with calling data from the database
To get the correct data i need to Select value with codition
Example:
data 1 where index =1
data 2 where index =2
data 3 where index =3
data 4 where index =4
data 5 where index =5
data 6 where index =6
data 7 where index =7

To do this i create an array first

   $index = array(1,2,3,4,5,6,7)

then i use for loop to determine how many array element i have as follow

   for($j=0;$j<sizeof($index);$j++)
   
After that i create a select query to get the data from database as follow

   result = mysql_query(select A from table where INDEX = $index[$i])

Then i create a while loop to loop the row that i get from database
   
   $i=0;  
   while(mysql_fetch_array(result)
    {
     $this->result[$i++] = $row['A'];
 
  reason i use array and $i inreament by 1 is because each time the while loop, loop thru the select query
  it will store the data in new element in array.

  then i try to print_r($this->result) it on the screen and i get the result i want which is shown below
  Array ( [0] => "3" [1] => "37" [2] => "38" [3] => "58"
   
-------------
Ok then i create another function

this function first i try to print_r($this->result) and i get the output above which is correct

when try to do like this

foreach($this->anotherfunctionresult as $k => $data)
{
data1 =" $data | {$this->result[$k] ";
}

***forgot to tell i got only two record in my database***

i do print_r... i get two value only that print on the screen which is
3 and 37

i dont understand since i can print_r($this->result) and get the complete result, why i cant get it data1

pls help







0
Comment
Question by:firekiller15
  • 4
  • 3
  • 3
10 Comments
 
LVL 4

Assisted Solution

by:trippy1976
trippy1976 earned 125 total points
ID: 21799673
Why Not:
$index = array(1,2,3,4,5,6,7);
 

$wherestring = " where INDEX = 1";

for($i = 1; $i < count($index) ; $i++)

{

  $wherestring .= " OR INDEX = " . $index[$i];

}
 

$query = "select A from TABLE $where";

$result = mysql_query($query,$myDB);

while($row = mysql_fetch_array($result,1))

{

// Get each A...

$myvar = $row['A'];

}

Open in new window

0
 
LVL 4

Assisted Solution

by:trippy1976
trippy1976 earned 125 total points
ID: 21799680
Benefit of this is one single database call vs. 7 or 8.
0
 
LVL 2

Accepted Solution

by:
ayus80 earned 125 total points
ID: 21799990
trippy1976:
I think SQL Query is:
$query = "select A from TABLE $where";

$result = mysql_query($query,$myDB);

Open in new window

0
 
LVL 2

Assisted Solution

by:ayus80
ayus80 earned 125 total points
ID: 21799994
ayus80:
trippy1976:
I think SQL Query is:
$query = "select A from TABLE $wherestring";

$result = mysql_query($query,$myDB);

Open in new window

0
 

Author Comment

by:firekiller15
ID: 21800128
what is $myDB??
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:firekiller15
ID: 21800142
i recieverd error messae
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in
0
 
LVL 2

Assisted Solution

by:ayus80
ayus80 earned 125 total points
ID: 21800305
do you have replace $where to $wherestring?
0
 

Author Comment

by:firekiller15
ID: 21800389
yup
0
 

Author Comment

by:firekiller15
ID: 21800396
and also the array message is about supplied argument is not a valid MySQL result resource

it says this
$result = mysql_query($query,$myDB);
while($row = mysql_fetch_array($result,1))
is not value and
is $myDB mean databasename?
0
 
LVL 4

Assisted Solution

by:trippy1976
trippy1976 earned 125 total points
ID: 21803808
You have to initialize your database connection.  When you do that, you get a database object.  In my code (which should be taken as an example, though I tried to match it to your case) I assume "$myDB" is your database variable which you have already set up.

Mine is initialized by the following.  Assuming again you have appropriate values in the host, user, pass and databaseName entries.


$myDB = mysql_connect($host,$user,$pass);

mysql_select_db($databaseName,$myDB);

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now