Solved

while loop and SQL

Posted on 2008-10-01
4
1,263 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
[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
  • 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:
Cornelia Yoder 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

724 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