0

Friends please help me out here

My Config File config.php

**<?php
define('DB_SERVER', 'localhost'); // Mysql hostname, usually localhost
define('DB_USERNAME', 'gani'); // Mysql username
define('DB_PASSWORD', 'gani'); // Mysql password
define('DB_DATABASE', 'gani'); // Mysql database name
$connection = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die(mysql_error());
$database = mysql_select_db(DB_DATABASE) or die(mysql_error());
?>

My php file

**function instcount($updateid,$position)
{
  include "config.php";
  global $connection;

  $sel=mysql_query("select * from register where regid='$updateid'");
  $getcount=mysql_num_rows($sel);
  $rowss=mysql_fetch_array($sel);
  //print_r($rowss);
  if($getcount==0)
  {
    $insert=mysql_query("insert into register (regid,dtentered) values ('$updateid',CURDATE())");
  }
  else
  {
    $update=mysql_query("update dailycount set net=100 where regid='$updateid'");
  }

  $selw=mysql_query("select sid,position from register where regid='$updateid'");
  $rowd=mysql_fetch_array($selw);
  if($rowd['sid']!='admin' && $rowd['sid']!="")
  {
    instcount($rowd['sid'],$rowd['position']);
  }
  //return 0;
  mysql_close($connection);
}

$updated="10000";
$upfun=instcount($updated,$position);**

When i run above script I am getting below error

Warning: mysql_close(): 4 is not a valid MySQL-Link resource

Any one please help me out....

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
  • 2
    Please don't use `mysql_*` functions. It removed in PHP 7. Use `mysqli_*` or `PDO` instead. – Thomas Rollet Feb 03 '16 at 14:36
  • 1
    Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Feb 03 '16 at 14:36
  • but other scripts are running with out any issues in my server please help me out... – user3049432 Feb 03 '16 at 14:38
  • 1
    What is `$connection` when the error happens? Why are you making it `global` in the first place? That implies something else is using it, so what else is using it and potentially modifying it? (Also, the standard advice that you're using deprecated functionality which is no longer supported and you have SQL injection vulnerabilities.) – David Feb 03 '16 at 14:42
  • Comment taken from an answer, which should belong here in comments: *"The code looks ok, are you sure you are not defining a new variabile somewhere else as $connection?"* – Funk Forty Niner Feb 03 '16 at 14:45
  • David $connection is mysql connection variable – user3049432 Feb 03 '16 at 14:47
  • I realise this is a bit of a cop-out, but why close the connection yourself. PHP will do it for you automatically when the script completes – RiggsFolly Feb 03 '16 at 15:22

1 Answers1

2

The problem is the

global $connection;

line of the code. You include the file within the function and according to php manual on include:

When a file is included, the code it contains inherits the variable scope of the line on which the include occurs.

This means, that the $connection variable becomes a local variable, since the include is within the function.

Either move the include out of the function or delete the global $connection; line from your code.

Shadow
  • 33,525
  • 10
  • 51
  • 64