[web] MYSQL

Started by
3 comments, last by konForce 19 years, 8 months ago
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.
Advertisement
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');?>
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.



Oh, I must have been mixed up, select blah is selecting a talbe from a database? Ok, and what's the PEAR DB thing?
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.

This topic is closed to new replies.

Advertisement