Ayuda codigo php

kanikase Seguir

Préstamo
Ómicron
Programador
Verificación en dos pasos activada
Verificado por Whatsapp
¡Ha verificado su Paypal!
Desde
22 Feb 2014
Mensajes
4.716
Necesito ayuda con un codigo php, es para un contador de visitas por entradas, ya lo tengo configurado, pero al subirlo a mi host, y abrir el archivo me sale "Warning: mysql_close() expects parameter 1 to be resource, null given in /home/u292154245/public_html/counter.php on line 69" que esta fallando, este es el codigo que utilizo:

PHP:
<?php

//Created by Anuj Pathania --- anujpathania#gmail.com. Please Feel Free to mail me if you want any help, script has a bug or just wanna say Hi. :D

//This code is released under GPL (General Public License).

/*
You need to create a database and table to store data first, please do so by executing this command in phpmyadmin first but make sure you change database and table name --

 CREATE TABLE `YOUR-COUNTER-DATABASE`.`YOUR-COUNTER-TABLE` (
`SN` BIGINT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR( 1000 ) NOT NULL ,
`hit` BIGINT NOT NULL DEFAULT '1',
PRIMARY KEY ( `SN` )
) ENGINE = MYISAM 

*/
	Header("content-type: application/x-javascript");
	$url = getenv("HTTP_REFERER");
	$url = str_replace ("http://",'',$url);
	$url = str_replace ("www.",'',$url);
    if ($url != "")
		{
			$username = "xxxxxxxx_xxxxx"; // Enter Your Username and Password Here
			$password = 'xxxxxxxxx'; // Enter Your Username and Password Here
			$database = 'xxxxxxxxx_xxxxx';  //Enter Your Database Name Here
			$table = "counter"; //Enter Your Counter Table Name Here
			
			
			$link = mysql_connect('localhost', $username, $password); 
			if (!$link) 
			{
    			die('Could not connect: ' . mysql_error());
			}
			$db_selected = mysql_select_db($database, $link); 
			if (!$db_selected) 
			{
    			die ('Cann\'t select database : ' . mysql_error());
			}
			$query = "Select hit from $table where name = '$url'"; 
			$result = mysql_query($query);
			if (!$result) 
			{
    			die('Invalid query: ' . mysql_error());
			}
			if (mysql_affected_rows()==0)
			{
				$query = "Insert into counter (name) values ('$url')";
				$result = mysql_query($query);
				echo "document.write('1');";
				if (!$result) 
				{
    				die('Invalid query: ' . mysql_error());
				}
			}
			else
			{
				$hitcount = mysql_result($result, 0);
				$hitcount++;
				echo "document.write('$hitcount');";
				$query = "Update counter set hit = $hitcount where name = '$url'";
				$result = mysql_query($query);
				if (!$result) 
				{
    				die('Invalid query: ' . mysql_error());
				}
			}
		}
		mysql_close($link);
?>

Lo que me dice que esta mal es esto: mysql_close($link);

Y aqui esta el archivo: http://toddescargas.com/counter.php
 

Cicklow

Admin
Dseda
Verificado
Verificación en dos pasos activada
Verificado por Whatsapp
¡Ha verificado su Paypal!
Suscripción a IA
Desde
30 May 2011
Mensajes
1.101
PHP:
<?php

//Created by Anuj Pathania --- anujpathania#gmail.com. Please Feel Free to mail me if you want any help, script has a bug or just wanna say Hi. :D

//This code is released under GPL (General Public License).

