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

Visit PubCon.com
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

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


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 :
<link rel="stylesheet" type="text/css" href="Glob_Style.css" />



$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";

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


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)

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)

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>

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


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,



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


 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


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


 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.


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

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


 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?


 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