Jump to content

Integration into website


Ameslee

Recommended Posts

there are several ways to display data from mysql database

here'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 :)
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 database

here'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]
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
[code]
<?php
echo "<h1>Hello<h1>";
?>

//displays in web source code
<h1>Hello<h1>
[/code]
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>
}
?>
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
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]<?php
echo("Foo \"Bar\"");
?>[/code]

to avoid any parse errors - sorry if the code is kinda messey, i was in a rush :)
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]<?php
echo("Foo \"Bar\"");
?>[/code]

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.