Jump to content

Help retrieving data from databases


cs1h

Recommended Posts

Hi,

 

I have a script that retrieves information from three databases and compiles it all together and displays it (it repeats this three times). The problem is that its meant to take some information from the second databse and use it to get the required information from the third database but it keeps coming up with no results for the third one. The information is in the third database but its not getting it.

 

The code is,

<?php 

mysql_connect("localhost","adfsd","ssfdsdf"); 

mysql_select_db("rsffsd") or die("Unable to select database");

$art = $_GET['id'];
$sql = "SELECT * FROM items WHERE id=$art";

$result = mysql_query($sql);
$num_rows = mysql_num_rows($result);
if($num_rows == 0) {
echo "No results please try a different <a href=asearch.html>search</a>.";
} else {
while($row = mysql_fetch_array($result)) {
$code1 = $row['code1']; 
$code2 = $row['code2'];
}
}

mysql_connect("localhost","dfssdfsd","sdffds"); 

mysql_select_db("dsfsdfsd") or die("Unable to select database"); 

$sql = "SELECT * FROM `$code1 by $code2` ORDER BY id DESC LIMIT 3";

$result = mysql_query($sql) or die(mysql_error());

$num_rows = mysql_num_rows($result);
if($num_rows == 0) {
echo "";
} else {
$CommAuth = array();
$Comment = array();
$horde = array();
$title = array();
while($row = mysql_fetch_assoc($result)) {
$CommAuth[] .= $row['name']; 
$Comment[] .= $row['message'];
$horde[] .= $row['horde'];
$title[] .= $row['title'];
}
}

mysql_connect("localhost","sdfdfssd","sfsdfds"); 

mysql_select_db("ssdfsdfsdf") or die("Unable to select database");

$sql2 = "SELECT * FROM ac_users WHERE username='$title' LIMIT 3";

$result2 = mysql_query($sql2);
$num_rows2 = mysql_num_rows($result2);
if($num_rows2 == 0) {
echo "No results please try a different <a href=reports.php>search 2</a>.";
} else {
$CommAvatar = array();
while($row2 = mysql_fetch_array($result2)) {
$CommAvatar[] .= $row2['avatar']; 
}
}
$count = 0;
foreach ($CommAuth as $value){

print "<div id=\"comment\">
<img src=\"/avatar/{$CommAvatar[$count]}\" width=\"56\" height=\"56\" class=\"comment_photo\" />
<div id=\"comment_header\"><span class=\"comment_author\"> {$value} </span><span class=\"comment_wrote\">wrote...</span><span class=\"comment_stars\"><img src=\"stars_5.png\"/></span></div>
<div class=\"comment_text\">{$Comment[$count]}</div>
<div id=\"comment_footer\"><span class=\"comment_delete\">Report | Comment on {$value} Profile | Message {$value}</span></div>
</div>";
$count++;
}
?>

 

Does anyone know why this might be?

 

Thanks,

Colin

Link to comment
https://forums.phpfreaks.com/topic/102105-help-retrieving-data-from-databases/
Share on other sites

Try this, I just placed the 2nd half inside the while loops, it may be wrong as I'm not sure if Foreach loops work inside While Loops.

 

<?php 

mysql_connect("localhost","adfsd","ssfdsdf"); 

mysql_select_db("rsffsd") or die("Unable to select database");

$art = $_GET['id'];
$sql = "SELECT * FROM items WHERE id=$art";

$result = mysql_query($sql);
$num_rows = mysql_num_rows($result);
if($num_rows == 0) {
echo "No results please try a different <a href=asearch.html>search</a>.";
} else {
while($row = mysql_fetch_array($result)) {
$code1 = $row['code1']; 
$code2 = $row['code2'];
}

mysql_connect("localhost","dfssdfsd","sdffds"); 

mysql_select_db("dsfsdfsd") or die("Unable to select database"); 

$sql = "SELECT * FROM `$code1 by $code2` ORDER BY id DESC LIMIT 3";

$result = mysql_query($sql) or die(mysql_error());

$num_rows = mysql_num_rows($result);
if($num_rows == 0) {
echo "";
} else {
$row = mysql_fetch_assoc($result) {
$CommAuth[] .= $row['name']; 
$Comment[] .= $row['message'];
$horde[] .= $row['horde'];
$title[] .= $row['title'];
}
}
}

