PDA

View Full Version : [TUT] Performing a simple database select query with PHP



Calon
09-12-2008, 10:17 PM
Pretty simple, a lot of people use PHP files too do this,



<?php
mysql_query( "QUERY" );
?>
Here are a few things;

SELECT - Select's from your database
* - Wildcard; grabs everything matching your query critera.
WHERE - Where field name has the result of

So here's an example query, using the three of them;



SELECT * FROM `users` WHERE `id` = '1'
And a bit of PHP..



<?php
// Assume we're already connected to the database ;)

// Querying the database..
$query = mysql_query( "SELECT * FROM `users` WHERE `id` = '1'" );

// While - gets all the results and does what you want to do with them in the while statement, and continously repeats them.
while( $object = mysql_fetch_object( $query ) )
{
echo $object->username; // You can use object or arrays, I'm an object fan. We're grabbing the field name "username"
}
?>
That above, is just a simple select statement, and will repeat any result with the field named "id" which has the result of 1.

MasterFM
09-12-2008, 10:21 PM
Gee, this is a pretty good tutorial :P

Calon
09-12-2008, 10:22 PM
Gee, this is a pretty good tutorial :P
Might help the new coders out! Like you! aha.

MasterFM
09-12-2008, 10:23 PM
OOOooo.
FAILLLLL


Edited by Yoshimitsui (Forum Super Moderator): Please do not make pointless comments.

Source
09-12-2008, 10:25 PM
INDENTATION CALON, INDENTATION!!!!



<?php
//connect here

//perform the query
$query = mysql_query( "SELECT * FROM `users` WHERE `id` = '1'" );

//while loop for echoing out zem all
while( $object = mysql_fetch_array( $query ) )
{
echo $object['username'];
}
?>

Excellent2
09-12-2008, 10:26 PM
Didn't you post this on faildev? I mean codedev.

Edited by Meti (Forum Moderator): Please don't post to cause arguments.

MasterFM
09-12-2008, 10:26 PM
INDENTATION CALON, INDENTATION!!!!



<?php
//connect here

//perform the query
$query = mysql_query( "SELECT * FROM `users` WHERE `id` = '1'" );

//while loop for echoing out zem all
while( $object = mysql_fetch_object( $query ) )
{
echo $object->username;// You can use object or arrays, I'm an object fan. We're grabbing the field name "username"
}
?>


lmaocopters


Edited by Yoshimitsui (Forum Super Moderator): Please do not continue to make pointless comments.

Calon
09-12-2008, 10:26 PM
INDENTATION CALON, INDENTATION!!!!



<?php
//connect here

//perform the query
$query = mysql_query( "SELECT * FROM `users` WHERE `id` = '1'" );

//while loop for echoing out zem all
while( $object = mysql_fetch_object( $query ) )
{
echo $object->username;// You can use object or arrays, I'm an object fan. We're grabbing the field name "username"
}
?>

It was an example, I can't indent in firefox using tab. I would have otherwise. :D



Didn't you post this on faildev? I mean codedev.

Nothing fails worse than you. Yes I did, the more people the tutorial helps the better.

Source
09-12-2008, 10:27 PM
5 spaces, SPACES!

Ok, I'll stop with the over exaggeration now.

Calon
09-12-2008, 10:28 PM
5 spaces, SPACES!

Ok, I'll stop with the over exaggeration now.
Thank you! :D

I never use spaces, even though firefox if I use tab they automatically convert.

Jackboy
09-12-2008, 10:32 PM
Didn't you post this on faildev? I mean codedev.

That made me lol irl.

I think everyone can select info lol, tis not hard

Calon
09-12-2008, 10:34 PM
That made me lol irl.

I think everyone can select info lol, tis not hard
How can it make you "lol irl" - I highly doubt that you have even heard of codedev, and I am not the owner, I suppose you can say I'm an investor. As I am lending them a vBulletin.

Also, this is a tutorial for the new coders, I am a bit offended as you feel the need to flame, I am helping out the new coders.

Excellent2
09-12-2008, 10:37 PM
Nothing fails worse than you. Yes I did, the more people the tutorial helps the better.Okay calon..
N
I
V
A
D
E

Calon
09-12-2008, 10:38 PM
Okay calon..
N
I
V
A
D
E


I hate it when idiots bring up old stuff, Nivade met it's targets, got the userbase I wanted it too get, I did NOT want anything big to come of it whilst I owned it. So I think you should truly get your facts right before you state that I failed.

