Welcome to Dream.In.Code
Become a PHP Expert!

Join 137,395 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,125 people online right now. Registration is fast and FREE... Join Now!




mySQL Results Not Being Displayed

 
Reply to this topicStart new topic

mySQL Results Not Being Displayed

BamaStangGuy
5 Oct, 2006 - 07:20 PM
Post #1

New D.I.C Head
*

Joined: 5 Oct, 2006
Posts: 11


My Contributions
What I want to ultimately do is build a database of Music lyrics and a search engine to find the lyrics (much like the countless music lyrics websites out there) for a Music site I have started up. I am just beginning to learn PHP. I have always known the basics but the basics don't do me any good when I want to code my own project.

So I have been looking around at articles and they all have confused me. Each article shows me a different way to ultimately do the same thing. I don't know which is better.

Well the below code is my first attempt at just connecting to the mysql database and retrieving information. Apparenlty it isnt working cause when I open the page its just a blank white page.

So if there is anything that you can spot (I am sure it is obvious) it would be very helpful to get me started.

and by any off chance you can provide me with a nice article that will help me achieve this goal of a music lyrics site that would be great as well.

Thanks for any help

CODE
<?php
$dbcnx = @mysql_connect("localhost", "xxxxxx", "xxxxxx");
if (!$dbcnx) {
  echo( "<p>Unable to connect to the " .
        "database server at this time. Which sucks cause damn't I want this to work.</p>" );
  exit();
}

if (! @mysql_select_db("xxxxxxxx") ) {
  echo( "<P>Unable to locate the joke " .
        "database at this time.</P>" );
  exit();
}

$result = mysql_query("SELECT id, artist, album, song, lyrics FROM lyrics_lyrics ORDER BY id");
if (!$result) {
  echo("<P>Error performing query: " .
       mysql_error() . "</P>");
  exit();
}

$while ($row = mysql_fetch_array($result)) {

$id = $row['id'];
$artist = $row['artist'];
$album = $row['album'];
$song = $row['song'];
$lyrics = $row['lyrics'];

}
?>

<html>
<head>
<title>Music Lyrics Search Engine</title>
</head>
<body>
I am just trying to get the hang of this. Let's see if I can spit out what I got right now.
<br />
<br />
<table cellspacing="5" cellpadding="5" border="0" width="100%" align="center">
<tr>
<td>ID</td>
<td>Artist</td>
<td>Album</td>
<td>Song</td>
<td>Lyrics</td>
</tr>
<tr>
<td><? $id ?></td>
<td><? $artist ?></td>
<td><? $album ?></td>
<td><? $song ?></td>
<td><? $lyrics ?></td>
</tr>
</table>
</body>
</html>

User is offlineProfile CardPM
+Quote Post

skyhawk133
RE: MySQL Results Not Being Displayed
5 Oct, 2006 - 07:28 PM
Post #2

Head DIC Head
Group Icon

Joined: 17 Mar, 2001
Posts: 14,974



Thanked: 48 times
Dream Kudos: 1650
Expert In: Web Development

My Contributions
I'm not going to test this, so pardon me if there are errors. But the first issue you've got is you're looping over your query result and not outputting anything. You're just setting variables then trying to output them later. Since you're bringing back multiple rows, your while statement needs to be around the <tr> </tr> tags in the table so that each iteration creates a new row. Something like this:

CODE
<?php
$dbcnx = @mysql_connect("localhost", "xxxxxx", "xxxxxx");
if (!$dbcnx) {
  echo( "<p>Unable to connect to the " .
        "database server at this time. Which sucks cause damn't I want this to work.</p>" );
  exit();
}

if (! @mysql_select_db("xxxxxxxx") ) {
  echo( "<P>Unable to locate the joke " .
        "database at this time.</P>" );
  exit();
}

$result = mysql_query("SELECT id, artist, album, song, lyrics FROM lyrics_lyrics ORDER BY id");
if (!$result) {
  echo("<P>Error performing query: " .
       mysql_error() . "</P>");
  exit();
}


?>

<html>
<head>
<title>Music Lyrics Search Engine</title>
</head>
<body>
I am just trying to get the hang of this. Let's see if I can spit out what I got right now.
<br />
<br />
<table cellspacing="5" cellpadding="5" border="0" width="100%" align="center">
<tr>
<td>ID</td>
<td>Artist</td>
<td>Album</td>
<td>Song</td>
<td>Lyrics</td>
</tr>
<?
//begin looping over result set
while ($row = mysql_fetch_array($result)) {
?>
<tr>
<td><? echo $row['id']; ?>td>
<td><? echo $row['artists']; ?></td>
<td><? echo $row['album']; ?></td>
<td><? echo $row['song']; ?></td>
<td><? echo $row['lyrics']; ?></td>
</tr>
<?
// End While Loop
}
?>

