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;
}
4
  • What about SELECT COUNT(*) FROM Commented Mar 12, 2014 at 4: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 us3.php.net/mysqli_error to error in sql. Commented Mar 12, 2014 at 4: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. Commented Mar 12, 2014 at 4:34
  • possible duplicate of mysql_fetch_array() expects parameter 1 to be resource, boolean given in select Commented Mar 18, 2014 at 17:38

2 Answers 2

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).

Sign up to request clarification or add additional context in comments.

6 Comments

$rows = mysqli_num_rows(mysqli_query($connection,"SELECT COUNT(*) FROM {$page} WHERE 'vote' = {$team1}")); //still returns Warnings.
check your query by echoing it. It must have problems. Or your database table have no rows matching the condition 'vote' = {$team1}
$rows = mysqli_num_rows(mysqli_query("SELECT * FROM $page WHERE vote = '$team1'")); please use this
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.
Since you are using only one connection, you need not add the connection parameter. It will work as it is. Glad it worked.
|
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

3 Comments

I'm still getting all the "mysqli_fetch_row()" warnings.
while using mysqli_num_rows we not have to use mysqli_fetch_row(), remove this because you just wanted to count na ?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.