You are the worlds biggest failure.

MasterFM
09-12-2008, 10:40 PM
I did NOT want anything big to come of it whilst I owned it. So I think you should truly get your facts right before you state that I failed.
I can confirm this, he only realy wanted people in his town using it

Calon
09-12-2008, 10:43 PM
I can confirm this, he only realy wanted people in his town using it
Which I met the targets of, people in my school used to suck up to me begging to be a member of staff on Nivade. And a lot of them terminated their account when they heard I no longer own Nivade, I think I earnt myself some respect from Nivade, and I've learnt a lot from Nivade php wise. So I think Nivade was more than I expected, but I got what I wanted.

Excellent2
09-12-2008, 10:43 PM
I hate it when idiots bring up old stuff, Nivade met it's targets, got the userbase I wanted it too get, I did NOT want anything big to come of it whilst I owned it. So I think you should truly get your facts right before you state that I failed.

You are the worlds biggest failure.I'll let you get back to teaching users how to query a database Calon :)

Calon
09-12-2008, 10:45 PM
I'll let you get back to teaching users how to query a database Calon :)
Sarcasm isn't wanted here. This was made to help out new coders, who want to understand how it works.. Not to see a thread being flamed by an epic failure.

Excellent2
09-12-2008, 10:46 PM
Sarcasm isn't wanted here. This was made to help out new coders, who want to understand how it works.. Not too see a thread being flamed by an epic failure.Yep, we can clearly see who fails Calon :)

Calon
09-12-2008, 10:47 PM
Yep, we can clearly see who fails Calon :)
Typo, I'll gladly put my hands up and admit that I have made a mistake there, unlike you and mistakes, eh? It seems you can't just admit how terribly you fail..

Excellent2
09-12-2008, 10:52 PM
Pretty simple, a lot of people use PHP files too do this,



I'm likely too intergrate the new Nivade that I'm coding for the person I sold Nivade too.
Yep, typos.

Calon
09-12-2008, 10:54 PM
Yep, typos.
Well, maybe you shouldn't expect everything perfect seeing as I have been awake for 2 days resolving important issues in real life.

Meh I can't be bothered arguing with failures.

Excellent2
09-12-2008, 11:00 PM
Everyone needs to start from somewhere Jason, I...

Everyone needs to start from somewhere Jason, I doubt the first thing you ever got your hands on became successful.

I sold it too Habtasia
Yep, two days.

Source
09-12-2008, 11:06 PM
Eljay, just shut up. You're making yourself look like a whining child, which you are to a pretty large extent. I myself moan/rage/go ott alot, but not on public forums where (as seen in this case) the person who doesn't drop it looks like an idiot.

Now move on, to another thread.

Excellent2
09-12-2008, 11:10 PM
Eljay, just shut up. Your making yourself look like a whining child, which you are to a pretty large extent. I myself moan/rage/go ott alot, but not on public forums where (as seen in this case) the person who doesn't drop it looks like an idiot.

Now move on, to another thread.I think you meant you're? And a whining child? I don't whine about anything unless it's wrong, which in this case it is. I'll gladly move onto another thread which deserves one of my posts.

Calon
09-12-2008, 11:11 PM
I think you meant you're? And a whining child? I don't whine about anything unless it's wrong, which in this case it is. I'll gladly move onto another thread which deserves one of my posts.
There's a forum called "Spam" - I think it was especially created for you.

Source
09-12-2008, 11:11 PM
I think you meant you're? And a whining child? I don't whine about anything unless it's wrong, which in this case it is. I'll gladly move onto another thread which deserves one of my posts.

Good good, have a nice day mate.

Anyway, this tut doesn't classify to a tutorial to an extent. Maybe add some more things in, like some basic SQL syntax - such as how to grab things in order etc...

Calon
09-12-2008, 11:13 PM
Good good, have a nice day mate.

Anyway, this tut doesn't classify to a tutorial to an extent. Maybe add some more things in, like some basic SQL syntax - such as how to grab things in order etc...
Thanks, I will create another tutorial explaining the basic MySQL functions, and the syntax of them when I have time, for now I am going to bed.

wazup999
10-12-2008, 12:32 AM
Why do people like to annoy and flame others so much on forums? Well I guess it's easy to act tough when you're hiding in your room and no one knows you.

Now why don't you flamers grow up and move on with your lives. People need tutorials even if they seem simple and you might think that everyone knows this stuff already but not everyone does. When you started coding I guess you knew everything and didn't need tutorials to help you off your feet?