/*
You need to create a database and table to store data first, please do so by executing this command in phpmyadmin first but make sure you change database and table name --

 CREATE TABLE `YOUR-COUNTER-DATABASE`.`YOUR-COUNTER-TABLE` (
`SN` BIGINT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR( 1000 ) NOT NULL ,
`hit` BIGINT NOT NULL DEFAULT '1',
PRIMARY KEY ( `SN` )
) ENGINE = MYISAM 

*/
	Header("content-type: application/x-javascript");
	$url = getenv("HTTP_REFERER");
	$url = str_replace ("http://",'',$url);
	$url = str_replace ("www.",'',$url);
    if ($url != "")
		{
			$username = "xxxxxxxx_xxxxx"; // Enter Your Username and Password Here
			$password = 'xxxxxxxxx'; // Enter Your Username and Password Here
			$database = 'xxxxxxxxx_xxxxx';  //Enter Your Database Name Here
			$table = "counter"; //Enter Your Counter Table Name Here
			
			
			$link = mysql_connect('localhost', $username, $password); 
			if (!$link) 
			{
    			die('Could not connect: ' . mysql_error());
			}
			$db_selected = mysql_select_db($database, $link); 
			if (!$db_selected) 
			{
    			die ('Cann\'t select database : ' . mysql_error());
			}
			$query = "Select hit from $table where name = '$url'"; 
			$result = mysql_query($query);
			if (!$result) 
			{
    			die('Invalid query: ' . mysql_error());
			}
			if (mysql_affected_rows()==0)
			{
				$query = "Insert into counter (name) values ('$url')";
				$result = mysql_query($query);
				echo "document.write('1');";
				if (!$result) 
				{
    				die('Invalid query: ' . mysql_error());
				}
			}
			else
			{
				$hitcount = mysql_result($result, 0);
				$hitcount++;
				echo "document.write('$hitcount');";
				$query = "Update counter set hit = $hitcount where name = '$url'";
				$result = mysql_query($query);
				if (!$result) 
				{
    				die('Invalid query: ' . mysql_error());
				}
			}
		}
		mysql_close($link);
?>

porke el mysql_close esta por fuera del if...

PHP:
<?php

//Created by Anuj Pathania --- anujpathania#gmail.com. Please Feel Free to mail me if you want any help, script has a bug or just wanna say Hi. :D

//This code is released under GPL (General Public License).

/*
You need to create a database and table to store data first, please do so by executing this command in phpmyadmin first but make sure you change database and table name --

 CREATE TABLE `YOUR-COUNTER-DATABASE`.`YOUR-COUNTER-TABLE` (
`SN` BIGINT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR( 1000 ) NOT NULL ,
`hit` BIGINT NOT NULL DEFAULT '1',
PRIMARY KEY ( `SN` )
) ENGINE = MYISAM 

*/
	Header("content-type: application/x-javascript");
	$url = getenv("HTTP_REFERER");
	$url = str_replace ("http://",'',$url);
	$url = str_replace ("www.",'',$url);
    if ($url != "")
		{
			$username = "xxxxxxxx_xxxxx"; // Enter Your Username and Password Here
			$password = 'xxxxxxxxx'; // Enter Your Username and Password Here
			$database = 'xxxxxxxxx_xxxxx';  //Enter Your Database Name Here
			$table = "counter"; //Enter Your Counter Table Name Here
			
			
			$link = mysql_connect('localhost', $username, $password); 
			if (!$link) 
			{
    			die('Could not connect: ' . mysql_error());
			}
			$db_selected = mysql_select_db($database, $link); 
			if (!$db_selected) 
			{
    			die ('Cann\'t select database : ' . mysql_error());
			}
			$query = "Select hit from $table where name = '$url'"; 
			$result = mysql_query($query);
			if (!$result) 
			{
    			die('Invalid query: ' . mysql_error());
			}
			if (mysql_affected_rows()==0)
			{
				$query = "Insert into counter (name) values ('$url')";
				$result = mysql_query($query);
				echo "document.write('1');";
				if (!$result) 
				{
    				die('Invalid query: ' . mysql_error());
				}
			}
			else
			{
				$hitcount = mysql_result($result, 0);
				$hitcount++;
				echo "document.write('$hitcount');";
				$query = "Update counter set hit = $hitcount where name = '$url'";
				$result = mysql_query($query);
				if (!$result) 
				{
    				die('Invalid query: ' . mysql_error());
				}
			}
			mysql_close($link);
		}
?>
 

kanikase

Préstamo
Ómicron
Programador
Verificación en dos pasos activada
Verificado por Whatsapp
¡Ha verificado su Paypal!
Desde
22 Feb 2014
Mensajes
4.716
porke el mysql_close esta por fuera del if...

PHP:
<?php

//Created by Anuj Pathania --- anujpathania#gmail.com. Please Feel Free to mail me if you want any help, script has a bug or just wanna say Hi. :D

//This code is released under GPL (General Public License).

