scepanmali Posted March 21, 2008 Share Posted March 21, 2008 Why this code returns data from all 5 tables? It suppose to return only one result from id. What I have to do to resolve this problem? Please, someone, HELP $dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL"); echo "<br>"; $selected = mysql_select_db("spotovi1", $dbhandle) or die("Could not select DB"); $row['Artist']=$Artist; $row['Song']=$Song; $id= $row['id']; $row['id']= $id; $id = $_GET['id']; $Artist = $_GET['Artist']; $Song = $_GET['Song']; $tables = array("oblast_metal","oblast_rock","oblast_pop","oblast_hip_hop","oblast_domaca"); foreach($tables as $table){ $sql = mysql_query("SELECT * FROM ".$table." WHERE ID = '$id'"); while ($row = mysql_fetch_array($sql)) { ?> <?php $zajedno=$row['Artist']."- ".$row['Song']; echo "<font size=3><font color=white><b>Download </b></a> </font></font>"; echo "<font size=4><font color='#A49F8B'><a href=\"{$row['Link']}\"><font color='#A49F8B'>$zajedno</font></font></a>"; echo "<font size=3><font color=white><b> Music Video</b> </a> </font></font>"; } } php?> Quote Link to comment Share on other sites More sharing options...
scepanmali Posted March 21, 2008 Author Share Posted March 21, 2008 I have to explain some more! When I execute the query for just one row in this five tables it return rows from all five tables. I have metal, rock... tables and when I want to pull data from, let say, metal(by 'id'), it returns data not from just that table, but all tables for the requested "id" number. I just need to pull data from the clicked id row of the particular table, not the rest of them! Thank you in advance! Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted March 21, 2008 Share Posted March 21, 2008 try this: $sql = mysql_query("SELECT * FROM ".$table." WHERE ID = '$id'") or die(mysql_error()); Quote Link to comment Share on other sites More sharing options...
scepanmali Posted March 21, 2008 Author Share Posted March 21, 2008 No, it is still the same. I don't know what to do. I just can't find the mistake. Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted March 21, 2008 Share Posted March 21, 2008 okay, let's make sure the SQL is correct each time by echoing it. try this: $sql_t = "SELECT * FROM ".$table." WHERE ID = '$id'"; echo "$sql_t<BR>"; $sql = mysql_query($sql_t) or die(mysql_error()); etc.... Quote Link to comment Share on other sites More sharing options...
jkewlo Posted March 21, 2008 Share Posted March 21, 2008 $row['id']= $id; $id = $_GET['id']; should be $row['id']= $id; $id = $_POST['id']; i had the same problem little things can tear your head off. let me know how it works for you Quote Link to comment Share on other sites More sharing options...
mb81 Posted March 21, 2008 Share Posted March 21, 2008 No, it is still the same. I don't know what to do. I just can't find the mistake. -- The first mistake is in your data structure, why do you have different tables for different genres of music? -- If you don't fix that, then you have to have some way to determine which table that particular song is in before you display it. I am assuming that your id is the auto increment id for that table, so all 5 tables are going to have an id #1, all 5 will have a #2, all 5 will have #3, so you are going to get 5 results every time. Quote Link to comment Share on other sites More sharing options...
scepanmali Posted March 21, 2008 Author Share Posted March 21, 2008 I noticed that the first time do the right thing and then goes to line up the other 4, if there is any. So, can I limit this to display only the first one? To Jkewlo! Nope, but thanks for the effort Quote Link to comment Share on other sites More sharing options...
mb81 Posted March 21, 2008 Share Posted March 21, 2008 You need to be sending a variable from the previous page that tells you which table it's in, then get rid of the foreach loop and display just the one record. Quote Link to comment Share on other sites More sharing options...
scepanmali Posted March 21, 2008 Author Share Posted March 21, 2008 Yes, I done it, but the problem is I have 5 different tables so I can't do that(or I don't know how)! If it is only one table, it won't be a problem! Quote Link to comment Share on other sites More sharing options...
mb81 Posted March 21, 2008 Share Posted March 21, 2008 Yes, I done it, but the problem is I have 5 different tables so I can't do that(or I don't know how)! If it is only one table, it won't be a problem! If you are sending Artist, Song, and id from the page before this one, where does that information come from? Yes, I agree that it wouldn't be a problem if it was all in one table. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted March 21, 2008 Share Posted March 21, 2008 you should just add a genre field to a single table and store it all in 1 table Quote Link to comment Share on other sites More sharing options...
scepanmali Posted March 21, 2008 Author Share Posted March 21, 2008 It, came from one of that 5 tables we are talking about! Do you want to show you the code or... Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted March 21, 2008 Share Posted March 21, 2008 you should just add a genre field to a single table and store it all in 1 table there should only be one table, not 5. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted March 21, 2008 Share Posted March 21, 2008 make 1 new table then make a function that says <?php $tables = array("oblast_metal","oblast_rock","oblast_pop","oblast_hip_hop","oblast_domaca"); foreach($tables as $table){ $q = "Select * from `".$table."`"; $r = mysql_query($q) or die(mysql_error())."<br />".$q); while($row = mysql_fetch_assoc($r)){ $q = "Insert into `NEW_TABLE` FIELDS VALUES($ROW STUFF) $r2 = mysql_query($q) or die(mysql_error()."<br /><br />".$q); } } ?> and its all in 1 table now and u are happy Quote Link to comment Share on other sites More sharing options...
scepanmali Posted March 22, 2008 Author Share Posted March 22, 2008 I tried to use LIMIT 1 function for the code, but it's not working! It continues to echo results from all 5 tables. Can someone tell me why, or help me out to get it work? Thank you all for the help $sql = mysql_query("SELECT * FROM ".$table." WHERE ID = '$id' LIMIT 1"); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.