People waste their time flaming others when they could be helping them instead on improving. But I guess it's easier to shoot someone weaker than of helping back back on his feet.

People these days have no respect :/
Waz

Jackboy
10-12-2008, 08:54 AM
How can it make you "lol irl" - I highly doubt that you have even heard of codedev, and I am not the owner, I suppose you can say I'm an investor. As I am lending them a vBulletin.

Also, this is a tutorial for the new coders, I am a bit offended as you feel the need to flame, I am helping out the new coders.


I'll let you get back to teaching users how to query a database Calon :)

Lol kthnx.

Feel free to do a tutorial on javascript for js noobs like me ;)

Blinger1
10-12-2008, 09:06 AM
I was reading this tutorial but i don't get the object line?

Calon
10-12-2008, 11:31 AM
I was reading this tutorial but i don't get the object line?
You can use arrays as I said.

If you wanted to call an object, like you would with an array $array_var[ 'name' ] You do: $object->name;

Meti
10-12-2008, 03:45 PM
Good tut Calon.
Could get closed due to arguments, so make sure keeping it on-topic ;)

+rep IF I can

Calon
10-12-2008, 04:47 PM
I'll let you get back to teaching users how to query a database Calon :)

http://www.habboxforum.com/showthread.php?t=500740&highlight=mysql+excellent

Protege
10-12-2008, 05:02 PM
Dont you need to connect to the database/teach them how to connect to a database b4 doing a query cause I tried ur code and it ended in fatal error :(

Im a noob & i can't read comments soz

Calon
10-12-2008, 05:02 PM
Dont you need to connect to the database/teach them how to connect to a database b4 doing a query cause I tried ur code and it ended in fatal error :(
Performing a query, simple PHP. I'm sure they would have learnt too connect before.

Protege
10-12-2008, 05:04 PM
Okay darlin', I'll reads PHP 4 Dummies thx xxxx

Good tutorial m8 enit

MasterFM
10-12-2008, 05:07 PM
Okay darlin', I'll reads PHP 4 Dummies thx xxxx

Good tutorial m8 enit

Nar matee you suck at bein scouse mate learn the lingo lyk.

Excellent2
10-12-2008, 05:57 PM
http://www.habboxforum.com/showthread.php?t=500740&highlight=mysql+excellentA very long time ago mr calon :)

Calon
10-12-2008, 06:12 PM
A very long time ago mr calon :)
12-07-2008, 01:40 AM
I don't think that being a long time ago.

Excellent2
10-12-2008, 06:39 PM
12-07-2008, 01:40 AM
I don't think that being a long time ago.Well I take it you're no good with maths.

Lets make a simple add here.
7 + 5 = 12.
The month number of December = 12.

I make it that the thread you're reffering to was made 4 months and 29 days ago. In this time I've much improved at PHP and actually learnt to take on comments.

;)

I also won't be posting in here again as I've already got 5 infractions.

If you want some "on topic" comments I'd suggest you make a much better post including how to connect to the database and using a query to get some submit or grab some information from that particular database.

L?KE
10-12-2008, 09:31 PM
Will you phannies stop arguing please :)

Admittedly it's a simple tutorial which could be improved a lot, but what is there is helpful to anyone who needs it and was not posted in malice. Even though Lucas is my life partner, I'm gona have to say that you're wrong here babe. You won't be getting any tonight.

Jackboy
11-12-2008, 05:12 PM
performing simple database select queries are so damn hard!

Im sure glad this tutorial was available.

Calon
11-12-2008, 07:28 PM
performing simple database select queries are so damn hard!

Im sure glad this tutorial was available.
I'm so glad that you're here, otherwise I would have killed myself.. You are such a pro...

not

Jackboy
11-12-2008, 10:04 PM
I'm so glad that you're here, otherwise I would have killed myself.. You are such a pro...

not

No im not pro. I have read this tutorial 5 times over and still don't understand how to select queries.

Heres my code



<?
include "http://google.co.uk/config.php";
$google = mysql_query("SELECT * FROM `googleusers` WHERE `level` = 'admin'");
while ($googleadmins = mysql_fetch_array($google)){
echo "<b>Username:</b> $googleadmins[username]<br>
<b>Password:</b> $googleadmins[password]<br>
<b>Email Address:</b> $googleadmins[email]<br>";

}
?>
WHY DOES IT NOT WORK!