/*
You need to create a database and table to store data first, please do so by executing this command in phpmyadmin first but make sure you change database and table name --

 CREATE TABLE `YOUR-COUNTER-DATABASE`.`YOUR-COUNTER-TABLE` (
`SN` BIGINT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR( 1000 ) NOT NULL ,
`hit` BIGINT NOT NULL DEFAULT '1',
PRIMARY KEY ( `SN` )
) ENGINE = MYISAM 

*/
	Header("content-type: application/x-javascript");
	$url = getenv("HTTP_REFERER");
	$url = str_replace ("http://",'',$url);
	$url = str_replace ("www.",'',$url);
    if ($url != "")
		{
			$username = "xxxxxxxx_xxxxx"; // Enter Your Username and Password Here
			$password = 'xxxxxxxxx'; // Enter Your Username and Password Here
			$database = 'xxxxxxxxx_xxxxx';  //Enter Your Database Name Here
			$table = "counter"; //Enter Your Counter Table Name Here
			
			
			$link = mysql_connect('localhost', $username, $password); 
			if (!$link) 
			{
    			die('Could not connect: ' . mysql_error());
			}
			$db_selected = mysql_select_db($database, $link); 
			if (!$db_selected) 
			{
    			die ('Cann\'t select database : ' . mysql_error());
			}
			$query = "Select hit from $table where name = '$url'"; 
			$result = mysql_query($query);
			if (!$result) 
			{
    			die('Invalid query: ' . mysql_error());
			}
			if (mysql_affected_rows()==0)
			{
				$query = "Insert into counter (name) values ('$url')";
				$result = mysql_query($query);
				echo "document.write('1');";
				if (!$result) 
				{
    				die('Invalid query: ' . mysql_error());
				}
			}
			else
			{
				$hitcount = mysql_result($result, 0);
				$hitcount++;
				echo "document.write('$hitcount');";
				$query = "Update counter set hit = $hitcount where name = '$url'";
				$result = mysql_query($query);
				if (!$result) 
				{
    				die('Invalid query: ' . mysql_error());
				}
			}
			mysql_close($link);
		}
?>

Ya esta, pero ahora al abrirlo se queda en blanco :C
 

vicram10

Épsilon
Programador
Verificación en dos pasos activada
Verificado por Whatsapp
¡Ha verificado su Paypal!
Desde
8 May 2013
Mensajes
751
Edad
39
mejor saca el link al contador... aparece el nombre de usuario y demas cosas..
deberas hacer pruebas localmente para poder saber el error, pero te aseguras que se conecte a la base de datos y por lo menos te traiga un dato?
 

Vittey

VIP
Pi
Social Media
Verificado
Verificación en dos pasos activada
Verificado por Whatsapp
¡Ha verificado su Paypal!
Verificado por Binance
Suscripción a IA
Desde
13 Abr 2013
Mensajes
5.841
Estaba mirando el codigo,
Ya te lo arregló cicklow :encouragement:
 

Cicklow

Admin
Dseda
Verificado
Verificación en dos pasos activada
Verificado por Whatsapp
¡Ha verificado su Paypal!
Suscripción a IA
Desde
30 May 2011
Mensajes
1.101
Ya esta, pero ahora al abrirlo se queda en blanco :C

porque regresa un javascript... tienes que poner
Insertar CODE, HTML o PHP:
<script src="file.php"></script>
 

Vittey

VIP
Pi
Social Media
Verificado
Verificación en dos pasos activada
Verificado por Whatsapp
¡Ha verificado su Paypal!
Verificado por Binance
Suscripción a IA
Desde
13 Abr 2013
Mensajes
5.841
Estoy de acuerdo con vicram10, es mejor que hagas todas las pruebas en local, trabajas mas rápido y no arriesgas nada en tu web.
O debuggeas o habilitas la vista de Errores, asi solucionas mas rápido.
 

leonbloy

Curioso
¡Usuario con pocos negocios! ¡Utiliza siempre saldo de Forobeta!
Desde
4 Abr 2014
Mensajes
5
Por Dios no uses ese código, te expone a SQL injection porque no sanitiza el referer. Un hacker podría llegar a borrarte la base de datos.
NUNCA armar un query SQL pegando strings dinamicos que provienen "de afuera".
 
Arriba