Can someone help me with this please? I’m playing with a stat tracker, and with the code snippets below I’m trying to get the total result for each page stored in the database. The problem is that I get this warning when I run the script:

Warning: mysql_result() expects parameter 1 to be resource, object given in C:\webroot\tracker\stats.php on line 11
Warning: mysql_result() expects parameter 1 to be resource, object given in C:\webroot\tracker\stats.php on line 12
Warning: mysql_result() expects parameter 1 to be resource, object given in C:\webroot\tracker\stats.php on line 13

I am using PHP 5.3 and MySQL 5.1.36

<?php
 require_once ('mysqli_connect.php');

$query = "SELECT *, count(*) FROM tracker GROUP BY page";
$result = mysqli_query($dbc, $query);

for ($i = 0; $i < mysqli_num_rows($result); $i++)

{
 
$page = mysql_result($result, $i, "page");
 $IP = mysql_result($result, $i, "IP");
 $views = mysql_result($result, $i, "count(*)");

 echo "page: $page ";
 echo "views: $views<br />";

}
?>

Hi Longoro,

eliminate the comma behind * in your sql statement.

*, is wrong sql syntax which leads to empty $result. As $result is invalid, all subsequent sql functions using it will fail

Cheers

Toni

Thanks antonkejr

but now, after I removed the comma, I get this warning:

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\webroot\tracker\stats.php on line 7

Hi Longoro,

that points to an unsuccessfull db connection.

In principle this message means, that mysqli_query returns false and NOT a result set. Put var_dump($result) behind the call and you will see..

I assume you have something like

$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

in your include. Right ? If not, this might be the problem.


Cheers

Toni

I don’t think it is the db connection. I tried this in the same script:

$query = "SELECT * FROM tracker GROUP BY page";
$result = mysqli_query ($dbc, $query);
$views = mysqli_num_rows($result);
echo $views. " page.";

and that works fine, I get the result from db

Ok. Then the connection is ok. Well I'm guessing ;-) ..

Did you try the var_dump($result) behind query ? What does it say ?

Which exact version of php are you using. 5.3.0 is quite buggy as I had to experience ...

Sorry, I have soon to go. It's far after midnight here in germany ;-)

Cheers

Toni

Oh, I'm too stupid tonight ;-)

$query = "SELECT *, count(*) FROM tracker GROUP BY page";

was correct. The comma has to be there. Pardon me ...

Did you try this statement in phpMyAdmin ? What does it say ?

Cheers

Toni

No problem :-), I was trying the SQL command in PhpMyAdmin and it gave me error (without comma) and was just away to post it :-). Anyway, I'm using PHP php 5.3.0.

Well reading your initial code again, I think your problem is the mix between mysqli and mysql. I just realized that now.

Your are using mysqli_query but then you are trying to get the results by mysql_result. If using mysqli you have to use a different set of functions/methods. Please look here:
http://de.php.net/manual/de/mysqli-result.num-rows.php
(it's in english. don't worry)

So you have to use either the one or the other set and don't mix them.

Hope this will help us out of this ;-)

Cheers

Toni

Ok i do that. Thanks for helping

You're welcome :-).

Good luck and good night :-)

Toni

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.