mysql_result error in Firefox browser

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Sosh
Forum Newbie
Posts: 5
Joined: Mon Sep 07, 2009 8:04 am

mysql_result error in Firefox browser

Post by Sosh »

Hi,

I'm developing a php website that's based on a simple mysql database.

the code works fine on the internet-explorer browser and on the google chrome browser, but the mozilla firefox browser displays an error "mysql_result(): supplied argument is not a valid mysql result resource"

I don't understand what's the problem

here is the code:
$query="SELECT `Name`,`Author`,`Genre`,`Form`,`condition` FROM `books` WHERE (`Book_Id` =".$BookId.");";
$result=mysql_query($query);

$Name=mysql_result($result ,0,"Name");
$Author=mysql_result($result ,0,"Author");
$Genre=mysql_result($result ,0,"Genre");
$Form=mysql_result($result ,0,"Form");
$Condition=mysql_result($result ,0,"Condition");

Thanks!!!!!!
S
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: mysql_result error in Firefox browser

Post by Eric! »

Is the field "Condition" capitialized because it isn't capitialized in your query? Perhaps this was just a typo.

Anyway, I assume you are pulling these values from a web based form and submitting them to your query. Firefox has a tendency to add linefeeds and stuff to text fields. Try using the trim() function on the data submitted by your form. http://php.net/trim

In your case $BookId=trim($BookId);
Sosh
Forum Newbie
Posts: 5
Joined: Mon Sep 07, 2009 8:04 am

Re: mysql_result error in Firefox browser

Post by Sosh »

Thank you Eric,

The field "Condition" was not capitalized in the query, but it did not make a difference (but thanks for pointing it out!).

I checked and found that firefox is adding a double ';;' to a session string when I asked it to add only one ';'
$BookId came from a session that adds the Id's of the selected books with a ';' between them
I tried trim() but it did not work, I do not want to remove all the semicolons

is there a way to fix this?

Thanks again for your help!
S
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: mysql_result error in Firefox browser

Post by Eric! »

Well, without seeing your code, it's hard to keep guessing. It seems that something isn't quite right in your program.

However a quick hack would be to just filter the string and replace double ;; with single ; that occur at the end of a string with a single ;.

Code: Select all

$input = preg_replace("/(;;)\z/i",';', $input);
If I understood your problem correctly this should patch it up. This code will only replace groups of two ;; and only at the end of a string, everything else is ignored.

If I missunderstood and you are getting double ;; in other parts of the strings then use this

Code: Select all

$input = preg_replace("/(;;)/i",';', $input);
Which will take out all doubles and replace them with a single ;.
Sosh
Forum Newbie
Posts: 5
Joined: Mon Sep 07, 2009 8:04 am

Re: mysql_result error in Firefox browser

Post by Sosh »

Thanks again,
here is the whole story:
I set a SESSION['requestedBooks'] to be a string of ID's with semicolons between them, eg: ;3;5;22;15;
Adding a book to the list like this:

Code: Select all

    $tmp=$_SESSION['requestedBooks'].$_GET['BookId'].';';
    $_SESSION[' requestedBooks ']=$tmp;
Then I display the requested books on another page, separating each ID and selecting the information from the database

Code: Select all

 
$BooksWanted=$_SESSION['requestedBooks'];
$TmpBooksWanted=substr($BooksWanted,1);  //requestedBooks without the first ';'
WHILE($TmpBooksWanted != "")        
{   //get and display each BookId separetly
$len= strpos($TmpBooksWanted ,';'); 
$BookId=substr($TmpBooksWanted,0,$len);
$TmpBooksWanted=substr($TmpBooksWanted,$len+1);         
$query="SELECT `Name`,`Author`,`Genre`,`Form`,`condition` FROM `books` WHERE (`Book_Id` =".$BookId.");";
$result=mysql_query($query);
 
$Name=mysql_result($result ,0,"Name");
$Author=mysql_result($result ,0,"Author");
$Genre=mysql_result($result ,0,"Genre");
$Form=mysql_result($result ,0,"Form");
$Condition=mysql_result($result ,0,"condition");
 
//display info in table
}
 
Now I let the user delete a requested ID- removing from the session the id and the semicolon after it.

Code: Select all

 
$BookIdlen=strlen($Book_IdToRemove); //amount of digits in the id
$len= strpos($BooksWanted ,';'.$Book_IdToRemove.';');//index of  ;id;   
$tmpBegining= substr($BooksWanted,0,$len+1);
$tmpEnding= substr($BooksWanted,$len+2+$BookIdlen);
$_SESSION['requestedBooks']=$tmpBegining.''.$tmpEnding;
 
