Ameslee Posted April 18, 2006 Share Posted April 18, 2006 Hope someone can help me, im new at PHP and to tell you the truth this is my very first time at integration into a website. Can someone tell me how i display stuff from my database to my website pages? I just want records from my database to be displayed. HELP please!Thanks Quote Link to comment Share on other sites More sharing options...
devofash Posted April 18, 2006 Share Posted April 18, 2006 there are several ways to display data from mysql databasehere's the way i do it[code]<?php $hostname = localhost; $username = username; $password = password; $db = mysql_connect($hostname, $username, $password) or die(mysql_error()); $connection = mysql_select_db("mydb", $db); $sql = "SELECT fname, lname FROM table_name"; $result = mysql_query($sql, $connection); while ($rows = mysql_fetch_array($result)) { $fname = $rows['fname']; $lname = $rows['lname']; echo $fname; echo $lname; }?>[/code]i'm havent tested it..... i probably missed a semi colon somewhere .. but it should be alrite .... and a simple google search will give u lots and lots of info on using php/mysql :) Quote Link to comment Share on other sites More sharing options...
Ameslee Posted April 18, 2006 Author Share Posted April 18, 2006 Can't you use a database include, like include("database.inc");?or do you use the kind of way like below?[!--quoteo(post=365806:date=Apr 18 2006, 11:21 AM:name=devofash)--][div class=\'quotetop\']QUOTE(devofash @ Apr 18 2006, 11:21 AM) [snapback]365806[/snapback][/div][div class=\'quotemain\'][!--quotec--]there are several ways to display data from mysql databasehere's the way i do it[code]<?php $hostname = localhost; $username = username; $password = password; $db = mysql_connect($hostname, $username, $password) or die(mysql_error()); $connection = mysql_select_db("mydb", $db); $sql = "SELECT fname, lname FROM table_name"; $result = mysql_query($sql, $connection); while ($rows = mysql_fetch_array($result)) { $fname = $rows['fname']; $lname = $rows['lname']; echo $fname; echo $lname; }?>[/code]i'm havent tested it..... i probably missed a semi colon somewhere .. but it should be alrite .... and a simple google search will give u lots and lots of info on using php/mysql :)[/quote] Quote Link to comment Share on other sites More sharing options...
Ameslee Posted April 18, 2006 Author Share Posted April 18, 2006 And also, when someone views the source are they going to be able to get the php code? Quote Link to comment Share on other sites More sharing options...
earl_dc10 Posted April 18, 2006 Share Posted April 18, 2006 include doesn't necessarily display the data, it just, like it's name implies, includes its info into the current documentand php is server code, it is parsed before it ever reaches the user, they can only see the output[code]<?phpecho "<h1>Hello<h1>";?>//displays in web source code<h1>Hello<h1>[/code] Quote Link to comment Share on other sites More sharing options...
Ameslee Posted April 18, 2006 Author Share Posted April 18, 2006 I can't get the above code to work, i have changed it to suit my purposes, here it is:<?php $hostname = localhost; $username = *******_***; $password = ********; $conn = mysql_connect($hostname, $username, $password) or die(mysql_error()); $connection = mysql_select_db($database_conn, $conn); $query = "SELECT name, age, gifts, about FROM Stories"; $mysql_result=mysql_query($query,$conn); while ($rows = mysql_fetch_array($mysql_result)) { $name = $rows['name']; $age = $rows['age']; $gifts = $rows['gifts']; $about = $rows['about']; <table> <tr> <td><b>Name</b><td>echo $name; <tr><td><b>Age</b><td>echo $age; <tr><td><b>Gifts and Talents</b><td>echo $gifts; <tr><td><b>More about $name</b><td>echo $about;</tr> </table> } ?> Quote Link to comment Share on other sites More sharing options...
Ameslee Posted April 18, 2006 Author Share Posted April 18, 2006 ERROR MESSAGE:Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/tafeamy/public_html/project/Klever Kids/stories.php on line 106please help!Thanks Quote Link to comment Share on other sites More sharing options...
Ameslee Posted April 18, 2006 Author Share Posted April 18, 2006 i have also changed the code to this:?php $connect = mysql_connect("localhost","*******_***","********"); $db = mysql_select_db("Stories",$connect); $query = "SELECT * FROM Stories"; $mysql_result = mysql_query($query); $display_array = mysql_fetch_array($mysql_result); echo $display_array["name"]; echo $display_array["age"]; echo $display_array["gifts"]; echo $display_array["about"]; ?>and still get this error:Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/tafeamy/public_html/project/Klever Kids/stories.php on line 105 Quote Link to comment Share on other sites More sharing options...
Hooker Posted April 18, 2006 Share Posted April 18, 2006 Try This:[code]<?php$hostname = localhost;$username = *******_***;$password = ********;$conn = mysql_connect($hostname, $username, $password) or die(mysql_error());$connection = mysql_select_db($database_conn, $conn);$query = "SELECT name, age, gifts, about FROM Stories";$mysql_result=mysql_query($query,$conn);echo("<table>");while ($rows = mysql_fetch_array($mysql_result)){echo("<tr><td><b>Name</b><td>" . $rows['name'] . "<tr><td><b>Age</b><td>" . $rows['age'] . "<tr><td><b>Gifts and Talents</b><td>" . $rows['gifts'] . "<tr><td><b>More about " . $rows['name'] . "</b><td>" . $rows['about'] . "</tr>");}echo("</table>");?>[/code]you cant output raw html within the <?php ?> tags, you need to echo them, also advanced warning: make sure to \ any " or ' within echos like this:[code]<?phpecho("Foo \"Bar\"");?>[/code]to avoid any parse errors - sorry if the code is kinda messey, i was in a rush :) Quote Link to comment Share on other sites More sharing options...
Ameslee Posted April 18, 2006 Author Share Posted April 18, 2006 Thanks alot, this does work, fine.[!--quoteo(post=365895:date=Apr 18 2006, 03:30 PM:name=Hooker)--][div class=\'quotetop\']QUOTE(Hooker @ Apr 18 2006, 03:30 PM) [snapback]365895[/snapback][/div][div class=\'quotemain\'][!--quotec--]Try This:[code]<?php$hostname = localhost;$username = *******_***;$password = ********;$conn = mysql_connect($hostname, $username, $password) or die(mysql_error());$connection = mysql_select_db($database_conn, $conn);$query = "SELECT name, age, gifts, about FROM Stories";$mysql_result=mysql_query($query,$conn);echo("<table>");while ($rows = mysql_fetch_array($mysql_result)){echo("<tr><td><b>Name</b><td>" . $rows['name'] . "<tr><td><b>Age</b><td>" . $rows['age'] . "<tr><td><b>Gifts and Talents</b><td>" . $rows['gifts'] . "<tr><td><b>More about " . $rows['name'] . "</b><td>" . $rows['about'] . "</tr>");}echo("</table>");?>[/code]you cant output raw html within the <?php ?> tags, you need to echo them, also advanced warning: make sure to \ any " or ' within echos like this:[code]<?phpecho("Foo \"Bar\"");?>[/code]to avoid any parse errors - sorry if the code is kinda messey, i was in a rush :)[/quote] 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.