Welcome to WebmasterWorld Guest from 54.146.1.178

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

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)

New User

5+ Year Member

joined:Feb 25, 2009
posts: 23
votes: 0


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)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Dec 13, 2009
posts:945
votes: 0


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)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Feb 22, 2009
posts:1396
votes: 0


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)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 24, 2005
posts:697
votes: 0


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)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 24, 2005
posts:697
votes: 0


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)

New User

5+ Year Member

joined:Feb 25, 2009
posts:23
votes: 0


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)

New User

5+ Year Member

joined:Mar 8, 2010
posts:11
votes: 0


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

New User

5+ Year Member

joined:Feb 25, 2009
posts:23
votes: 0


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)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:May 6, 2008
posts:2011
votes: 0


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.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members