Solved

while loop and SQL

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
PHP and Soap 3 28
Need help with validating checkout field in WooCommerce 4 46
Instering to MySQL table 5 22
php ssh2_scp_send 1 0
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…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
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…

758 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

19 Experts available now in Live!

Get 1:1 Help Now