Solved

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

Posted on 2008-06-16
10
232 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP curl issue VERBOSE output 18 86
What does != "" mean in programming 8 78
Delete  php pages of a part of our site 8 31
Wordpress Only run code if on a certain page 11 22
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

840 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