flit Posted April 9, 2008 Share Posted April 9, 2008 Hi there, I don't know how to explain this: I have some id's saved in a session I used the foreach (idArray as $id) to execute the following mysql query $query = "SELECT * FROM books where id=$id"; When I echo $id I see 1,2,3, and the last , makes mysql give me an error Is it possible to continue with the rest of my code after an mysql error or is there some other way to prevent this? I want to get rid of the , so the code can continue Quote Link to comment Share on other sites More sharing options...
p2grace Posted April 9, 2008 Share Posted April 9, 2008 If there's always an extra comma at the end, you could use substr to remove it. <?php $id = "1,2,3,"; $id = substr($id,0,-1); echo $id; // will echo 1,2,3 (no extra comma) ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted April 9, 2008 Share Posted April 9, 2008 trim() might be safer. If there isn't a trailing comma substr($id,0,-1) will leave you with one. <?php $id = "1,2,3,"; $id = trim ($id, ','); echo $id; // will echo 1,2,3 (no extra comma) ?> Quote Link to comment Share on other sites More sharing options...
p2grace Posted April 9, 2008 Share Posted April 9, 2008 Ahh I didn't know trim removed anything except spaces, good to know. Thanks Barand Quote Link to comment Share on other sites More sharing options...
flit Posted April 9, 2008 Author Share Posted April 9, 2008 Thanks both solutions worked, but now I have another problem When I run the code I do not receive an error message any more but the output is not what I wanted. I receive something like this Title: //without anything Author: //without anything ISBN: //without anything Quote Link to comment Share on other sites More sharing options...
Barand Posted April 9, 2008 Share Posted April 9, 2008 your code? Quote Link to comment Share on other sites More sharing options...
flit Posted April 9, 2008 Author Share Posted April 9, 2008 foreach ($idArray as $id); { $query = "SELECT * FROM books where id=$id"; $result = mysql_query($query) or $id = trim($id, ','); $row = mysql_fetch_array($result); echo "Title: ".$row['title']; echo "<br>"; echo "Author : ".$row['author']; echo "<br>"; echo "ISBN: ".$row['isbn']; ......... Quote Link to comment Share on other sites More sharing options...
flit Posted April 9, 2008 Author Share Posted April 9, 2008 Title: //without anything Author: //without anything ISBN: //without anything ............this happens when I press on reload/refresh Quote Link to comment Share on other sites More sharing options...
p2grace Posted April 9, 2008 Share Posted April 9, 2008 Where's the idarray coming from? Quote Link to comment Share on other sites More sharing options...
Barand Posted April 9, 2008 Share Posted April 9, 2008 $result = mysql_query($query) or $id = trim($id, ','); ??? ??? ??? what does echo '<pre>', print_r($idArray , 1) , '</pre>'; output Quote Link to comment Share on other sites More sharing options...
flit Posted April 9, 2008 Author Share Posted April 9, 2008 Where does he $IdArray come from??? -> $id = $_SESSION['sessionvar']; idArray = explode(',' , $id); Did you meant this part?? Quote Link to comment Share on other sites More sharing options...
phpSensei Posted April 9, 2008 Share Posted April 9, 2008 $idArray comes from your session you exploded after each comma, then you put it in your foreach ($idArray as $id), so print_r($idArray) before the foreach, or even after, but not in it. Quote Link to comment Share on other sites More sharing options...
Barand Posted April 9, 2008 Share Posted April 9, 2008 what does echo '<pre>', print_r($idArray , 1) , '</pre>'; output Still waiting for an answer to that one Quote Link to comment Share on other sites More sharing options...
flit Posted April 9, 2008 Author Share Posted April 9, 2008 Ow sorry Barrand. I don't understand what you mean. But the program is supposed to run like this There are some session id's in a session file sessionvar|s:7:"3,2,1"; The code creates a session id -> foreach The code echos for every id in the session info from an msql data echo "Title :".$row['id']; But when I press to refresh in my browser, there comes some info, but no mysql data I don't know if I understood you correctly but is this what you meant?? Quote Link to comment Share on other sites More sharing options...
Barand Posted April 9, 2008 Share Posted April 9, 2008 Simple enough. Put this line of code in your program and tell us what it outputs echo '<pre>', print_r($idArray , 1) , '</pre>'; Quote Link to comment Share on other sites More sharing options...
flit Posted April 9, 2008 Author Share Posted April 9, 2008 It outputs, 1 Connected Successfully Array([0]=>1[1] =>1) --> Array ( [0] => 1 [1] => 1 ) The program works perfectly until I press refresh Then comes blank data Quote Link to comment Share on other sites More sharing options...
p2grace Posted April 9, 2008 Share Posted April 9, 2008 Are the sessions being lost for some reason on refresh? Quote Link to comment Share on other sites More sharing options...
flit Posted April 9, 2008 Author Share Posted April 9, 2008 It outputs, 1 Connected Successfully Array([0]=>1[1] =>1) --> Array ( [0] => 1 [1] => 1 [2] => [3] => ) The program works fine until I refresh the page Quote Link to comment Share on other sites More sharing options...
flit Posted April 9, 2008 Author Share Posted April 9, 2008 I really don't know. Can't I trim that info too?? Quote Link to comment Share on other sites More sharing options...
flit Posted April 9, 2008 Author Share Posted April 9, 2008 When I open the session file I see sessionvar|s:8:"1,2,3,,,"; I think that this might have caused the problem, anyone know a sulotion for this??? Quote Link to comment Share on other sites More sharing options...
phpSensei Posted April 10, 2008 Share Posted April 10, 2008 When I open the session file I see sessionvar|s:8:"1,2,3,,,"; I think that this might have caused the problem, anyone know a sulotion for this??? Are you adding to the sessions? How are you inserting the data to the $_SESSION Quote Link to comment Share on other sites More sharing options...
flit Posted April 10, 2008 Author Share Posted April 10, 2008 I am inserting data into the session via the url <a href="books.php?action=add&id=1"> When I open http://localhost/books.php it inserts some data that I do not want in the page When I press refresh it inserts it again. Quote Link to comment 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.