Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Debug The Php Code
djax
post Sep 20 2005, 04:41 PM
Post #1


Newbie [Level 1]
*

Group: Members
Posts: 17
Joined: 18-September 05
Member No.: 12,022



CODE
if($s == "poed")
 {
 if($c == "relva")
  {
  if($osta <= 4 && $osta > 0)
   {
   $sql = "SELECT * FROM charinf WHERE nimi = $flgin";
   $query = mysql_query($sql);
   while($q = mysql_fetch_array($query))
    {
    $hraha = $q["raha"];
    }
   if($hraha >= $relvahinnad[$osta])
    {
    $sql = "INSERT INTO kott (omanik, aid, arv, kat) VALUES ($flgin , $osta , 1 , 1)";
    mysql_query($sql);
    $jarraha = $hraha - $relvahinnad[$osta];
    $sql = "UPDATE charinf SET raha = $jarraha WHERE nimi = $flgin";
    mysql_query($sql);
    }
   }
  }
 }
Go to the top of the page
 
+Quote Post
dogomchawi
post Sep 20 2005, 06:48 PM
Post #2


Newbie [Level 3]
***

Group: Members
Posts: 42
Joined: 20-September 05
Member No.: 12,091



Without knowing the error that you were encountering I am going to give you my best explanation of what is wrong.

I highlighted the lines with the problems on it below.


CODE

 $sql = "SELECT * FROM charinf WHERE nimi = $flgin";
 $sql = "INSERT INTO kott (omanik, aid, arv, kat) VALUES ($flgin , $osta , 1 , 1)";
 $sql = "UPDATE charinf SET raha = $jarraha WHERE nimi = $flgin";


Explanation:
When you are working with "" in PHP/MySQL you need to remember that it will treat it as a literal string.
What the code lines above are telling the MySQl to do is to compare two columns together - not the values of a variable in one of the columns ( i.e SELECT all from the charinf table where the table column nimi is equal to the table column with the name of the variable of $flgin). This is not what you are looking to do with this code. If you want to use variables in a MySQL statement within PHP you need to put it in single quotes (' ').




Here is my proposed solution::

CODE
if($s == "poed")
 {
 if($c == "relva")
  {
  if($osta <= 4 && $osta > 0)
   {
$sql = "SELECT * FROM charinf WHERE nimi = '$flgin' ";
   $query = mysql_query($sql);
   while($q = mysql_fetch_array($query))
    {
    $hraha = $q["raha"];
    }
   if($hraha >= $relvahinnad[$osta])
    {
    $sql = "INSERT INTO kott (omanik, aid, arv, kat) VALUES ('$flgin' , '$osta' , 1 , 1)";
    mysql_query($sql);
    $jarraha = $hraha - $relvahinnad[$osta];
    $sql = "UPDATE charinf SET raha = '$jarraha' WHERE nimi = '$flgin' ";
    mysql_query($sql);
    }
   }
  }
 }



NOW - assuming that all your other syntax and stuff is right according to how u wrote the rest of tha page - the code above should work. If not then you would still have had that problem with the code anyways.

Notice from snlildude87:
Posts merged
Go to the top of the page
 
+Quote Post
beeseven
post Sep 22 2005, 11:06 PM
Post #3


Privileged Member
*********

Group: Members
Posts: 629
Joined: 26-February 05
Member No.: 3,995



O_o unless they changed it recently (last 2 months or so), then variables are treated as their values only in double quotes, not singles. Ex:
CODE
$x = 9;
echo "$x";
echo "<br>";
echo '$x';

should output
CODE
9
$x
Go to the top of the page
 
+Quote Post
Spectre
post Sep 23 2005, 03:47 PM
Post #4


Privileged Member
*********

Group: Members
Posts: 874
Joined: 30-July 04
Member No.: 246



That's what dogomchawi is saying. The entire query must be enclosed in double quotes, but field values must be enclosed in single quotes within the double quotes (or it can work the other way, provided you specify either constant values or add them outside the quotes). MySQL requires that string-like values be enclosed in quotes to determine delimitation - numeric values are, obviously, only going to contain numbers (and possible a decimal place), whereas strings can contain spaces and any other number of 'special' characters which MySQL may mistake as indicating it should move to the next field assignment.

I hope that makes sense. Oh, and you should -always- use single quotes where possible. Strings contained within single quotes are not checked for variable references etc, so they are processed much more quickly (even if a string contained within double quotes doesn't contain any variable names at all, it is still checked for them as well as other escaped characters, which takes processing power and time).
Go to the top of the page
 
+Quote Post
magiccode9
post Nov 2 2005, 09:03 AM
Post #5


Premium Member
********

Group: Members
Posts: 162
Joined: 1-November 05
From: SATA II
Member No.: 13,683



hi, all,
I have go through all the code above, yes, that should be careful. If not, would produces a long time debug action. furthermore, I checked that the code have only use only single field of the table, but that select all fields of that, this should make the mysql server generate more data and activity, as many programmer experts said, avoid any unneeded data. That will make server service more client and lower the tco.

- hope this help
- Eric
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. How do you test your php code(77)
  2. Php Clock(7)
  3. Change Permission With Php Code(3)
  4. Adapting Html Code Embed To Work On Phpnuke(7)
  5. Dynamic Image / Signature Generator(12)
  6. Wap Source Code Viewer(4)
  7. You Want It, I Can Make It(49)
  8. More Dynamic ?id=browsing With Php (associative Array)(1)
  9. Display The Current Date/time(3)
  10. Use Bb Code On Your Site!(7)
  11. Some Basic Php Code Snippets For All Levels Of Experience(3)
  12. My Code Doesnt Resize Large Images, Please Help.(2)
  13. Php Education Class (first Code)(0)
  14. How To Make A Random 7 Number Code?(2)
  15. Whats Wrong>?(9)
  1. Awesome Source Code Viewer Script(7)
  2. Html Code Tester. Online Script(15)
  3. I Need Some Proof Reading For My Code Please! [resolved](7)
  4. Will This Code Work(5)
  5. Use Rss In Php Code(3)
  6. Php Code(15)
  7. Php Code Needed(5)
  8. Php Code?(12)
  9. Php Code Needed Iii(10)
  10. Php And Mysql Programming(2)
  11. Malicious Code Injection(3)
  12. Php Source Code Unveiled In Browser?(7)
  13. Create Table - Mysql Code - Help(1)


 



- Lo-Fi Version Time is now: 5th September 2008 - 11:49 PM