mysql_connect("localhost","sdfdfssd","sfsdfds"); 

mysql_select_db("ssdfsdfsdf") or die("Unable to select database");

$sql2 = "SELECT * FROM ac_users WHERE username='$title' LIMIT 3";

$result2 = mysql_query($sql2);
$num_rows2 = mysql_num_rows($result2);
if($num_rows2 == 0) {
echo "No results please try a different <a href=reports.php>search 2</a>.";
} else {
$CommAvatar = array();
while($row2 = mysql_fetch_array($result2)) {
$CommAvatar[] .= $row2['avatar']; 

$count = 0;
foreach ($CommAuth as $value){

print "<div id=\"comment\">
<img src=\"/avatar/{$CommAvatar[$count]}\" width=\"56\" height=\"56\" class=\"comment_photo\" />
<div id=\"comment_header\"><span class=\"comment_author\"> {$value} </span><span class=\"comment_wrote\">wrote...</span><span class=\"comment_stars\"><img src=\"stars_5.png\"/></span></div>
<div class=\"comment_text\">{$Comment[$count]}</div>
<div id=\"comment_footer\"><span class=\"comment_delete\">Report | Comment on {$value} Profile | Message {$value}</span></div>
</div>";
$count++;
}
}
}
?>

Try:

 

<?php 

mysql_connect("localhost","adfsd","ssfdsdf"); 

mysql_select_db("rsffsd") or die("Unable to select database");

$art = $_GET['id'];
$sql = "SELECT * FROM items WHERE id=$art";

$result = mysql_query($sql);
$num_rows = mysql_num_rows($result);
if($num_rows == 0) {
echo "No results please try a different <a href=asearch.html>search</a>.";
} else {
while($row = mysql_fetch_array($result)) {
$code1 = $row['code1']; 
$code2 = $row['code2'];
}

mysql_connect("localhost","dfssdfsd","sdffds"); 

mysql_select_db("dsfsdfsd") or die("Unable to select database"); 

$sql = "SELECT * FROM `$code1 by $code2` ORDER BY id DESC LIMIT 3";

$result = mysql_query($sql) or die(mysql_error());

$num_rows = mysql_num_rows($result);
if($num_rows == 0) {
echo "";
} else {
$row = mysql_fetch_assoc($result) 
$CommAuth[] .= $row['name']; 
$Comment[] .= $row['message'];
$horde[] .= $row['horde'];
$title[] .= $row['title'];
}
}
}

mysql_connect("localhost","sdfdfssd","sfsdfds"); 

mysql_select_db("ssdfsdfsdf") or die("Unable to select database");

$sql2 = "SELECT * FROM ac_users WHERE username='$title' LIMIT 3";

$result2 = mysql_query($sql2);
$num_rows2 = mysql_num_rows($result2);
if($num_rows2 == 0) {
echo "No results please try a different <a href=reports.php>search 2</a>.";
} else {
$CommAvatar = array();
while($row2 = mysql_fetch_array($result2)) {
$CommAvatar[] .= $row2['avatar']; 

$count = 0;
foreach ($CommAuth as $value){

print "<div id=\"comment\">
<img src=\"/avatar/{$CommAvatar[$count]}\" width=\"56\" height=\"56\" class=\"comment_photo\" />
<div id=\"comment_header\"><span class=\"comment_author\"> {$value} </span><span class=\"comment_wrote\">wrote...</span><span class=\"comment_stars\"><img src=\"stars_5.png\"/></span></div>
<div class=\"comment_text\">{$Comment[$count]}</div>
<div id=\"comment_footer\"><span class=\"comment_delete\">Report | Comment on {$value} Profile | Message {$value}</span></div>
</div>";
$count++;
}
}
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.