Web Hosting Deals Holiday Logo Design Webcam Chat Website Header Templates Register domain Search Engine Optimisation Web Hosting
Go Back   Talk Mania Forum > Programming / Scripting / Coding

Programming / Scripting / Coding Got a question about : PHP / Perl / Java / JavaScript / CGI Scripts / JavaScript coding. Ask here! We'll get you the answers !

 Image
Buy Sell Downloads

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-17-2007, 03:01 AM
Junior Member
 
Join Date: Mar 2007
Posts: 14
pitbullet is on a distinguished road
How do i execute a SQL Query ?

How do i execute a SQL Query ? In driving me

Reply With Quote
  #2 (permalink)  
Old 03-18-2007, 05:14 AM
Base's Avatar
Senior Member
 
Join Date: Mar 2007
Location: North Yorkshire, UK
Posts: 121
Base is on a distinguished road
Send a message via MSN to Base Send a message via Skype™ to Base
Ok, so by your post i am guessing your using such a tool as command line SQL?

Or your going to use a programming language..

Or you want to literally know how to execute an SQL query.

I will awnser the last one first.


Say you wanted to get all of the information from the table called talk-mania

So in any language that deals with SQL you would have (somewhere in the code)

Code:
SELECT * FROM talk-mania
Ok, so to execute this code in a database, there are a few tools you can use.

One is the baisic command line that you get with any SQL installation let it be mysql or even msSQL you will allways get a command line with it.

But!

Lets say your on a shared host, then you don't have a command line *OH NOES!!!*

Then what you need is an SQL tool, now the most popular tool for such things (its most likey you will have heard of it) is callled PHP MY ADMIN this is a script supprisingly enough written in PHP, that allows you to administer any number of databases, on any number of systems, from one simple tool.

This also has a SQL command line tool, built in

Check it out @ www.phpmyadmin.net

BUT!

Lets say thats not what your after, and lets say you have PHP my admin installed and you can use it, but you want somthing for the end user to see.

SO!

For this you need a programming language.

Now the most popular for SQL querys is PHP, SQL is built in, you don't have to add anything at all..

Well lets take the same example, that you wanted to select everything in the table called talk-mania.

Now if your using PHP, then your going to need to define a few things first.


PHP Code:
<?php
mysql_connect
("localhost""admin""1admin") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());

// Retrieve all the data from the "example" table
$result mysql_query("SELECT * FROM example")
or die(
mysql_error());  

// store the record of the "example" table into $row
$row mysql_fetch_array$result );
// Print out the contents of the entry 

echo "Name: ".$row['name'];
echo 
" Age: ".$row['age'];

?>
Now i don't want to make this a mysql tutorial, as i allready have one coming up that i am going to post soon (making a simple news system, should be ace, watch out for it)

But i will briefly go through this line by line for you.


mysql_connect("localhost", "admin", "1admin") or die(mysql_error());

OK, so we are using the function mysql_connect and now we are going to declare a few things.

"localhost" - This is the ip address of where the server which contains the database is. If its on the same server, then you use localhost.

"admin" - this is the username you connect to the database with.

"1admin" - and this is the password.

or die(mysql_error())

This is a great little thing, lets you display the reason why it failed, bloody usefull if your testing.

mysql_select_db("test") or die(mysql_error());

so we are using the function mysql_select_db which supprisingly enough means "in mysql selcect the database that i am going to define."

"test" this is the name of the database your trying to connect too.

$result = mysql_query("SELECT * FROM example")

So this is the meat really.

mysql_query.

This is what your after i think, this is the PHP command to query a database, you can put any valid command in between these to speach marks and it will query it.


$row = mysql_fetch_array( $result );

Now this is another fantastic thing.

mysql_fetch_array is literally fetching the data, and making it an array (array meaning "an orderly arrangement")

so the contents of $result which in this case is "mysql_query("SELECT * FROM example")" its going to place into a nice order.


echo "Name: ".$row['name'];
echo " Age: ".$row['age'];

OK!

So we are saying echo the following:

Name: (the contents of the variable $row) (within the row called 'name')

So yeah!

I hope that helped..

And if it hasent awnsered your question.

Then next time, be more specific :P


Cheers and hope you learned somthing!


Base
__________________
http://www.pointserv.co.uk/gfx/deal_..._pointserv.png
PointServ.co.uk - One of the UK's cheapest webhosting companies.
Reply With Quote
  #3 (permalink)  
Old 03-18-2007, 05:17 AM
Junior Member
 
Join Date: Mar 2007
Posts: 14
pitbullet is on a distinguished road
10x very much man .. you are a kewl
Reply With Quote
  #4 (permalink)  
Old 03-18-2007, 05:18 AM
Base's Avatar
Senior Member
 
Join Date: Mar 2007
Location: North Yorkshire, UK
Posts: 121
Base is on a distinguished road
Send a message via MSN to Base Send a message via Skype™ to Base
Just for me, was it the PHP example you were looking for?

Cheers

Base

__________________
http://www.pointserv.co.uk/gfx/deal_..._pointserv.png
PointServ.co.uk - One of the UK's cheapest webhosting companies.
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT +2. The time now is 06:31 PM.


Fake ID

Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC8
Forums Copyright © Talk-Mania.com