</table>
</body>
</html>

User is offlineProfile CardPM
+Quote Post

snoj
RE: MySQL Results Not Being Displayed
5 Oct, 2006 - 07:35 PM
Post #3

$Null
Group Icon

Joined: 31 Mar, 2003
Posts: 3,304



Thanked: 7 times
Dream Kudos: 700
My Contributions
Yeah skyhawk, the code looks like it'll compile (I haven't tested it either).

I'd suggest that if you're really looking into making a full fledged lyrics site, to look into using templates and a templating system like Smarty. Not only will it help you in developing the site, but it'll teach you more about how to use PHP even better!
User is offlineProfile CardPM
+Quote Post

BamaStangGuy
RE: MySQL Results Not Being Displayed
5 Oct, 2006 - 07:36 PM
Post #4

New D.I.C Head
*

Joined: 5 Oct, 2006
Posts: 11


My Contributions
Your code worked Skyhawk smile.gif Thanks a ton

QUOTE(hotsnoj @ 5 Oct, 2006 - 08:35 PM) *

Yeah skyhawk, the code looks like it'll compile (I haven't tested it either).

I'd suggest that if you're really looking into making a full fledged lyrics site, to look into using templates and a templating system like Smarty. Not only will it help you in developing the site, but it'll teach you more about how to use PHP even better!


I will look into that then. Just taking it a step at a time right now smile.gif Thanks for all the help and advice
User is offlineProfile CardPM
+Quote Post

marlinspike
RE: MySQL Results Not Being Displayed
9 Feb, 2007 - 10:07 AM
Post #5

New D.I.C Head
*

Joined: 9 Feb, 2007
Posts: 1


My Contributions
I am a new user. I have installed Apache, PHP and MySql. PHP works well (tested with simple script), MySQL works if I open a MySQL window, enter password and then set up basic tables. Both my PHP book and online tutorials ask me to use this simple piece of code:
CODE

<?php
$msg="You did not connect";
$user="root";
$server="localhost";
$pwd="******"
$conn=mysql_connect($server,$user,$pwd)
if($conn){
$msg="Congratulations, $user, you connected to MySQL";
?>
<html><head><title>connecting user</title></head>
<body>
<?php echo($msg); ?>
<?php echo("user is $user, server is $server, password is $pwd")
</body>
</html>


The code gave me a white page - no errors, nothing. I commented out the mysql_connect line and got the messages. I suspected that it has something to do with one of the .ini files so I downloaded php_mysqli.dll and libmysql.dll, set up a folder in apache and pointed the php extentions there, as well as updating the ini file to include the two new extension. Of course this also had no effect.

If you have any suggestions, I'd be very grateful for some help!!

Cheers
User is offlineProfile CardPM
+Quote Post

BetaWar
RE: MySQL Results Not Being Displayed
9 Feb, 2007 - 11:25 AM
Post #6

#include <soul.h>
Group Icon

Joined: 7 Sep, 2006
Posts: 2,054



Thanked: 84 times
Dream Kudos: 1175
My Contributions
Okay, the problem that you are having is that you are telling it to do all of the looping way up in the first PHP section. It doesn't loop through the whole program everyime you tell it to loop, just through the beginning and end of the loop brackets. That means that you are telling it to output only one row of database info. I have also found that normally unless you specify this it tries to add an extra blank set of results to the end. That means that it is outputing everything that it is supposed to. The only problem is that your code isn't telling it to output what you think it it.

Try using skyhawk's code, it has the loop positioned in the right place to at least get the output to work.

Also not all codes on the web are without flaws. You would be amazed how many codes I have come upon that claim to work but don't. That is the reason it is easiest to make your own asap.
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: MySQL Results Not Being Displayed
9 Feb, 2007 - 11:41 AM
Post #7

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
If you are referring to the first user in the thread, he indicated the problem had been solved a few months ago.
User is offlineProfile CardPM
+Quote Post

BetaWar
RE: MySQL Results Not Being Displayed
9 Feb, 2007 - 09:04 PM
Post #8

#include <soul.h>
Group Icon

Joined: 7 Sep, 2006
Posts: 2,054



Thanked: 84 times
Dream Kudos: 1175
My Contributions
Oh, missed that. Sorry I thought the guy above my previous post was the same who started the topic.

My bad.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/5/08 02:49AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month