SQL Query - A little help please.
Hi, I am using PHP for this, so tell me if this is possible...
I want to query the database, and "echo" number of rows that contain a "1".
The complex bit, that I dont understand, is how to do this, but only query 1 column.
Lets say I have this structure:
localhost > myDB > users >
uname | pword | email | is_online | last_ping | last_ip | banned | admin | id
How could I run a query on "is_online" alone?
This column only displays it's data in 1/0, and I only want to return results with a "1"
I hope this is clear. I am already running a query somewhere alse on this database, the code goes like this:
<?php
$result = mysql_query(\"SELECT * FROM table_name\");
$num_rows = mysql_num_rows($result);
echo \"$num_rows\n\";
?>
I would like to keep this layout.
Any information would be greatly appreciated.
Thanks in advance.
All the best news here: https://newsbotnet.com
teammatt3 posted this at 16:26 — 6th February 2008.
He has: 2,102 posts
Joined: Sep 2003
You modify your query to something like
"SELECT * FROM User WHERE is_online = 1"
The WHERE clause filters all the rows out that don't have a 1 in the is_online column.
Another way to do it is
"SELECT COUNT(*) AS Cnt FROM User WHERE is_online = 1"
Then do a $row = mysql_fetch_assoc($result) and echo out $row['Cnt']
I heard it's faster to make MySQL do your counting.
PS You also need to change echo "$num_rowsn"; to "$num_rows";
Greg K posted this at 16:43 — 6th February 2008.
He has: 2,145 posts
Joined: Nov 2003
It is a good general rule to have the DB server do as much calculating and figuring as possible and then pass back the final results to the "client" (in this case, PHP)
This is even more so when you have a separate DB server (physical box) from the web server. There is less recourses to passing back and forth, and if done right, the SQL server should be fine tuned for the DB server, so should calculate faster than on a "general" server set up fro multiple operations.
But as you are already have a query else where (to possible list the online users), the format you have is correct with the mentioned addition of the WHERE clause.
-Greg
DarkLight posted this at 16:46 — 6th February 2008.
He has: 287 posts
Joined: Oct 2007
Concidence. I had just figured that out right now. I was just returning to TWF to delete this post. Thanks Teammatt3.
Yeah, our codes are identical. Except I went with
<?php
\"SELECT * FROM User WHERE is_online = 1\"
?>
Thanks for your help, and sorry for wastin anyones time (again).
It seems to me, that I try to do something, then after a few days, I give up, and post on TWF, then guaranteed, I will figure it out when I have already posted the darn question!!!
Cheers anyways.
All the best news here: https://newsbotnet.com
Greg K posted this at 16:55 — 6th February 2008.
He has: 2,145 posts
Joined: Nov 2003
All questions are worth asking. Never delete a question even if you find the answer, instead post a second message saying you solved it, and let people know how you solved it.
This serves two purposes. 1. Sometimes you may find something that works, but someone else knows a more efficient way of doing it. 2. Someone else may encounter the same issue and do a search and find your post, and then solve their problem too.
-Greg
DarkLight posted this at 17:11 — 6th February 2008.
He has: 287 posts
Joined: Oct 2007
Yeah, thats true
I am currently making all the queries from PHP, what significant difference would there be if I let the database do all the work?
All the best news here: https://newsbotnet.com
JeevesBond posted this at 02:21 — 7th February 2008.
He has: 3,956 posts
Joined: Jun 2002
Greg K is saying that the general rule is to make the database do as much of the hard work as possible. Writing the SQL in PHP and running the query using mysql_query() is the right way to do this, that's not the hard part.
Where a lot of programmers go wrong is to do a basic SQL query, then loop through the results using an if statement to get the records they want. It's usually wrong to use an
if
statement instead of aWHERE
clause in your SQL.a Padded Cell our articles site!
DarkLight posted this at 18:08 — 7th February 2008.
He has: 287 posts
Joined: Oct 2007
OK... But how would I go about this?
This is my current PHP code:
<?php
$result = mysql_query("SELECT * FROM ajaxim_chats");
$num_rows = mysql_num_rows($result);
echo "<strong>Public Chatrooms:</strong><br />$num_rows\n";
?>
<?php
$result = mysql_query("SELECT * FROM ajaxim_users WHERE is_online = 1");
$num_rows = mysql_num_rows($result);
echo "<strong>Users Online:</strong><br />$num_rows\n";
?>
How could this be initiated on the SQL Server?
Thanks in Advance.
All the best news here: https://newsbotnet.com
JeevesBond posted this at 02:08 — 8th February 2008.
He has: 3,956 posts
Joined: Jun 2002
It already is. mysql_query() will send the query up to the database server, the database server will process it and return the result to PHP.
You could optimise that code a bit by using a SQL
COUNT()
instead of themysql_num_rows()
PHP function:<?php
$chatrooms = mysql_result(mysql_query('SELECT COUNT(*) FROM ajaxim_chats'));
print '<strong>Public chatrooms:</strong><br />'. $chatrooms;
$users_online = mysql_result(mysql_query('SELECT COUNT(*) FROM ajaxim_users WHERE is_online=1'));
print '<strong>Users online:</strong><br />'. $users_online;
?>
mysql_result
is a good function to use here as the SQL is only ever going to return one value, no multiple rows or any of that malarkey.a Padded Cell our articles site!
DarkLight posted this at 09:30 — 8th February 2008.
He has: 287 posts
Joined: Oct 2007
Ahh, OK, cheers.
I have just closed my website because Drupal aint right for me. I am going back to e107. It has more support, more modules, and Less bugs.
Cheers for the info. I can still use this code on the new platform.
Thank you to everyone who has helped on this topic.
All the best news here: https://newsbotnet.com
JeevesBond posted this at 10:07 — 8th February 2008.
He has: 3,956 posts
Joined: Jun 2002
Not sure about this. Drupal has ~2,000 modules, e107 has 187 (at time of writing). Don't know about the support and bugs, never really had a problem personally. There are plenty of reasons to not like Drupal, but lack of modules isn't one of them. Maybe it's that Drupal lacked a particular module you wanted that e107 has got? That's fair enough.
Just use whatever you want to use. None of these projects are really competing with each other, they're all Free software, run and maintained by their respective communities. Free software developers usually think a little like this: 'It's cool if my software works for other people, but what's important is that it works for me.'
What happens then is people find the work and discover it works well for them as well as the person whom originally wrote it. They usually find the software lacks some functionality, or has some bug or other. As it almost works for them, they submit a patch to add the functionality or fix the bug. This is how a community grows around a project.
So saying: 'I'm moving to project X, Drupal doesn't do what I need.' Is rather pointless, as it doesn't help the project, or fix any bugs, no-one is going to be interested. Really, just use whatever the heck you like. e107 probably does some stuff that Drupal doesn't, or works in a way that better fits you personally. That's great! Most of all though, I hope you can make a contribution to the e107 community.
a Padded Cell our articles site!
DarkLight posted this at 17:51 — 8th February 2008.
He has: 287 posts
Joined: Oct 2007
I had just uploaded to Drupal, a module that integrates ajaxim into Drupal. I noticed they lack in good Chat services.
No, the main reason I am switching, like you said, because they have something Drupal does not.
No don't get me wrong, I still think Drupal is the best maintained, and easily the best scripted, but for my particular use, e107 would be better.
Cheers for the info.
All the best news here: https://newsbotnet.com
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.