Solved

while loop and SQL

Posted on 2008-10-01
4
1,256 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

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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 …

733 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