arrays - php mysqli_query function seems to run twice -
this custom function site need show different url's inside iframe. want show each url once user first creating array of available urls, array of seen urls , compare 2 create new array urls shown. need echo first element of array iframe.
the problem seems lie within foreach function execute entry database $entry = "insert views values ('', '$currentusername', '$urltoshow')";
i tried many different approaches, no matter 2 urls $urlstoshow
array added database , 1 of them gets echoed inside iframe. result every other site gets skipped altogether , user never sees them.
i have print_r'd $urlstoshow
make sure array , did same make sure $urltoshow
not array.
i'm not sure if php problem more...
here code:
function get_urls() { require 'config.php'; global $con; global $currentusername; $con = mysqli_connect($hostname, $dbusername, $dbpassword, $dbname); $query = "select site_url sites site_url not null"; $result = mysqli_query($con, $query); // site urls 1 array $siteurls = array(); $index = 0; while($row = mysqli_fetch_assoc($result)) { $siteurls[$index] = $row['site_url']; $index++; } $query2 = "select site_url views user = '$currentusername' , site_url not null"; $result2 = mysqli_query($con, $query2); // urls user has seen array $seenurls = array(); $index = 0; while($row2 = mysqli_fetch_assoc($result2)) { $seenurls[$index] = $row2['site_url']; $index++; } // compare 2 arrays , create yet array of urls show $urlstoshow = array_diff($siteurls, $seenurls); if (!empty($urlstoshow)) { // echo url show iframe within browse.php , add entry database user has seen site foreach ($urlstoshow $urltoshow) { $entry = "insert views values ('', '$currentusername', '$urltoshow')"; mysqli_query($con,$entry); echo $urltoshow; break; } } // show allseen file when ads seen else {echo 'includes/allseen.php';} mysqli_free_result($result); mysqli_close($con); }
edit
i took advice from here , made function smaller, same result still happening. 2 urls added views table, first of 2 gets echoed in iframe.
function get_urls() { require 'config.php'; global $con; global $currentusername; $con = mysqli_connect($hostname, $dbusername, $dbpassword, $dbname); $query = "select sites.site_url sites left join views on views.site_url=sites.site_url , views.user='$currentusername' sites.site_url not null , views.site_url null"; $result = mysqli_query($con,$query); $urlstoshow = mysqli_fetch_assoc($result); if (!empty($urlstoshow)) { $urltoshow = $urlstoshow['site_url']; echo $urltoshow; $entry = "insert views values ('', '$currentusername', '$urltoshow')"; mysqli_query($con,$entry); } else {echo 'includes/allseen.php';} mysqli_free_result($result); mysqli_close($con); }
edit 2
i have tried php readfile(); function because thought perhaps it's iframe that's causing this. no, same result. maybe i'm calling function wrong?
<iframe src="<?php get_urls();?>"/>
edit 3
i have tried echo'ing iframe html tags within function along urltoshow see if makes difference. nope, didn't.
anyone?
do have access php error log? path debug two-fold:
add
static $calls = 0;
top of function, , add line undererror_log('called '.(++$called).' time(s).');
. tell if being called more 1 times part of same request.if determine being called multiple times in same request, try logging first entry in debug_backtrace.
place following @ beginning of function log location function called from.
$trace = current(debug_backtrace()); error_log("called {$trace['file']}:{$trace['line']}");
combined, these 2 things should identify problem.
Comments
Post a Comment