1

New to PHP and overwhelmed by all the different solutions to similar problems. I don't know if I have a coding problem, a multiple query problem, or both/more.

In one php file I am opening a connection, running a query, and then on success counting the number of times that entry appears in the database... or at least attempting to.

// $team1, $team2 and $page come in through _POST up here...

$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);

// build long query at this point...

$result = mysqli_query($connection, $query);

//I was successful getting it into the database, now I want to count how many times each entry appears.
if ($result) {
        $team1result = mysqli_query($connection,"SELECT * FROM {$page} WHERE 'vote' = {$team1}") ;
        $team1row = mysqli_fetch_row($team1result);
        $team1count = $team1row[0];

        $team2result = mysqli_query($connection,"SELECT * FROM {$page} WHERE 'vote' = {$team2}") ;
        $team2row = mysqli_fetch_row($team2result);
        $team2count = $team2row[0];

        echo $team1count . " and " . $team2count;
}

I'm able to insert into the database just fine but then my console.log lights up with...

Warning: mysqli_fetch_row() expects parameter 1 to be mysqli_result, boolean given in...
Warning: mysqli_fetch_row() expects parameter 1 to be mysqli_result, boolean given in ...

Thanks for all the help tonight.

SOLUTION (Thanks to wishchaser):

if ($result) {
    $team1rows = mysqli_num_rows(mysqli_query($connection,"SELECT * FROM $page WHERE vote = '$team1'"));
    $team2rows = mysqli_num_rows(mysqli_query($connection,"SELECT * FROM $page WHERE vote = '$team2'"));
    echo $team1 . " : " . $team1rows . "   |   ". $team2 . " : ". $team2rows;
}
Layne
  • 642
  • 1
  • 13
  • 32
  • What about `SELECT COUNT(*) FROM` – sashkello Mar 12 '14 at 04:32
  • 2
    Quotes should only be used around strings and not table names or columns names Put a ` around table names and column names. Also use http://us3.php.net/mysqli_error to error in sql. – Joshua Bixler Mar 12 '14 at 04:34
  • 1
    You say you're _counting the number of times that entry appears in the database_. I don't see any counting in your code. You're just fetching one row of results from each query. – Barmar Mar 12 '14 at 04:34
  • possible duplicate of [mysql\_fetch\_array() expects parameter 1 to be resource, boolean given in select](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-boolean-given-in-select) – John Conde Mar 18 '14 at 17:38

2 Answers2

1

There were no resulting rows for queries $team1result and $team2result. That is why you are getting this error.

use a if statement to check this

if($team1result)
$team1row = mysqli_fetch_row($team1result);

if($team2result)
$team2row = mysqli_fetch_row($team1result);

You will not get the errors.

And for counting the number of rows that a query result, use the folowing

$rows=mysqli_num_rows(mysqli_query($query));    

and a good practice of finding the mistake in your query statement would be to echo it.

in this case

echo "SELECT * FROM $page WHERE vote = '$team1'";
echo "SELECT * FROM $page WHERE vote = '$team2'";

check if the echoed query has no mistakes(like an undefined variable).

wishchaser
  • 628
  • 2
  • 7
  • 19
  • $rows = mysqli_num_rows(mysqli_query($connection,"SELECT COUNT(*) FROM {$page} WHERE 'vote' = {$team1}")); //still returns Warnings. – Layne Mar 12 '14 at 05:37
  • check your query by echoing it. It must have problems. Or your database table have no rows matching the condition `'vote' = {$team1}` – wishchaser Mar 12 '14 at 05:42
  • `$rows = mysqli_num_rows(mysqli_query("SELECT * FROM $page WHERE vote = '$team1'"));` please use this – wishchaser Mar 12 '14 at 05:47
  • Whoops, replied to wrong comment... HUZZA! $rows = mysqli_num_rows(mysqli_query($connection,"SELECT * FROM $page WHERE vote = '$team1'")); //WORKS!!! Note: I had to add "$connection to that last line your used. – Layne Mar 12 '14 at 05:55
  • Since you are using only one connection, you need not add the connection parameter. It will work as it is. Glad it worked. – wishchaser Mar 12 '14 at 06:00
  • It wouldn't work without it. I kept getting the warning errors I mentioned above. When I put the $connection in there, it worked beautifully. Regardless, thank you! – Layne Mar 12 '14 at 06:03
0

You can easily count this using num_rows no need to access its index and all, simply use this

echo $team1row = mysqli_num_rows($team1result);

Reference Link

SagarPPanchal
  • 9,839
  • 6
  • 34
  • 62
  • I'm still getting all the "mysqli_fetch_row()" warnings. – Layne Mar 12 '14 at 05:11
  • while using mysqli_num_rows we not have to use mysqli_fetch_row(), remove this because you just wanted to count na ? – SagarPPanchal Mar 12 '14 at 05:30
  • HUZZA! $rows = mysqli_num_rows(mysqli_query($connection,"SELECT * FROM $page WHERE vote = '$team1'")); //WORKS!!! Note: I had to add "$connection to that last line your used. – Layne Mar 12 '14 at 05:51