Welcome to WebmasterWorld Guest from 54.145.209.34

Forum Moderators: coopster & jatar k

Something's not right. New to PHP please help.

Unable to read simple data from MySQL table

   
8:45 am on Mar 9, 2010 (gmt 0)

5+ Year Member



Hi,

I'm trying to run some basic tests on a domain I am hoping to develop. I'm new to PHP so this is really just a testing ground for me.

My goal : To display MySQL table fields on my webpage.

I have already set up the table, and viewed it with the host tool PHPmyAdmin. I can see that it has a single row with a number of fields.

The code :
<html>
<head>
<link rel="stylesheet" type="text/css" href="Glob_Style.css" />
</head>

<body>

<?php

$mysql_host = "localhost";
$mysql_database = "maindb";
$mysql_user = "main_user";
$mysql_password = "********";


mysql_connect ($mysql_host,$mysql_user,$mysql_password);
mysql_select_db ($mysql_database);

$query="SELECT * FROM artdetail";
$result=mysql_query($query);


while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "Artist : {$row['artist']} <br>" .
"ARTwork: {$row['artwork']} <br>" .
"Genre : {$row['genre']} <br>" ;
}
?>


</body>
</html>


I would appreciate any feedback because I've used this basic code before on a different host, and it's worked? So either my logon details are wrong, or there is a problem on the host side?

Thanks all.

[edited by: coopster at 1:09 pm (utc) on Mar 9, 2010]
[edit reason] no personal urls please [/edit]

9:32 am on Mar 9, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



echo "Artist : {$row['artist']} <br>" .
"ARTwork: {$row['artwork']} <br>" .
"Genre : {$row['genre']} <br>" ;

I think the problem here is bad syntax, try changing the above to this:

echo 'Artist : ' . $row['artist'] . '<br>
ARTwork: ' . $row['artwork'] . '<br>
Genre : ' . $row['genre'] . '<br>';
9:46 am on Mar 9, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Hi there Protospleen,

I think as the mods will zap that link as urls are generally meant to be example.com especially if the URL is to your site ;-p

Also for future reference:-


while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "Artist : {$row['artist']} <br>" .
"ARTwork: {$row['artwork']} <br>" .
"Genre : {$row['genre']} <br>" ;
}


Can be done with this:-


while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
?>
Artist : <?php echp $row['artist']; ?> <br>
ARTwork: <?php echo $row['artwork']; ?><br>
Genre : <?php echo $row['genre']; ?> <br>
<?php
}


This makes for for easier HTML editing & is less workload for the parser if you increase the amount of HTML being echoed ;-)

EDIT:

Just noticed that:-


$conn = mysql_connect ($mysql_host,$mysql_user,$mysql_password);
mysql_select_db ($mysql_database);

$query="SELECT * FROM artdetail";
$result=mysql_query($query, $conn) or die(mysql_error());

Just place the or die(mysql_error()); there to see what errors come from the db, but never let this be put there when you release..

You needed a connection reference in the query!

Hope that helps,

Cheers,

MRb

[edited by: Matthew1980 at 9:52 am (utc) on Mar 9, 2010]

9:50 am on Mar 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just for note,

echo "Artist : {$row['artist']} <br>";

That syntax is correct, it should work there.
Have you tried checking if the result is returning any rows at all ?

Since your post has not been moderated yet, and your link still appears here. "View source" of your page and you will see php code there. That means your server is not parsing your .html file as php. Either tell server to do so, or write your code in a .php file
9:53 am on Mar 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You needed a connection reference in the query!


Another note, Connection reference is not a "must", IMHO. :)
10:08 am on Mar 9, 2010 (gmt 0)

5+ Year Member



Thank you all - some great advice, some good tips, and Anyango I see what you mean! Thanks for pointing that out...

Much appreciated all.
10:09 am on Mar 9, 2010 (gmt 0)

5+ Year Member



I wrote it without the MYSQL_ASSOC and it worked for me!
1:42 pm on Mar 9, 2010 (gmt 0)

5+ Year Member



Thanks Anyango, put the code into a php file and it worked. :)

Is this normal for web hosts? Is it something they need to fix, or is there something I can do to get the server to parse the php in my html?
1:57 pm on Mar 9, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



All your .html files that use PHP should just be renamed as .php files. In fact, once you learn PHP/MySQL you'll be able to remove a lot of your .html files in place of one .php file.
 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month