Jump to content

MySql Info Display Problem


theweirdone

Recommended Posts

Hey, I'm having some trouble with this code:
[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();

?>[/code]

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
Link to comment
Share on other sites

[!--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.
Link to comment
Share on other sites

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...
Link to comment
Share on other sites

Try this:
[code]<?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();

?>[/code]
Try running your code again. Do you get any errors?
Link to comment
Share on other sites

Still doesn't work... I don't get it... Here's the code to add songs, it might help find the problem...
[code]
<?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();

?>
[/code]

Hope it helps find the problem...
Link to comment
Share on other sites

Try chaning
[code]$query = "INSERT INTO Songs (song_title) VALUES ('$song_title')";[/code]

[code]$query = "INSERT INTO `Songs` VALUES ('', '$song_title')";[/code]

As you wrote this
[code]$song_title = $_GET['song_title'];[/code]
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..
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.