Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2008-06-16
10
Medium Priority
?
252 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
10 Comments
 
LVL 4

Assisted Solution

by:trippy1976
trippy1976 earned 375 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 375 total points
ID: 21799680
Benefit of this is one single database call vs. 7 or 8.
0
 
LVL 2

Accepted Solution

by:
ayus80 earned 375 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Assisted Solution

by:ayus80
ayus80 earned 375 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 375 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 375 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

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…
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.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …
Suggested Courses

610 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