Sign in to follow this  

[web] MYSQL

This topic is 4852 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm kinda new to the database scene, and I'm using mysql now. What I'm not sure is, how to grab data from a row and then format it. For example, you use a mysql_query('select blah'); and save it in a variable. Now, just echo() ing it doesn't work. I've heard people say "You need to use mysql_fetch_array or mysql_fetch_row blah blah blah" I've read the manual but I'm really sure what it's doing. What I'm tryign to do is seperate the fields of a obtained row to format that information but I see no way how you could do that! Thanks for help in advance.

Share this post


Link to post
Share on other sites
I'd use the PEAR DB class, or some other abstraction layer. It adds a lot of functionality and makes your code portable to other databases.


<?php
require_once('DB.php');

$db =& DB::connect('mysql://user:pass@host/dbname');
$db->setFetchMode(DB_FETCHMODE_ASSOC);

// method 1, using recordset
$rs = $db->query('SELECT * FROM foo');
while ($rs->fetchInto($row))
{
print $row['colname'];
}
$rs->free();

// method 2, array
$data = $db->getAll('SELECT * FROM foo');
foreach ($data as $row)
{
print $row['colname'];
}

// method 3, associative array
$data = $db->getAssoc('SELECT id,foo1,foo2 FROM foo');
foreach ($data as $id=>$row)
{
print $id;
print $row['foo1'];
}

// method 4, single row
$array = $db->getRow('SELECT * FROM foo');
print $array['colname'];

// method 5, single field
print $db->getOne('SELECT a FROM foo WHERE b=1');
?>

Share this post


Link to post
Share on other sites
Like kon said, the PEAR DB class is excellent. That's that I use for all my database access.

To answer your question a little more indepth using the mysql functions..... When you do you query on the database...

$result = mysql_query('SELECT * FROM foo');

$result will return the results of the query but in order to extract a row you need to use the mysql_fetch_row() function.

while ($row = mysql_fetch_row($result,MYSQL_ASSOC) {
print_r($row);
}

This will continue to print $row until all the rows have been iterated through.

passing the optional MYSQL_ASSOC will give you the row array with the keys from the field names in your table. 90 percent of the time that is probably what you want. Otherwise you just get integers 0 through # of fields.



Share this post


Link to post
Share on other sites
It sounds like you should read up on SQL, which is the universal DB query language.

SELECT a,b,c FROM d

* d would be the name of the table.
* a, b, c would be the names of individual fields/columns that you want to retreive.

Before you can do a SELECT query, you will have already chosen the database that you want to work with.

PHP has many official OO classes to help deal with common tasks. These are called PEAR. PEAR DB is usually installed by default under Linux. If you don't have it, you can just download the archive and extract the PHP files somewhere and include it in your PHP include_path.

Share this post


Link to post
Share on other sites

This topic is 4852 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this