Jump to content

mysql count rows


uwannadonkey

Recommended Posts

<?php
include('inc/header.php');

echo"--- Food---<br>";


$result=mysql_query('SELECT * FROM food WHERE owner = $user->ID and name = Apple'); 
if (mysql_num_rows($result) >= 1)
{
$apple = mysql_num_rows($result);
echo " Apples($apple)";
}









include('inc/footer.php');
?>

 

thats the code for counting apples, but this comes out:

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/trukki4/public_html/averyel/inventory.php on line 8

Link to comment
https://forums.phpfreaks.com/topic/91683-mysql-count-rows/
Share on other sites

silly me i should of noticed, you are using ' quotes instead of " for your query, you cannot include $variables without concatenating with single quotes, you have 2 options:

 

either:

 

$result=mysql_query("SELECT * FROM food WHERE owner = $user->ID and name = Apple"); 

 

or concatenate:

 

$result=mysql_query('SELECT * FROM food WHERE owner = '.$user->ID.' and name = Apple'); 

Link to comment
https://forums.phpfreaks.com/topic/91683-mysql-count-rows/#findComment-469563
Share on other sites

Unknown column 'Apple' in 'where clause'

 

but there is a column apple..

 

this is a copy of the mysql table:

ID  owner  name  effect  bonus

1 1             Apple 5 energy

2 1             Apple 2 energy

3 1               Apple 5 energy

Link to comment
https://forums.phpfreaks.com/topic/91683-mysql-count-rows/#findComment-469572
Share on other sites

fixed

 

$apple1= Apple;

$result=mysql_query("SELECT * FROM food WHERE owner = $user->ID AND name = 'Apple'")or die(mysql_error()); 

if (mysql_num_rows($result) >= 1)

{

$apple = mysql_num_rows($result);

echo " Apples($apple)";

}

 

apple needed a 'apple' to work

Link to comment
https://forums.phpfreaks.com/topic/91683-mysql-count-rows/#findComment-469583
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.