Board index   FAQ   Search  
Register  Login
Board index PHP PHP General

mysql_num_rows error

General discussions related to php

Moderators: macek, egami, gesf

mysql_num_rows error

Postby Pythonmonty » Fri Jun 06, 2014 1:18 pm

Hello

I recieve the an error saying that mysql_num_rows is suppose to be a resource but a boolean is given. I am trying to create a hit counter (through a database) by following one of bucky's tutorials on this, everthing works fine apart from this error.

The code is as follows:

<?php

require 'connect.php';

$user_ip = $_SERVER['REMOTE_ADDR'];

function ip_exists($ip){
global $user_ip;
$query = "SELECT ip FROM hits_ip WHERE ip = $user_ip;";
$query_run = mysql_query($query);
$query_num_rows = mysql_num_rows($query_run);
if ($query_num_rows==0){
return false;
}else if ($query_num_rows==1){
return true;
}

}

function update_unique_count(){
$query = "SELECT unique_count FROM hits_count";
if ($query_run = mysql_query($query)){
$uniqueCount = mysql_result($query_run, 0, 'unique_count');
$unique = $uniqueCount + 1;

$query_update = "UPDATE hits_count SET unique_count = $unique";
$query_update_run = mysql_query($query_update);
}
}

function ip_add($ip){
$query = "INSERT INTO hits_ip VALUES ('$ip')";
$query_run = mysql_query($query);
}

if (!ip_exists($user_ip)){
update_unique_count();
ip_add($user_ip);
}
?>

Any help is appreciated

Kind regards
Pythonmonty
New php-forum User
New php-forum User
 
Posts: 2
Joined: Fri Jun 06, 2014 1:10 pm

Re: mysql_num_rows error

Postby peterachutha » Fri Jun 06, 2014 3:35 pm

I'm new to mysql but I noticed that if I used

$query = "SELECT ip FROM hits_ip WHERE ip = $user_ip;";

the code did not work but if I had used

$query = "SELECT ip FROM hits_ip WHERE ip = '$user_ip' ;";

or in some cases

$query = "SELECT ip FROM hits_ip WHERE ip = '".$user_ip.'" ;";

the code worked. I do not know why, as yet. ... possiblythat the php value is transferred into the query string.
peterachutha
New php-forum User
New php-forum User
 
Posts: 10
Joined: Sat May 31, 2014 10:35 pm

Re: mysql_num_rows error

Postby Pythonmonty » Sat Jun 07, 2014 7:53 am

I checked the coding at various points. The value does increment however the mysql_num_rows does not work. It is suppose to return a boolean value and then based on this value it should increment the counter however it skips this part and adds the value which is why it is not functioning properly.
Pythonmonty
New php-forum User
New php-forum User
 
Posts: 2
Joined: Fri Jun 06, 2014 1:10 pm

Re: mysql_num_rows error

Postby iconofsin » Tue Jun 10, 2014 6:11 pm

$query_num_rows = mysql_num_rows($query_run);
if ($query_num_rows==0){
return false;
}else if ($query_num_rows==1){
return true;
}

That code only handles situations in which mysql_num_rows is equal to 0 or 1
most of the time you will ether get 0 (due to an error) or more than 1 due to a successful query.
if mysql_num_rows returns any other value nothing will happen.

try this instead

$query_num_rows = mysql_num_rows($query_run);
if ($query_num_rows==0){
return false;
}else if ($query_num_rows>=1){
return true;
}
iconofsin
New php-forum User
New php-forum User
 
Posts: 3
Joined: Thu Jun 05, 2014 2:30 pm


Return to PHP General

Who is online

Users browsing this forum: No registered users and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.