Solved

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

Posted on 2008-06-16
10
231 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

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.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

810 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