views:

45

answers:

1

I've got a php file echoing hashes from a MySQL database. This is necessary for a remote program I'm using, but at the same time I need my other php script opening and checking it for specified strings POST parsing. If it checks for the string pre-parsing, it'll just get the MySQL query rather than the strings to look for.

I'm not sure if any functions do this. Does fopen() read the file prior to parsing? or file_get_contents()?

If so, is there a function that'll read the file after the php and mysql code runs?

The file with the hashes query and echo is in the same directory as the php file reading it, if that makes a difference.

Perhaps fopen reads it post-parse, and I've done something wrong, but at first I was storing the hashes directly in the file, and it was working fine. After I changed it to echo the contents of the MySQL table, it bugged out.

The MySQL Query script:

$query="SELECT * FROM list";
$result=mysql_query($query);
while($row=mysql_fetch_array($result, MYSQL_ASSOC)){
 echo $row['hash']."<br>";
 }

What I was using to get the hash from this script before, when it was just a list of hashes:

            $myFile = "hashes.php";
            $fh = fopen($myFile, 'r');
            $theData = fread($fh, filesize($myFile));
            fclose($fh);
            $mystring = $theData;
            $findme   = $hash;
            $pos = strpos($mystring, $findme);
+1  A: 

The easiest thing to do would be to modify your first php file which echoes everything, along these lines:

  • change every instance of echo to e.g. $data[] =
  • at the bottom, do foreach($data as $d) echo $d (this will produce the same result as you have right now)
  • you now still have your $data array which you can loop through and do whatever you like with it.

To provide working code examples, it would be great if you could post the current code of your file.

EDIT

If you change your script like so:

$query="SELECT * FROM list";
$result=mysql_query($query);
while($row=mysql_fetch_array($result, MYSQL_ASSOC)){
 $data[] = $row['hash']."<br />";
 }
foreach($data as $d) {
     echo $d;
}

...you'll have the array $data that contains each hash in a key. You can then loop through this array like so:

foreach($data as $d) {
  //do something
}
bobsoap
Updated with the current code.
Rob
Have you tried just re-using the variable `$result` in your other script? You could do the same while() loop there. You don't need anything like fopen() for this at all. That said, I'm not sure what kind of parsing you are doing which you'd like to preserve in the new script - all you are doing now is echoing each row with a `<br>` tag (which you could change to `<br />` to remain XHTML-compliant, by the way). Is this the parsing you mean?
bobsoap
Updated my reply above, I hope this is what you've been looking for.
bobsoap
Basically the server-side script needs to check if the hash is there.
Rob
That doesn't make any sense... sorry. What server-side script? They are all server-side. And your current one echoes the hash field. What does the hash field contain?
bobsoap
Okay sorry, let me try to explain what this script does.I have a program that connects to the script echoing the hashes from the database, to verify that the user has paid for the program. If his hash isn't echoed, the program exits.However it also does another check, each time the program is used, but its checked locally on the server instead of by the program, so the program queries the PHP script, and that php script needs to be able to query another PHP script to verify that the hash is there. I'd like to be able to do it via the file that the program originally queries
Rob
Ok... thanks for the explanation. I believe if you tried the code I posted above, you'd get what you want it to do: the existing script will still echo everything just like before, plus it will store what it echoes in the $data array for later use. So you just have to make that small change to the current script that echoes the hashes, then you can re-use the $data array in your new script (which performs the second check). Do you know what I mean?
bobsoap
Yeah that makes sense. Thanks.
Rob
Cool. Post back if you need more help.
bobsoap