Jump to content


Photo

Integration into website


  • Please log in to reply
9 replies to this topic

#1 Ameslee

Ameslee
  • Members
  • PipPipPip
  • Advanced Member
  • 131 posts

Posted 18 April 2006 - 12:36 AM

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

#2 devofash

devofash
  • Members
  • PipPipPip
  • Advanced Member
  • 81 posts

Posted 18 April 2006 - 01:21 AM

there are several ways to display data from mysql database

here's the way i do it


<?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;
     }
?>


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 :)

#3 Ameslee

Ameslee
  • Members
  • PipPipPip
  • Advanced Member
  • 131 posts

Posted 18 April 2006 - 02:23 AM

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) View Post[/div][div class=\'quotemain\'][!--quotec--]
there are several ways to display data from mysql database

here's the way i do it


<?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;
     }
?>
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]


#4 Ameslee

Ameslee
  • Members
  • PipPipPip
  • Advanced Member
  • 131 posts

Posted 18 April 2006 - 02:35 AM

And also, when someone views the source are they going to be able to get the php code?

#5 earl_dc10

earl_dc10
  • Members
  • PipPipPip
  • Advanced Member
  • 71 posts

Posted 18 April 2006 - 02:38 AM

include doesn't necessarily display the data, it just, like it's name implies, includes its info into the current document

and php is server code, it is parsed before it ever reaches the user, they can only see the output
<?php
echo "<h1>Hello<h1>";
?>

//displays in web source code
<h1>Hello<h1>

got a problem? Google helps many of those in need

#6 Ameslee

Ameslee
  • Members
  • PipPipPip
  • Advanced Member
  • 131 posts

Posted 18 April 2006 - 03:08 AM

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>
}
?>

#7 Ameslee

Ameslee
  • Members
  • PipPipPip
  • Advanced Member
  • 131 posts

Posted 18 April 2006 - 03:24 AM

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 106

please help!

Thanks

#8 Ameslee

Ameslee
  • Members
  • PipPipPip
  • Advanced Member
  • 131 posts

Posted 18 April 2006 - 05:04 AM

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


#9 Hooker

Hooker
  • Members
  • PipPipPip
  • Advanced Member
  • 193 posts
  • LocationWales, UK

Posted 18 April 2006 - 05:30 AM

Try This:

<?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>");
?>

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:

<?php
echo("Foo \"Bar\"");
?>

to avoid any parse errors - sorry if the code is kinda messey, i was in a rush :)

#10 Ameslee

Ameslee
  • Members
  • PipPipPip
  • Advanced Member
  • 131 posts

Posted 18 April 2006 - 05:52 AM

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) View Post[/div][div class=\'quotemain\'][!--quotec--]
Try This:

<?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>");
?>

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:

<?php
echo("Foo \"Bar\"");
?>

to avoid any parse errors - sorry if the code is kinda messey, i was in a rush :)
[/quote]





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users