Blinger1
11-12-2008, 10:15 PM
<?
include "http://google.co.uk/config.php";
$google = mysql_query("SELECT * FROM `googleusers` WHERE `level` = 'admin'");
while ($googleadmins = mysql_fetch_array($google)){
echo "<b>Username:</b> $googleadmins[username]<br>
<b>Password:</b> $googleadmins[password]<br>
<b>Email Address:</b> $googleadmins[email]<br>";

}
?>
WHY DOES IT NOT WORK!

hmm... i wonder...:eusa_thin

Calon
11-12-2008, 11:08 PM
No im not pro. I have read this tutorial 5 times over and still don't understand how to select queries.

Heres my code



<?
include "http://google.co.uk/config.php";
$google = mysql_query("SELECT * FROM `googleusers` WHERE `level` = 'admin'");
while ($googleadmins = mysql_fetch_array($google)){
echo "<b>Username:</b> $googleadmins[username]<br>
<b>Password:</b> $googleadmins[password]<br>
<b>Email Address:</b> $googleadmins[email]<br>";

}
?>
WHY DOES IT NOT WORK!
You are one terrible coder..

Short tags, including variables without 'word' . $var .'otherpart';

Jackboy
11-12-2008, 11:09 PM
You are one terrible coder..

Short tags, including variables without 'word' . $var .'otherpart';

Lmao!

Yes because what i put up there is how i code normally.

Im so happy that you understand the concept of sarcasm. You are so smart!

Noob.

Calon
11-12-2008, 11:12 PM
Lmao!

Yes because what i put up there is how i code normally.

Im so happy that you understand the concept of sarcasm. You are so smart!

Noob.
Shall I define "noob" for you, because clearly you don't understand the definition?

Thanks for the private message, I think I shall report it.

Jackboy
11-12-2008, 11:14 PM
Shall I define "noob" for you, because clearly you don't understand the definition?

Thanks for the private message, I think I shall report it.

Go for it? I'm honestly not fussed.

Anyway i will stop arguing with you now as it'll annoy a loads of the members.

Talk tomoz, Cya

Calon
11-12-2008, 11:15 PM
Go for it? I'm honestly not fussed.

Anyway i will stop arguing with you now as it'll annoy a loads of the members.

Talk tomoz, Cya
So continue private messaging me then? I'm giving something to the community, you're flaming people.

You are the forums worst coder, I think if Meti learned PHP he'd probably be a better coder than you.

Excellent2
11-12-2008, 11:36 PM
I'm so glad that you're here, otherwise I would have killed myself.. You are such a pro...

notYou're no better.

Jackboy
12-12-2008, 09:08 AM
So continue private messaging me then? I'm giving something to the community, you're flaming people.

You are the forums worst coder, I think if Meti learned PHP he'd probably be a better coder than you.

Why use Meti as an example? Meti has the potential to be a great PHP coder.

And besides i said i wouldn't reply. I only commented since you brought Meti into it...

Anyway soz i didn't reply last night. Had to go to bed ;)

Calon
12-12-2008, 03:46 PM
Why use Meti as an example? Meti has the potential to be a great PHP coder.

And besides i said i wouldn't reply. I only commented since you brought Meti into it...

Anyway soz i didn't reply last night. Had to go to bed ;)
No he doesn't lol.

He thinks PHP is started by: <?php>

L?KE
12-12-2008, 05:59 PM
Yeah but that's why you learn..
I used to think the <head> went inside the <body>.. (rofl :rolleyes:)

Excellent2
12-12-2008, 06:21 PM
Yeah but that's why you learn..
I used to think the <head> went inside the <body>.. (rofl :rolleyes:)Whats changed? ;)

Protege
12-12-2008, 06:32 PM
Lul i was going to say that ;(

L?KE
12-12-2008, 07:08 PM
Rofl lucas.

The guy who always nags me for help on msn.. :rolleyes:

Excellent2
12-12-2008, 07:21 PM
Rofl lucas.

The guy who always nags me for help on msn.. :rolleyes:Rofl luke.
And the answer I get is "having a tank" as you have no girlfriend to do it for you ;)

Haha.

Calon
12-12-2008, 07:27 PM
Rofl luke.
And the answer I get is "having a tank" as you have no girlfriend to do it for you ;)

Haha.
Where did this thread turn into a discussion about pets?

Excellent2
12-12-2008, 07:37 PM
Where did this thread turn into a discussion about pets?It didn't lol?

Calon
12-12-2008, 07:45 PM
It didn't lol?
Misread post.

Want to hide these adverts? Register an account for free!