0

Why won't this query work?!?

Error

Parse error: syntax error, unexpected T_STRING in E:\xampp\htdocs\pf\shop\buy.php on line 5

Example Info For Variables

$character->islots = 20

$chatacter->name = [RE] Tizzle

$e2 = 10

The Function

function increaseSlots($e2) {
 $slots = ($character->islots)+($e2);
 mysql_query('UPDATE `phaos_characters` SET `inventory_slots`="'.$slots.'" WHERE `name`="'.$character->name.'"'); // <-- Line 5
 if (mysql_affected_rows() != 0) {
 echo 'Inventory Size Incresed By '.$e2.' Slots';
 }else{
 echo mysql_error();
 }
}
asked Sep 18, 2012 at 20:18
10
  • mysql_query(...) or die(mysql_error()); outputs what? Commented Sep 18, 2012 at 20:19
  • guess i sued the mysql_error() wrong lol, there is an error there = Parse error: syntax error, unexpected T_STRING in E:\xampp\htdocs\pf\shop\buy.php on line 5 Commented Sep 18, 2012 at 20:21
  • I'm not seeing anything wrong with the syntax though... Commented Sep 18, 2012 at 20:31
  • var_dump('UPDATE phaos_characters SET inventory_slots="'.$slots.'" WHERE name="'.$character->name.'"'); dumps what? Commented Sep 18, 2012 at 20:36
  • because they don+t exists... is this function increaseSlots() in an class? Commented Sep 18, 2012 at 20:39

4 Answers 4

3

Look at the docs: http://php.net/manual/en/function.mysql-num-rows.php

Retrieves the number of rows from a result set. This command is only valid for statements like SELECT or SHOW that return an actual result set. To retrieve the number of rows affected by a INSERT, UPDATE, REPLACE or DELETE query, use mysql_affected_rows().

You need to use mysql_affected_rows() or better yet, PDO or mysqli.

answered Sep 18, 2012 at 20:20
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. New I had something else wrong in there too. Now how about the parse error?!?
2
$slots = ($character->islots)+($e2);

Looks like there is a typo. Try:

$slots = ($character->slots)+($e2);
answered Sep 18, 2012 at 20:20

1 Comment

thats actually my type error on here sorry its supposed to be $character->islots
1

First off you should know that mysql_num_rows only returns a valid result for SELECT or SHOW statements, as stated in the PHP documentation. You can use mysql_affected_rows() for your particular needs.

However, the old PHP MySQL API (that you are using) is being phased out, so I would recommend using mysqli or PDO for your DB connection needs.

While keeping with your requirements, though, you can try to use the following syntax to make sure you receive the MySQL error if it throws one. Your PHP script will stop, but you will see the error.

$query = sprintf('UPDATE `phaos_characters` SET `inventory_slots`=%d WHERE `name`="%s"',$slots,$character->name)
$result = mysql_query($query) or die(mysql_error());

As a final idea, in situations like this it helps to print out your resulting $query and run it manually through something like phpMyAdmin to see what happens.

answered Sep 18, 2012 at 20:41

Comments

0

Bleh... I Found a better way to do it for the time being.. sorry to waste your guys' time...

I just threw the $character object into a variable before processing the function.

function increaseSlots($e2,$charname,$charslots) {
 $slots = $charslots+$e2;
 mysql_query('UPDATE `phaos_characters` SET `inventory_slots`="'.$slots.'" WHERE `name`="'.$charname.'"');
 if (mysql_affected_rows() != 0) {
 echo 'Inventory Size Incresed By '.$e2.' Slots';
 }
}
answered Sep 18, 2012 at 20:49

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.