Solved

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

Posted on 2008-06-16
10
226 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 …

760 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

23 Experts available now in Live!

Get 1:1 Help Now