TripRev Posted June 5, 2011 Share Posted June 5, 2011 Ok, this really takes the biscuit. I've been trying to debug this for hours and can't find the problem. Basically I have the result of a MySQL query stored in the var $selectList and before this code, the resultset has results in it. if( mysql_result($selectList,0,'viewhash') == 'anyone' ){ $userCanView = true; } but after, the fields have all become [empty string]. The fieldnames are intact, just the values have been set to [empty string] by the above code somehow. I've attached a screenshot of the var $selectList dumped out before and after this code. That is all that's between the 2 dumps. Serious driving me insane. Any suggestions much appreciated. Thanks. [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/238499-strangest-problem-ever/ Share on other sites More sharing options...
jcbones Posted June 5, 2011 Share Posted June 5, 2011 Is $selectList the resource, or an array derived from another call to the resource? ala. mysql_fetch_assoc... Quote Link to comment https://forums.phpfreaks.com/topic/238499-strangest-problem-ever/#findComment-1225588 Share on other sites More sharing options...
Pikachu2000 Posted June 5, 2011 Share Posted June 5, 2011 You're going to need to post more code than that. Include the query and the code you use to display the results. Quote Link to comment https://forums.phpfreaks.com/topic/238499-strangest-problem-ever/#findComment-1225590 Share on other sites More sharing options...
TripRev Posted June 5, 2011 Author Share Posted June 5, 2011 Is $selectList the resource, or an array derived from another call to the resource? ala. mysql_fetch_assoc... It's the resource, resulting from this: $selectList = mysql_query('SELECT * FROM the_table WHERE id = '.$_GET['listNum'],$con); Quote Link to comment https://forums.phpfreaks.com/topic/238499-strangest-problem-ever/#findComment-1225592 Share on other sites More sharing options...
teynon Posted June 5, 2011 Share Posted June 5, 2011 Can you post all of your code? Better info = better answers Quote Link to comment https://forums.phpfreaks.com/topic/238499-strangest-problem-ever/#findComment-1225595 Share on other sites More sharing options...
TripRev Posted June 5, 2011 Author Share Posted June 5, 2011 OK, here's more code. <?php include('funcs.php'); include('dbconnect.php'); session_start(); setDef($userMsg); setDef($_GET['userMsg']); setDef($_GET['listNum']); setDef($_SESSION['uid']); setDef($_SESSION['viewhash']); setDef($_SESSION['edithash']); $selectList = mysql_query('SELECT * FROM the_table WHERE id = '.$_GET['listNum'],$con); if( mysql_num_rows($selectList) ==1 ){ $userCanView = false; $userCanEdit = false; $editPassSet = false; if( mysql_result($selectList,0,'viewhash') == 'anyone' ){ // for some reason, this line is setting the resultset $selectList to be blank $userCanView = true; } if( $_SESSION['viewhash'] == mysql_result($selectList,0,'viewhash')){ $userCanView = true; } if( $_SESSION['edithash'] == mysql_result($selectList,0,'edithash') ){ $userCanView = true; $userCanEdit = true; } if( $_SESSION['uid'] == mysql_result($selectList,0,'owner') ){ $userCanView = true; $userCanEdit = true; } if( mysql_result($selectList,0,'edithash') != 'no' ){ $editPassSet = true; }elseif(mysql_result($selectList,0,'edithash') == $_SESSION['edithash']){ $userCanEdit = true; } // load the list's items $selectItems = mysql_query('SELECT * FROM the_table WHERE list_id = '.$_GET['listNum'].' AND status = "live" ORDER BY pos ASC, added DESC',$con); }else{ $userMsg = '<p class="msgError">That list does not exist. </p>'; header('Location:user.php?userMsg='.urlencode($userMsg)); } // redirect user to passreq.php if list requires a view password if( mysql_result($selectList,0,'owner') != $_SESSION['uid'] ){ $listViewhash = mysql_result($selectList,0,'viewhash'); if( ($listViewhash != 'no') && ($listViewhash != 'anyone') && ($listViewhash != $_SESSION['viewhash']) ){ header('Location:'.$siteURL.'/passreq.php?l='.$_GET['listNum'].'&type=view'); } // redirect to login page if user does not have view permission at least }elseif( !$userCanView ){ $userMsg = '<p class="msgError">You do not have permission to view that list. </p>'; header('Location:'.$siteURL.'/user.php?userMsg='.urlencode($userMsg)); } ?> Dumping out $selectList before and after the second IF shows that it's happening there. Driving me nuts! Quote Link to comment https://forums.phpfreaks.com/topic/238499-strangest-problem-ever/#findComment-1225613 Share on other sites More sharing options...
teynon Posted June 5, 2011 Share Posted June 5, 2011 Found on http://php.net/manual/en/function.mysql-result.php bruce at kaskubar dot com 29-Apr-2011 01:30 The warning against mixing the use of mysql_result with other result set functions is a bit generic. More specifically, mysql_result alters the result set's internal row pointer (at least in a LAMP environment). This is anything but obvious as the nature of the function is random access for grabbing a quick byte. Using mysql_data_seek after some mysql_result calls, before going into a mysql_fetch_array loop, will set things straight. Quote Link to comment https://forums.phpfreaks.com/topic/238499-strangest-problem-ever/#findComment-1225616 Share on other sites More sharing options...
xyph Posted June 5, 2011 Share Posted June 5, 2011 To avoid this problem, I always immediately dump the results into an array after I check that results were actually returned. Rather than use multiple mysql_fetch/result etc calls, I simply use the array I've populated and forget about internal pointers Quote Link to comment https://forums.phpfreaks.com/topic/238499-strangest-problem-ever/#findComment-1225673 Share on other sites More sharing options...
TripRev Posted June 7, 2011 Author Share Posted June 7, 2011 Thanks teynon and xyph, using mysql_fetch_assoc to put the results into an array before using them solved the problem. Quote Link to comment https://forums.phpfreaks.com/topic/238499-strangest-problem-ever/#findComment-1226633 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.