Jump to content

Recommended Posts

caps because its absolutely rediculous.

i dontunderstand why its not working. there is a column called id and a collumn called to

then there is also. two rows with the field for `to` equal to the number: 1

still doesnt work:

just outputs . this user has no commnets, none. (every single time)

:'(

<?php $testlll = mysql_query("SELECT to FROM cmnts WHERE to = 1");
if(!$testlll){echo "this user has no comments, none.";} else{echo "this user has $testlll comments";} echo $testlll; ?>

 

and of course $testlll (which i renamed trying to fix it -from $test) is equal to, well. NOTHING!!! whats going on here? do i have to change a table name or something?

i didnt try that because i would have to redo the whole table. theres only one column called to and one column called id (so no dupes)

also im not dumb it used to be $testlll = mysql_num_rows(mysql_query thing but that didnt work either.

You could have an error, "to" column is a reservered word

 

mysql_query("SELECT to FROM cmnts WHERE to = 1") or trigger_error("Query Failed: " . mysql_error());

 

Using that will show you what to error is. To fix it either change the column name (I recommend this) or use backticks to surround the column name:

mysql_query("SELECT `to` FROM cmnts WHERE `to` = 1") or trigger_error("Query Failed: " . mysql_error());

 

I would highly recommend getting used to doing or trigger_error as I have shown above as it will save you headache by showing you what the errors are.

Also you might want to try something like:

 

$testlll = mysql_query("SELECT COUNT(*) AS to FROM cmnts");
$testarray = mysql_fetch_row($testlll);
if($testarray[0] == 0) { 
echo "This user has no comments, none."; 
}
else {
echo "This user has " . $testarray[0] . " comments.";
}

If you use Count(), do not use Count(*) use Count(column) as it will be more efficient since it is only counting one column but does essentially the same thing. Doing that will be more efficient, as for your scenario it should be better doing that as CL pointed out.

why is this other way better? i dont understand

 

It all depends on what you want out. If all you want is the number of rows, it is better to just pull the COUNT(column) because it does not return any data just the number of rows. If you want to actually use the data then you would want to pull out the data and do the num_rows. This will make your script more efficient as MySQL does not have to pull out any data.

 

Does that mean it is working then?

 

You have to love double negatives.

 

count isnt used to get values from the database. essentially it does the same thing as mysql_num_rows. If you were to do this:

 

"SELECT name, count(age) as count_age FROM people"

 

you could then access the count in your code by doing:

 

echo $row['count_age'];

 

This would give you the count of the rows with age, but instead of counting all the results you are just counting the age row.

i think i was getting confused on how to output the count. cause usually i use something like $row = mysql_fetch_array($var) for getting data.

but this $row[field] is the row part buildt in or somethin i dont understand where its coming from

AS is a keyword that creates an Alias for a field. It is simply the name that item will then have in the dataset returned. The AS keyword is generally optional and SELECT COUNT(field) the_word can normally be used, but since your new I'd recommend using it as it helps keep things clear.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.