1

I am trying to do is add value to a user who referred the logged in user after the logged user has opened a certain page

so i made a few changes and im now getting the following error

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource 
in C:\xampp\htdocs\test\goingback_t_play_150.php on line 18

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in  
C:\xampp\htdocs\test\goingback_t_play_150.php on line 20
, 

here is my code

<?php
session_start();
?>
<?php
// Adjust MySQL connection settings...
$mysql_hostname = "localhost";
$mysql_user = "user";
$mysql_password = "";
$mysql_database = "";
$bd = mysql_connect($mysql_hostname,    $mysql_user,$mysql_password) or die("");
mysql_select_db($mysql_database, $bd) or die

$sql="SELECT * FROM users where id={$_SESSION['refcode']}"; 

$val = 2; //#2 : Why do u need the ()?
$result = mysql_query($sql,$bd);
$data=mysql_fetch_assoc($result);
if (mysql_num_rows($result)==1) {
// equate the value to a variable to use outside
// this while loop
 $acc_balance = $data['com_balance'];

$commision = $data['id'];
}
$remainder = $acc_balance + $val;       
$update_query = mysql_query("UPDATE users SET com_balance = '".                 
mysql_real_escape_string($remainder) ."'
WHERE id='refcode'"); 
if ($update_query) {
 print "";
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
Ree
  • 53
  • 2
  • 3
  • 9
  • Check the value of $result with `var_dump($result)`, you may have a problem with the return of your db request – Aelios Sep 26 '12 at 07:49
  • `mysql` is discouraged and will be deprecated in the future. If your project is not live and not too large I'd recommend to use `mysqli` or `pdo` from now on and change everything you've made already. If your project is live and too large to commit those changes immediately, maybe mirror it and change everything (might take a while) and then update the live version with the new version. – Deep Frozen Sep 26 '12 at 07:55

2 Answers2

1

mysql_ usage is discouraged, I recommend you to use mysqli_ instead, more info here.

For your problem, you should check if your variable $_SESSION['refcode'] is correctly defined.

If this variable is correctly set, then there could be an error into your SQL syntax or inside your database.

Try to replace :

$result = mysql_query($sql,$bd);

With this :

$result = mysql_query($sql,$bd) or die(mysql_error($bd));

Your script will output the MySQL error if there are any, since your result variable seems to be undefined.

Community
  • 1
  • 1
Axel Isouard
  • 1,498
  • 1
  • 24
  • 38
1

Looks like there is an error in the database query.

After :

    $result = mysql_query($sql,$bd);

Add :

    if (!$result) {
      die(mysql_error());
    }

Should tell you the problem with the query.

janenz00
  • 3,315
  • 5
  • 28
  • 37