mc136 Posted November 18, 2006 Share Posted November 18, 2006 Hi guysbrand new to php. Ive created a mysql database containing fields e.g. name, description etc.Question is how can I display these fields using loop system. Currently using :foreach (array("name", "description") as $v) { echo "$v\n";}would much prefer to query the database and pull back this info.Rgdsmc136 Quote Link to comment Share on other sites More sharing options...
fert Posted November 18, 2006 Share Posted November 18, 2006 [code]$cn=@mysql_connect("host","username","password");@mysql_select_db("name".$cn);$result=@mysql_query("SELECT * FROM `table`",$cn);$row=mysql_fetch_array(result);for($count=0;$count<mysql_num_rows($result);$count++){echo $row[$count];echo "<br>";}@mysql_close($cn);[/code] Quote Link to comment Share on other sites More sharing options...
jsladek Posted November 18, 2006 Share Posted November 18, 2006 Since your brand new, help me out and give this a try. http://www.iobe.net/proj/Hopefully you have a few tables in your database.I'm just looking for feedback and a newbie is a good tester.-John Quote Link to comment Share on other sites More sharing options...
mc136 Posted November 18, 2006 Author Share Posted November 18, 2006 thanks for the input. when trying this it pulls back what is in the row (nice to know) but i need something simpler to pull back the field names first i.e.Date: Namethen to pull back the rows under each column (thanks). cheersmc136will check out your site also andlet you know Quote Link to comment Share on other sites More sharing options...
mc136 Posted November 19, 2006 Author Share Posted November 19, 2006 note to self:To do this, you should use mysql_fetch_array, or in many cases, mysql_fetch_assoc. The first one obtains an array where you can either use the column name, or a row number. Since I don't think in numbers, I use the second one, which only obtains an array of the column names. More info: Quote Link to comment Share on other sites More sharing options...
mc136 Posted November 19, 2006 Author Share Posted November 19, 2006 <?phpinclude ("db_connect.inc"); $result=@mysql_query("SELECT * FROM `test`"); $row=mysql_fetch_array($result,MYSQL_ASSOC);print_r($row); foreach($row as $key => $val){echo $key;echo "<br>";}echo $row[0]; Quote Link to comment Share on other sites More sharing options...
vlowe Posted November 19, 2006 Share Posted November 19, 2006 [quote author=jsladek link=topic=115481.msg470228#msg470228 date=1163887525]Since your brand new, help me out and give this a try. http://www.iobe.net/proj/Hopefully you have a few tables in your database.I'm just looking for feedback and a newbie is a good tester.-John [/quote]Install fails Quote Link to comment Share on other sites More sharing options...
mc136 Posted November 19, 2006 Author Share Posted November 19, 2006 and finally..<?php include("db_connect.inc"); $numfields = mysql_num_fields($result); for ($i=0;$i<$numfields;$i++) { echo "<td>".mysql_field_name($result, $i).":</td>"; }?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.