I have a PHP script which takes a value from a row in my MySQL database, runs it through a function, and if it determines it's true returns one value, and if it's false, it needs to go to the next value in the database and check that one until eventually one returns true. I think I need to use mysql_fetch_assoc, but I'm not really sure in what way to use it... I wish I could post my code to be more specific, but it's a lot of code and most of it has no bearing on this issue...

No correct solution


Is the "function" something you could do in the database instead? It's really inefficient to process every row in the table to check for some type of condition. That's exactly what databases are good at, namely, processing queries efficiently and getting answers to you quickly.

So I'd recommend looking at how to do it all on the database side so that your PHP code is just fetching the end result (i.e. rows filtered by the function). Maybe if you provide more details of what your "function" is doing, a more specific answer can be provided.

You can use mysql_fetch_array, and just jump on the values fetched using $row[id] and not $row['name']. Say your function returns true, you'd just use $row[lastid+1].

If the ID isn`t incremental, this could work :

$qry_result = mysql_query($qry) or die(mysql_error());
while ($row = mysql_fetch_array($qry_result)) {
    $result = yourfunction($row['whatever');
    if ($result != false)

Also there is a php function next() which advances a pointer to the next array element. In your function you could implement an array builder, and then cycle between the elements with this. Depends on what your function actually does or script purpose is. It could lead to some load if there are alot of results.

  1. You should not check database this way, as mentioned above. Database has a little difference from the plain text file.
  2. You should not have a field in your database that has a bunch of values separated by value1:value2|value1:value2|value1:value2. It must be separate fields. Database has a little difference from the plain text file and you better learn it.

You could try something like this:

FROM table
WHERE field NOT LIKE '%$sessionvar:%';

I think that is what you are after?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow