OK. Sorry if the subject didnt make sense.
I ran a query in a php script that was supposed to pull a list of band members for a particular artist from a table in my database. I only had one band member in my table at the time and it worked fine. I added the remaining 2 band members for this artist into my table using phpmyadmin and refreshed the php script but it is still only picking up the first band member that I added.
Anyway, thinking it must have somehow been a problem with my very very simple query, I copied the query from my php script and ran it in phpmyadmin - It worked fine picking up all 3 band members.
The only thing I can think of is that MySQL/PHP has some sort of caching of queries going on in the background so even though I have updated the database it is picking up the results of the original query.
THis is driving me nuts!
Does anybody know if there is any query result caching that goes on with MySQL/PHP? If so, how can I check the settings on my web host and/or empty the cache?
THanks,
Richard[/img]
There are engines that can cache php output. But you need to install and configure them to make it work with your site. If you are testing it on frihost, then it is not the case. If it is your home comp, then you would have known if you had installed it.
Anyway, there might be some minor error in your code (like calling mysql_fetch_array only once instead inside a loop). It will help if you can post the chunk of code where you are fetching the data and displaying it.
Gday kv,
Its not on Frihost its on another web server (http://musicplace.my100freemb.com is the url).
I was hoping it was something other than my code, but I'll post it anyway. Here is my php code:
| Code: |
// build query to retrieve band memeber info
$mbrQuery = "SELECT members_name FROM members WHERE members_artistid=" . $_REQUEST['artist_id'];
// send query to mysql
$mbrResult = mysql_query($mbrQuery) or die("Query failed retireving band members: " . mysql_error());
// get query results
$bandMbrDetails = mysql_fetch_array($mbrResult);
//build output into string of comma seperated names
if ($bandMbrDetails) {
$firstrun=true;
foreach ($bandMbrDetails as $KEY => $VALUE) {
if ($KEY === "members_name") {
if ($firstrun == true) {
$bandMembers .= $VALUE;
$firstrun = false;
} else {
$bandMembers .= ", " . $VALUE;
}
}
}
} else {
$bandMembers = "Not listed in database";
}
|
Now, you can probably tell, but I'm relatively new to php so please ignore any "bad" programming techniques and I'm very new to MySQL.
Is there anything that I can look for in phpinfo() that might help me?
The $bandMembers variable gets outputted in the html code further down my page - It is not manipulated or used again except for when it is outputted.
Cheers,
Richard[/url]
It could be your browser that is caching the results.
Try adding this right after getting the query results.
| Code: |
| var_dump($bandMbrDetails); |
Just see what it dumps out, and post that here, please.
It will show you how the array that is returned is constructed, and might provide some clues as to why it isn't working. It will also show you if the MySQL database is only returning one row.
Here is what var_dump spits out -
| Code: |
| array(2) { [0]=> string(12) "Daniel Johns" ["members_name"]=> string(12) "Daniel Johns" } |
I had used var_dump when I first noticed the problem and it didnt help. I'm sure it has to be something with my sql because I think its obvious that MYSQL is just not returning the right results.
Anyway, this is what the table in MYSQL looks like (from my php):
And here is what the same query outputs in phpmyadmin:
Any thoughts anybody?
Cheers,
Richard
This should fix the problem
| Code: |
// build query to retrieve band memeber info
$mbrQuery = "SELECT members_name FROM members WHERE members_artistid=" . $_REQUEST['artist_id'];
// send query to mysql
$mbrResult = mysql_query($mbrQuery) or die("Query failed retireving band members: " . mysql_error());
$bandMembers = "";
// get query results
/* watch this. mysql_fetch_array will return only one row (with all selected columns) in an array. You need to loop to get all rows. */
while ( $bandMbrDetails = mysql_fetch_array($mbrResult) )
{
$bandMembers=$bandMembers.$bandMbrDetails['members_name'];
}
if($bandMembers=="") $bandMembers = "Not listed in database";
|
Thanks kv,
That worked.
So am I right in saying that doing this:
| Code: |
| $bandMbrDetails = mysql_fetch_array($mbrResult); |
only retrieves the first row of the results. But doing it your way:
| Code: |
while ( $bandMbrDetails = mysql_fetch_array($mbrResult) )
{
$bandMembers=$bandMembers.$bandMbrDetails['members_name'];
} |
Will keep retrieving rows until there are no more to retrieve?
THanks heap
| richard270384 wrote: |
| Code: | | $bandMbrDetails = mysql_fetch_array($mbrResult); |
only retrieves the first row of the results. But doing it your way:
| Code: | while ( $bandMbrDetails = mysql_fetch_array($mbrResult) )
{
$bandMembers=$bandMembers.$bandMbrDetails['members_name'];
} |
Will keep retrieving rows until there are no more to retrieve?
|
Yes. That is correct.
PHP content is usually only cached when you actively do so, meaning you have to try to get it to happen, but it can get cached depending on several different factors.
Regardless, I suggest you download the Firefox plugin called LiveHTTPHeaders, it has helped me immensely when it comes to questions about caching.
Good luck!