Jump to content


Photo

MySql Info Display Problem


  • Please log in to reply
6 replies to this topic

#1 theweirdone

theweirdone
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 27 June 2006 - 01:51 AM

Hey, I'm having some trouble with this code:
<?php

include("dogs.inc");

mysql_connect($host,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "SELECT song_title FROM Songs";
$results = mysql_query($query);

while($row = mysql_fetch_assoc($query)){
echo "$row['song_title']";
}



mysql_close();

?>

This is in a file called all_songs.inc which is being called by a file called index.php. I can't find the problem. The variables $host, $username, $password and $database are all being stored in the file dogs.inc. I know that they are correct, because i have another file adding files to the database, and it works fine. The database has one table called Songs, with two fields: song_id and song_title. I've tried using the functions mysql_fetch_assoc and mysql_fetch_array. Should I be using a different one? Anyone have any ideas?
Oh, and what does the "@" in front of mysql_select_db do?

Thanx
¯|¯µ€W€¦®ÐØÑ€

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 27 June 2006 - 01:59 AM

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Hey, I'm having some trouble with this code[/quote]

Any particular kind of trouble?

The @ suppresses errors - remove it, as I assume you have some kind of error.
Legend has it that reading the manual never killed anyone.
My site

#3 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 27 June 2006 - 09:17 AM

Try to rename dogs.inc to dogs.inc.php
Not sure if this causes the problem.

Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.


#4 theweirdone

theweirdone
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 28 June 2006 - 07:14 PM

Sorry, should have been more specific. The thing is, it's suppose to display song titles in the database, but nothing shows, it's blank.
I took off the '@', but it still shows nothing. I also renamed 'dogs.inc' to 'dogs.inc.php', and that doesn't help either. I've read through the code a few times, and I can't find the error.
Would it help if I showed the code that adds songs to the database? Cause that works fine...
¯|¯µ€W€¦®ÐØÑ€

#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 28 June 2006 - 07:26 PM

Try this:
<?php

include("dogs.inc");

mysql_connect($host,$username,$password) or die("Unable to connnect to mysql: " . mysql_error());

mysql_select_db($database) or die( "Unable to select database: " . mysql_error());

$query = "SELECT song_title FROM Songs";
$results = mysql_query($query) or die("Unable to perform query: <i>" . $query . "</i>: " . mysql_error());

while($row = mysql_fetch_assoc($query)){
echo "$row['song_title']";
}

mysql_close();

?>
Try running your code again. Do you get any errors?

#6 theweirdone

theweirdone
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 28 June 2006 - 07:44 PM

Still doesn't work... I don't get it... Here's the code to add songs, it might help find the problem...
<?php
include("dogs.inc.php");

$song_title = $_GET['song_title'];

mysql_connect($host,$username,$password);
mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO Songs (song_title) VALUES ('$song_title')";
mysql_query($query) or die("Unable to do query");
echo "Song $song_title has been added successfully";

mysql_close();

?>

Hope it helps find the problem...
¯|¯µ€W€¦®ÐØÑ€

#7 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 29 June 2006 - 08:38 AM

Try chaning
$query = "INSERT INTO Songs (song_title) VALUES ('$song_title')";

$query = "INSERT INTO `Songs` VALUES ('', '$song_title')";

As you wrote this
$song_title = $_GET['song_title'];
So let's say example you save the file as addsong.php
People can add song by going to [a href=\"http://domain/addsong.php?song_title=Crazy_Frog\" target=\"_blank\"]http://domain/addsong.php?song_title=Crazy_Frog[/a]

Crazy_Frog will be added to database..

Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users