Solved

while loop and SQL

Posted on 2008-10-01
4
1,253 Views
Last Modified: 2008-10-01
What I am trying to do is read the values form a field in my database.

I want it to look through the database something like this until there are not any more values, so I want it to start at value one and go up one by one, but I need to assign the value in the database to a variable before it and create an action, before moving onto the next value in the databse.

Basically what i have so far.

<?
$query  = "SELECT index FROM index_list";
$result = mysql_query($query);
 while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
        $search = $row;
        echo $search;
}

I get an error when I try and do this:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result
0
Comment
Question by:cstormer
  • 2
4 Comments
 

Author Comment

by:cstormer
ID: 22620857
I made some changes...

$result = mysql_query($query);
 while($row = mysql_fetch_assoc($result)){
        $search = $row[0];
        echo $search;

but now no error, but no results either....
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 22620882
You'll need to connect to the database first:
mysql_connect('host','user','pass');
mysql_select_db('dbname');

Open in new window

0
 
LVL 27

Accepted Solution

by:
yodercm earned 500 total points
ID: 22620895
$result = mysql_query($query);
echo mysql_error();
 while($row = mysql_fetch_array($result)){
        $search = $row[0];
        echo $search;


Use mysql_fetch_array, not _assoc
and echo the error message if you get one.
0
 

Author Comment

by:cstormer
ID: 22620949
at the top of the page i have

  require_once("conf/db_connection.php");, with the info in ther eto connect.

Anyway it's connecting for sure now because if i change $row[0] to $row it will list the word array 10 times and there are 10 items in the database
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses how to create an extensible mechanism for linked drop downs.
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 dynamically set the form action using jQuery.

831 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