homepage Welcome to WebmasterWorld Guest from 54.225.24.227
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Something's not right. New to PHP please help.
Unable to read simple data from MySQL table
Protospleen




msg:4093814
 8:45 am on Mar 9, 2010 (gmt 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]

 

Readie




msg:4093834
 9:32 am on Mar 9, 2010 (gmt 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>';

Matthew1980




msg:4093847
 9:46 am on Mar 9, 2010 (gmt 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]

Anyango




msg:4093852
 9:50 am on Mar 9, 2010 (gmt 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

Anyango




msg:4093857
 9:53 am on Mar 9, 2010 (gmt 0)

You needed a connection reference in the query!


Another note, Connection reference is not a "must", IMHO. :)

Protospleen




msg:4093875
 10:08 am on Mar 9, 2010 (gmt 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.

Barnamedve




msg:4093876
 10:09 am on Mar 9, 2010 (gmt 0)

I wrote it without the MYSQL_ASSOC and it worked for me!

Protospleen




msg:4093966
 1:42 pm on Mar 9, 2010 (gmt 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?

StoutFiles




msg:4093974
 1:57 pm on Mar 9, 2010 (gmt 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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved