Jump to content

Is it possible to continue with the rest of the code on an mysql error?


flit

Recommended Posts

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

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)

?>

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

 

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'];

 

.........

 

 

 

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??

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.