Lassie Posted August 1, 2007 Share Posted August 1, 2007 I have apiece of code to retireve ads placed by a selected user from a db. The code use a function to retieve the info and then goes through the result to dispaly the records in a table. I am getting an error I have not sen before. Can anyone tell me what it means. The error is ....uninitialized string offset:0 This occurs in both cols. The code is //set short variables $u=$_SESSION['user_id']; $ads=get_ads($u); if (is_array($ads)) { echo "<h2>You have the following advertisements pending activation</h2><br/>"; echo '<table id="t4" align="center">'; echo '<caption align="center"><b>Advertisement Summary<b></caption>'; echo '<tr><th>Ad Ref</th><th>Date Placed</th></tr>'; foreach ($ads as $row) { echo '<tr><td>'; echo ($row['ad_ref']); echo '</td>'; echo '<td>'; echo ($row['date_placed']); echo '</td></tr>'; } echo '</table>' ; include ('./includes/footer.html'); } else{ echo "You have no advertisements awaiting activation"; include ('./includes/footer.html'); exit(); } The function is [code] function get_ads($u) { //connect to db $connection = db_connect(); //retrieve all ads placed by user but not activated. $query="SELECT * FROM ads WHERE user_id='$u'"; $result= mysql_query($query)or die(mysql_error()); if (!$result) return false; $result = mysql_fetch_assoc($result); return $result; } [/code] Quote Link to comment https://forums.phpfreaks.com/topic/62819-never-seen-this-error-before-help/ Share on other sites More sharing options...
GingerRobot Posted August 1, 2007 Share Posted August 1, 2007 It would be nice if you could tell us where the error occurs? Which line does the error message tell you it happens on? And do you think you could point that line out to us? It'd make it a whole stack easier to help you... Edit: Hmm, i guess i wasn't paying all that much attention - you did sort of say where it happens. I think, you need to change the way your function works slightly. You'll need to pass the result resource back to the script, and then use mysql_fetch_assoc() function in the main script: <?php function get_ads($u) { //connect to db $connection = db_connect(); //retrieve all ads placed by user but not activated. $query="SELECT * FROM ads WHERE user_id='$u'"; $result= mysql_query($query)or die(mysql_error()); $num = mysql_num_rows($result); if($num == 0){ return false }else{ return $result } } ?> $u=$_SESSION['user_id']; $ads=get_ads($u); if ($ads) { echo "<h2>You have the following advertisements pending activation</h2><br/>"; echo '<table id="t4" align="center">'; echo '<caption align="center"><b>Advertisement Summary<b></caption>'; echo '<tr><th>Ad Ref</th><th>Date Placed</th></tr>'; while($row = mysql_fetch_assoc($ads)) { echo '<tr><td>'; echo ($row['ad_ref']); echo '</td>'; echo '<td>'; echo ($row['date_placed']); echo '</td></tr>'; } echo '</table>' ; include ('./includes/footer.html'); } else{ echo "You have no advertisements awaiting activation"; include ('./includes/footer.html'); exit(); } Quote Link to comment https://forums.phpfreaks.com/topic/62819-never-seen-this-error-before-help/#findComment-312718 Share on other sites More sharing options...
Lassie Posted August 1, 2007 Author Share Posted August 1, 2007 Yes sorry. The error occurs on line 49 and 55 marked below: $u=$_SESSION['user_id']; $ads=get_ads($u); if (is_array($ads)) { echo "<h2>You have the following advertisements pending activation</h2><br/>"; echo '<table id="t4" align="center">'; echo '<caption align="center"><b>Advertisement Summary<b></caption>'; echo '<tr><th>Ad Ref</th><th>Date Placed</th></tr>'; foreach ($ads as $row) { echo '<tr><td>'; echo ($row['ad_ref']);//line 49 error echo '</td>'; echo '<td>'; echo ($row['date_placed']);//line 55 error echo '</td></tr>'; } echo '</table>' ; include ('./includes/footer.html'); } else{ echo "You have no advertisements awaiting activation"; include ('./includes/footer.html'); exit(); } Quote Link to comment https://forums.phpfreaks.com/topic/62819-never-seen-this-error-before-help/#findComment-312721 Share on other sites More sharing options...
GingerRobot Posted August 1, 2007 Share Posted August 1, 2007 Thought that might happen when i edited, probably should have added a new reply rather than editing. Ah well. Take a look at the code i suggested. Quote Link to comment https://forums.phpfreaks.com/topic/62819-never-seen-this-error-before-help/#findComment-312722 Share on other sites More sharing options...
Lassie Posted August 1, 2007 Author Share Posted August 1, 2007 thanks for that, however now get a parse error in the function at the else condition. I cant see why? function get_ads($u) { //connect to db $connection = db_connect(); //retrieve all ads placed by user but not activated. $query="SELECT * FROM ads WHERE user_id='$u'"; $result= mysql_query($query)or die(mysql_error()); $num = mysql_num_rows($result); if($num == 0) { return false }else{ //parse error return $result } } Quote Link to comment https://forums.phpfreaks.com/topic/62819-never-seen-this-error-before-help/#findComment-312733 Share on other sites More sharing options...
GingerRobot Posted August 1, 2007 Share Posted August 1, 2007 Whoops. Forgot some semi colons. And i can't even blame that on it being tired and late - its midday! Try: function get_ads($u) { //connect to db $connection = db_connect(); //retrieve all ads placed by user but not activated. $query="SELECT * FROM ads WHERE user_id='$u'"; $result= mysql_query($query)or die(mysql_error()); $num = mysql_num_rows($result); if($num == 0) { return false; }else{ //parse error return $result; } } Quote Link to comment https://forums.phpfreaks.com/topic/62819-never-seen-this-error-before-help/#findComment-312736 Share on other sites More sharing options...
Lassie Posted August 1, 2007 Author Share Posted August 1, 2007 thanks. No errors but no results returned.It goes to my else condition so something wrong somwhere as there are records to retrieve. I will investigate and give you a rest thanks Quote Link to comment https://forums.phpfreaks.com/topic/62819-never-seen-this-error-before-help/#findComment-312739 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.