How do I set a DB value as a variable in PHP?
Moderator: General Moderators
-
danarashad
- Forum Newbie
- Posts: 8
- Joined: Thu Aug 23, 2007 8:06 am
How do I set a DB value as a variable in PHP?
how do I set a DB value as a variable in PHP.
I am a coldfusion coder, but i am trying to learn PHP. in coldfusion
i'd write <cfset variable_name=queryname.column_name>
how do you do this in php. thanks for your help
Edit: Modified subject line to something more appropriate.
Please be sure to read the thread appropriately titled Before Post Read: General Posting Guidelines at the top of the forum before posting.
Following these guidelines really do help you get your questions answered faster.
I am a coldfusion coder, but i am trying to learn PHP. in coldfusion
i'd write <cfset variable_name=queryname.column_name>
how do you do this in php. thanks for your help
Edit: Modified subject line to something more appropriate.
Please be sure to read the thread appropriately titled Before Post Read: General Posting Guidelines at the top of the forum before posting.
Following these guidelines really do help you get your questions answered faster.
- aceconcepts
- DevNet Resident
- Posts: 1424
- Joined: Mon Feb 06, 2006 11:26 am
- Location: London
Hi,
Usually I'd declare the db in a seperate file and include it.
i.e.
So, i guess you might be able to assign a variable with this value or similar.
Usually I'd declare the db in a seperate file and include it.
i.e.
Code: Select all
mysql_select_db ("db_name");-
Steve Mellor
- Forum Commoner
- Posts: 49
- Joined: Thu Aug 02, 2007 8:18 am
Well, first of all you have to connect to the database. Now this depends somewhat on which database you're connecting to. I am using MYSQL in this example.
It's a good idea to store this in a separate file but for the time being lets assume it's all in the same script. Next we select the database using PHP's native MySQL commands.
You can see what I'm doing here. $database is the name of the database, taken from the first code snippet. $myConnection is the actual connection to your MySQL server.
The next thing you need to do is write a statement. This is in MySQLs own script so it's not PHP code at all. I'm going to put it in a variable called query and I will be selecting all records from a table called monkey.
Now, that's very basic. There are tones of things you can do to extract data from your database and that's the real power behind PHP/MySQL applications.
Next we run the actual query and store the results in a variable.
Now we pull the results into another variable that we can reference from the rest of the script.
We are now at a point where you can reference the first record you've pulled out using the echo command and the name of the field. Lets say we want to display the monkey's name.
And that's the basics. There are tones of tutorials on this if you search google. Hopefully you can search for specific bits of code now if you need more help on it. You'll also need to look at loops to draw out all of the records in the database.
Hope this helps.
Code: Select all
$hostname = "localhost";
$database = "icon";
$username = "root";
$password = "password";
$myConnection = mysql_connect($hostname, $username, $password) or trigger_error(mysql_error(),E_USER_ERROR);Code: Select all
mysql_select_db($database, $myConnection);The next thing you need to do is write a statement. This is in MySQLs own script so it's not PHP code at all. I'm going to put it in a variable called query and I will be selecting all records from a table called monkey.
Code: Select all
$query = "SELECT * FROM monkey ";Next we run the actual query and store the results in a variable.
Code: Select all
$results = mysql_query($query, $myConnection);Code: Select all
$row = mysql_fetch_assoc($results);Code: Select all
echo $row['name'];Hope this helps.
Assuming you are using MySQL (but it generally works like this in other db libraries, too), you'd want to look here.
http://php.net/mysql_fetch_assoc
This will give you a good start.
http://php.net/mysql_fetch_assoc
This will give you a good start.
- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
-
Steve Mellor
- Forum Commoner
- Posts: 49
- Joined: Thu Aug 02, 2007 8:18 am
- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
I'd just like to add that mysql_fetch_assoc() advances the pointer, and will also evaluate as false when it reaches the end. Because of this, you can use a while loop to iterate the result set like so:
Code: Select all
while($row = mysql_fetch_assoc($results)){
echo $row['name'];
}-
Steve Mellor
- Forum Commoner
- Posts: 49
- Joined: Thu Aug 02, 2007 8:18 am
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm