Jump to content


Photo

Question plz


  • Please log in to reply
3 replies to this topic

#1 esoom

esoom
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 22 May 2006 - 01:33 AM

if i have a table with its hostname as DBserver and database name as Dbase and the table consists of 5 columns called catref, title, artist, catorgry and number

how would i write the code to connect to the database and retrieve all the information on the table as a results table.....can some1 help me

#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 22 May 2006 - 01:45 AM

assuming you have a table called 'table' and some columns called 'name' and 'email' for example purposes:

//database information used to connect
$host = 'localhost'; // the host name for the database. usually 'localhost' works
$user = 'username'; // the database username
$pass = 'password'; // the database password
$dbname = 'databasename'; //name of your database

//connect to the database
$conn = mysql_connect($host, $user, $pass) or die(mysql_error());
$db = mysql_select_db($dbname) or die(mysql_error();

//select all the information from the table
$sql = "select * from table";
$result = mysql_query($sql, $conn) or die(mysql_error());

//get the results of the query and print it out
while ($listinfo = mysql_fetch_array($result)) {
   echo $listinfo['name'] . " " . $listinfo['email'] . "<br>";
}

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 esoom

esoom
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 22 May 2006 - 02:21 AM

Oh right

Thank you kind sir [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]

#4 esoom

esoom
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 22 May 2006 - 05:54 PM

another thing

ive written this code

<?php
$cat="Rock";
$result=@mysql_query("SELECT Title, Artist
FROM Stock WHERE Category='$cat'");
while ($item=@mysql_fetch_array($result))
{
extract($item);
print("<p>$Title $Artist</p>");
}
$number=@mysql_num_rows($result);
print("<p>$number matches found</p>");
?>

but how do i generate this in html when this run




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users