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
Solved

while loop and SQL

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
XML extra information 8 29
PHP Echo with auto submit 8 28
selector:validator cookies 4 30
JQuery Search Filter 2 28
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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…
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…
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…

809 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