For some reason firefox is making a mess out of the sessions, when I add a book it adds it with two semicolons eg: ;;3;;5;;22;;15;;
I tried as you suggested to replace all accurences of double ';;' to a single one ';' by

Code: Select all

 
$BooksWanted= preg_replace("/(;;)/i",';', $BooksWanted);
Both before displaying the books and before removing an id, but now it displays fine until I remove an id, then it's a mess again.

(Again, this happens only in firefox, -Internet Explorer and google chrome work fine).
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: mysql_result error in Firefox browser

Post by Eric! »

I'm still a bit confused. Are these bookID's submitted via $_GET to the same page? When the string is built does it work like this: one ID + submit, next ID + submit....? So for each submission ID your $_get=;5 then the next should be $_get=;5;10... right? (With the trailing semicolon added after the $_get value.)

Could it be you're getting double submissions from the form so blank + submit, 5 + submit. Thus $_get=;;5 then $_get=;;5;;10 ?

Looking at your code the only way an extra ; could be getting in there is if Firefox decides to add them, which I don't think is happening.

I also don't get this part:
Sosh wrote:Now I let the user delete a requested ID- removing from the session the id and the semicolon after it.

Code: Select all

$BookIdlen=strlen($Book_IdToRemove); //amount of digits in the id
$len= strpos($BooksWanted ,';'.$Book_IdToRemove.';');//index of ;id;
$tmpBegining= substr($BooksWanted,0,$len+1);
$tmpEnding= substr($BooksWanted,$len+2+$BookIdlen);
$_SESSION['requestedBooks']=$tmpBegining.''.$tmpEnding;
Both before displaying the books and before removing an id, but now it displays fine until I remove an id, then it's a mess again.
What does it look like before and after you've removed an ID? If the string is clean ";5;10;15;25;30;" and you remove "10" for example this code could work fine and you get ";5;15;20;25;30;" However if $BookIdToRemove is ";10" then it will be screwed up again.
and look like ";5;;15;20;25;"
Sosh
Forum Newbie
Posts: 5
Joined: Mon Sep 07, 2009 8:04 am

Re: mysql_result error in Firefox browser

Post by Sosh »

sorry, I have a displayAll page, Add page, displaySelected page, and a Remove page.

I display all with a select button for each book on the displayAll page, when a book is selected they go to the Add page that adds the id and then redirects them back to the displayAll page.

The Add page starts with

Code: Select all

if($_SESSION['requestedBooks'] == '' )
        $_SESSION['requestedBooks'] =';';
then

Code: Select all

$tmp=$_SESSION['requestedBooks'].$_GET['BookId'].';';
$_SESSION['requestedBooks']=$tmp;
First $_SESSION['requestedBooks']=';'
For each submission ID it goes like this:
$_GET['BookId']=5 $_SESSION['requestedBooks']=;5;
$_GET['BookId']=10 $_SESSION['requestedBooks']=;5;10;
$_GET['BookId']=23 $_SESSION['requestedBooks']=;5;10;23;

When I remove 10 I remove 10; and have: $_SESSION['requestedBooks']=;5;23;
Could it be you're getting double submissions from the form so blank + submit, 5 + submit. Thus $_get=;;5 then $_get=;;5;;10 ?
Yes!! that's what's happening. I disabled the button once the id is added but apparently firefox is sending a blank

I added before adding/removing

Code: Select all

    if($_GET['BookId']!='')
this helps, but now I need to repress the button when firefox decides not to send the ID.. :(
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: mysql_result error in Firefox browser

Post by Eric! »

When firefox does the double submission, why don't you experiment with the value of your $_POST['submit_button'] from your form. Check to see that the value is what you expect it to be before passing the data along to the PHP page. Perhaps during one of the bogus double submissions the button value won't be set and you can just filter that event out.

Hopefully this makes sense to you. I can explain it via an example if you want.
Sosh
Forum Newbie
Posts: 5
Joined: Mon Sep 07, 2009 8:04 am

Re: mysql_result error in Firefox browser

Post by Sosh »

I don't quite understand, I'm sending the information to be added on another page,

Code: Select all

print '<form action="add.php" method=post">';
//....
print '<input name="'.$Book_Id.'" type="submit" value="Select" onclick="window.location=\'add.php?sbj='.$sbj.'&setBy='.$setBy.'&BookId='.$Book_Id.'\'; this.disabled=true;"/>';
//....
print '</form>';
it goes to the other page every submission but with empty values,
Eric! wrote: Hopefully this makes sense to you. I can explain it via an example if you want.
I would be grateful if you do.
Post Reply