martin9t9 Posted June 8, 2011 Share Posted June 8, 2011 hey guys this is sorta hard to explain so ill give it a go anyways, just bare with me. on my site im retrieving data from a database and what i wanna do is retrieve data from another database but have it in the other database here's the code of what ive got: <?php require_once('auth.php'); ?> <?php $id = $_SESSION['SESS_MEMBER_ID'] ?> <html> <head> <title>Announcement</title> </head> <body> <table border="0" width="100%" height="100%"> <tr height="15%"> <td colspan="5"><?php include "header.php"; ?></td> </tr> <tr> <td> <table border="0" width="100%" height="100%"> <tr> <td width="25%" valign="top"><div valign="top"><?php include "menu.php"; ?></div></td> <td width="50%" valign="top"><center> <center><div class="subheading"><b>All Networks</b></div></center> <?php $host="***"; // Host name $username="***"; // Mysql username $password="***"; // Mysql password $db_name="***"; // Database name $tbl_name="post"; // Table name // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect server "); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM $tbl_name ORDER BY id DESC"; $result=mysql_query($sql); while($rows=mysql_fetch_array($result)){ ?> <table border="0" width="100%"> <tr> <td rowspan="2" width="45" height="45"> </td> <td colspan="2"><a href="view-profile.php?uid=<?php echo $rows['uid'] ?>" class="postname"><? echo $rows['first']; ?> <? echo $rows['last']; ?></a> on <a class="postname"><? echo $rows['network']; ?></a></td> </tr> <?php $network = $rows['network']; $postid = $rows['id']; ?> <tr> <td><div class="post"><? echo $rows['post']; ?></div></td> <td width="45"></td> </tr> <tr> <td colspan="3" class="text" align="right"> <?php if($rows['user'] !== $_SESSION['SESS_USER']) echo '<a href="like-exec.php?postid=' . $rows['id'] . '&url=member-index.php"><img src="resources/tick.png" alt="Like" border="0"></img></a><font class="like"> ' . $host1="***"; // Host name $username1="***"; // Mysql username $password1="***"; // Mysql password $db_name1="***"; // Database name $tbl_name1="rating"; // Table name // Connect to server and select database. mysql_connect("$host1", "$username1", "$password1")or die("cannot connect server "); mysql_select_db("$db_name1")or die("cannot select DB"); $sql="SELECT SUM(yes) as total FROM $tbl_name1 WHERE postid='$postid'"; $result=mysql_query($sql); while($rows=mysql_fetch_array($result)){ echo $rows['total']; } mysql_close(); //close database '</font> - <a href="dislike-exec.php?postid=' . $rows['id'] . '&url=member-index.php&like=' . $rows['no'] . '"><img src="resources/cross.png" alt="Dislike" border="0"></img></a> <font class="dislike"> ' . $rows['no'] . '</font>'; else echo '<font class="like"> ' . $rows['yes'] . '</font> - <font class="dislike"> ' . $rows['no'] . '</font>'; ?> </td> </tr> </table> <hr color="#F0F0F0"> <? } mysql_close(); //close database ?> </center></td> <td width="25%"></td> </tr> </table> </td> </tr> </table> </body> </html> please help with is bugging me so much! Quote Link to comment https://forums.phpfreaks.com/topic/238768-database-help/ Share on other sites More sharing options...
gristoi Posted June 8, 2011 Share Posted June 8, 2011 Are all of the databases stored on the same server? If so you can just use the schema prefix for the data under one connection. ie: select * from database1.table1 Quote Link to comment https://forums.phpfreaks.com/topic/238768-database-help/#findComment-1226879 Share on other sites More sharing options...
martin9t9 Posted June 8, 2011 Author Share Posted June 8, 2011 where could i put this code? Quote Link to comment https://forums.phpfreaks.com/topic/238768-database-help/#findComment-1226880 Share on other sites More sharing options...
gristoi Posted June 8, 2011 Share Posted June 8, 2011 it depends, are all of your database on the same server or not? Quote Link to comment https://forums.phpfreaks.com/topic/238768-database-help/#findComment-1226881 Share on other sites More sharing options...
martin9t9 Posted June 8, 2011 Author Share Posted June 8, 2011 yea they are Quote Link to comment https://forums.phpfreaks.com/topic/238768-database-help/#findComment-1226883 Share on other sites More sharing options...
gristoi Posted June 8, 2011 Share Posted June 8, 2011 <?php require_once('auth.php'); ?> <?php $id = $_SESSION['SESS_MEMBER_ID'] ?> <html> <head> <title>Announcement</title> </head> <body> <table border="0" width="100%" height="100%"> <tr height="15%"> <td colspan="5"><?php include "header.php"; ?></td> </tr> <tr> <td> <table border="0" width="100%" height="100%"> <tr> <td width="25%" valign="top"><div valign="top"><?php include "menu.php"; ?></div></td> <td width="50%" valign="top"><center> <center><div class="subheading"><b>All Networks</b></div></center> <?php $host="***"; // Host name $username="***"; // Mysql username $password="***"; // Mysql password $db_name="***"; // Database name $tbl_name="post"; // Table name // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect server "); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM database1.$tbl_name ORDER BY id DESC"; $result=mysql_query($sql); while($rows=mysql_fetch_array($result)){ ?> <table border="0" width="100%"> <tr> <td rowspan="2" width="45" height="45"> </td> <td colspan="2"><a href="view-profile.php?uid=<?php echo $rows['uid'] ?>" class="postname"><? echo $rows['first']; ?> <? echo $rows['last']; ?></a> on <a class="postname"><? echo $rows['network']; ?></a></td> </tr> <?php $network = $rows['network']; $postid = $rows['id']; ?> <tr> <td><div class="post"><? echo $rows['post']; ?></div></td> <td width="45"></td> </tr> <tr> <td colspan="3" class="text" align="right"> <?php if($rows['user'] !== $_SESSION['SESS_USER']) echo '<a href="like-exec.php?postid=' . $rows['id'] . '&url=member-index.php"><img src="resources/tick.png" alt="Like" border="0"></img></a><font class="like"> ' . $sql2="SELECT SUM(yes) as total FROM database2.$tbl_name1 WHERE postid='$postid'"; $result2=mysql_query($sql2); while($rows=mysql_fetch_array($result2)){ echo $rows2['total']; } mysql_close(); //close database '</font> - <a href="dislike-exec.php?postid=' . $rows2['id'] . '&url=member-index.php&like=' . $rows2['no'] . '"><img src="resources/cross.png" alt="Dislike" border="0"></img></a> <font class="dislike"> ' . $rows2['no'] . '</font>'; else echo '<font class="like"> ' . $rows['yes'] . '</font> - <font class="dislike"> ' . $rows2['no'] . '</font>'; ?> </td> </tr> </table> <hr color="#F0F0F0"> <? } mysql_close(); //close database ?> </center></td> <td width="25%"></td> </tr> </table> </td> </tr> </table> </body> </html> dont forget to replace database1 and database2 with whatever your databases are called Quote Link to comment https://forums.phpfreaks.com/topic/238768-database-help/#findComment-1226889 Share on other sites More sharing options...
martin9t9 Posted June 8, 2011 Author Share Posted June 8, 2011 im getting the error Parse error: syntax error, unexpected T_ELSE in D:\Hosting\7833904\html\announcement\admin\member-index.php on line 72 Quote Link to comment https://forums.phpfreaks.com/topic/238768-database-help/#findComment-1226891 Share on other sites More sharing options...
gristoi Posted June 8, 2011 Share Posted June 8, 2011 you had opened , and not closed a set of brackets, also you had an echo missing: <?php require_once('auth.php'); ?> <?php $id = $_SESSION['SESS_MEMBER_ID'] ?> <html> <head> <title>Announcement</title> </head> <body> <table border="0" width="100%" height="100%"> <tr height="15%"> <td colspan="5"><?php include "header.php"; ?></td> </tr> <tr> <td> <table border="0" width="100%" height="100%"> <tr> <td width="25%" valign="top"><div valign="top"><?php include "menu.php"; ?></div></td> <td width="50%" valign="top"><center> <center><div class="subheading"><b>All Networks</b></div></center> <?php $host="***"; // Host name $username="***"; // Mysql username $password="***"; // Mysql password $db_name="***"; // Database name $tbl_name="post"; // Table name // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect server "); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM database1.$tbl_name ORDER BY id DESC"; $result=mysql_query($sql); while($rows=mysql_fetch_array($result)){ ?> <table border="0" width="100%"> <tr> <td rowspan="2" width="45" height="45"> </td> <td colspan="2"><a href="view-profile.php?uid=<?php echo $rows['uid'] ?>" class="postname"><? echo $rows['first']; ?> <? echo $rows['last']; ?></a> on <a class="postname"><? echo $rows['network']; ?></a></td> </tr> <?php $network = $rows['network']; $postid = $rows['id']; ?> <tr> <td><div class="post"><? echo $rows['post']; ?></div></td> <td width="45"></td> </tr> <tr> <td colspan="3" class="text" align="right"> <?php if($rows['user'] !== $_SESSION['SESS_USER']) { echo '<a href="like-exec.php?postid=' . $rows['id'] . '&url=member-index.php"><img src="resources/tick.png" alt="Like" border="0"></img></a><font class="like"> ' . $sql2="SELECT SUM(yes) as total FROM database2.$tbl_name1 WHERE postid='$postid'"; $result2=mysql_query($sql2); while($rows=mysql_fetch_array($result2)){ echo $rows['total']; } mysql_close(); //close database echo '</font> - <a href="dislike-exec.php?postid=' . $rows['id'] . '&url=member-index.php&like=' . $rows['no'] . '"><img src="resources/cross.png" alt="Dislike" border="0"></img></a> <font class="dislike"> ' . $rows['no'] . '</font>'; } else { echo '<font class="like"> ' . $rows['yes'] . '</font> - <font class="dislike"> ' . $rows['no'] . '</font>'; ?> </td> </tr> </table> <hr color="#F0F0F0"> <? }} mysql_close(); //close database ?> </center></td> <td width="25%"></td> </tr> </table> </td> </tr> </table> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/238768-database-help/#findComment-1226894 Share on other sites More sharing options...
martin9t9 Posted June 8, 2011 Author Share Posted June 8, 2011 sorry but it still didnt work... it showed my webpage but it had a heap of errors in it : SELECT SUM(yes) as total FROM martin9t9.rating WHERE postid='105' Warning: mysql_query() [function.mysql-query]: Can't connect to MySQL server on 'localhost' (10061) in D:\Hosting\7833904\html\announcement\admin\member-index.php on line 67 Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in D:\Hosting\7833904\html\announcement\admin\member-index.php on line 67 Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in D:\Hosting\7833904\html\announcement\admin\member-index.php on line 69 Warning: mysql_close(): no MySQL-Link resource supplied in D:\Hosting\7833904\html\announcement\admin\member-index.php on line 90 Warning: mysql_close(): no MySQL-Link resource supplied in D:\Hosting\7833904\html\announcement\admin\member-index.php on line 74 Quote Link to comment https://forums.phpfreaks.com/topic/238768-database-help/#findComment-1226896 Share on other sites More sharing options...
gristoi Posted June 8, 2011 Share Posted June 8, 2011 try removing the line: mysql_select_db("$db_name")or die("cannot select DB"); Quote Link to comment https://forums.phpfreaks.com/topic/238768-database-help/#findComment-1226897 Share on other sites More sharing options...
martin9t9 Posted June 8, 2011 Author Share Posted June 8, 2011 the same things happening... its also echoing ' SELECT SUM(yes) as total FROM martin9t9.rating WHERE postid='125' ' Quote Link to comment https://forums.phpfreaks.com/topic/238768-database-help/#findComment-1226899 Share on other sites More sharing options...
gristoi Posted June 8, 2011 Share Posted June 8, 2011 re post your code Quote Link to comment https://forums.phpfreaks.com/topic/238768-database-help/#findComment-1226902 Share on other sites More sharing options...
martin9t9 Posted June 8, 2011 Author Share Posted June 8, 2011 just to let you know it says it cant connect to the server on 'localhost' mines not local host its an IP address not sure if this helps. <?php require_once('auth.php'); ?> <?php $id = $_SESSION['SESS_MEMBER_ID'] ?> <html> <head> <title>Announcement</title> </head> <body> <table border="0" width="100%" height="100%"> <tr height="15%"> <td colspan="5"><?php include "header.php"; ?></td> </tr> <tr> <td> <table border="0" width="100%" height="100%"> <tr> <td width="25%" valign="top"><div valign="top"><?php include "menu.php"; ?></div></td> <td width="50%" valign="top"><center> <center><div class="subheading"><b>All Networks</b></div></center> <?php $host="1*2.*0.1*3.2*"; // Host name $username="martin9t9"; // Mysql username $password="***"; // Mysql password $db_name="martin9t9"; // Database name $tbl_name="post"; // Table name // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect server "); $sql="SELECT * FROM martin9t9.$tbl_name ORDER BY id DESC"; $result=mysql_query($sql); while($rows=mysql_fetch_array($result)){ ?> <table border="0" width="100%"> <tr> <td rowspan="2" width="45" height="45"> </td> <td colspan="2"><a href="view-profile.php?uid=<?php echo $rows['uid'] ?>" class="postname"><? echo $rows['first']; ?> <? echo $rows['last']; ?></a> on <a class="postname"><? echo $rows['network']; ?></a></td> </tr> <?php $network = $rows['network']; $postid = $rows['id']; ?> <tr> <td><div class="post"><? echo $rows['post']; ?></div></td> <td width="45"></td> </tr> <tr> <td colspan="3" class="text" align="right"> <?php if($rows['user'] !== $_SESSION['SESS_USER']) { echo '<a href="like-exec.php?postid=' . $rows['id'] . '&url=member-index.php"><img src="resources/tick.png" alt="Like" border="0"></img></a><font class="like"> ' . $sql2="SELECT SUM(yes) as total FROM martin9t9.rating WHERE postid='$postid'"; $result2=mysql_query($sql2); while($rows=mysql_fetch_array($result2)){ echo $rows['total']; } mysql_close(); //close database echo '</font> - <a href="dislike-exec.php?postid=' . $rows['id'] . '&url=member-index.php&like=' . $rows['no'] . '"><img src="resources/cross.png" alt="Dislike" border="0"></img></a> <font class="dislike"> ' . $rows['no'] . '</font>'; } else { echo '<font class="like"> ' . $rows['yes'] . '</font> - <font class="dislike"> ' . $rows['no'] . '</font>'; ?> </td> </tr> </table> <hr color="#F0F0F0"> <? }} mysql_close(); //close database ?> </center></td> <td width="25%"></td> </tr> </table> </td> </tr> </table> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/238768-database-help/#findComment-1226904 Share on other sites More sharing options...
gristoi Posted June 8, 2011 Share Posted June 8, 2011 line 62. youve got a concatenator on the end of the line. should be: echo '<a href="like-exec.php?postid=' . $rows['id'] . '&url=member-index.php"><img src="resources/tick.png" alt="Like" border="0"></img></a><font class="like"> '; Quote Link to comment https://forums.phpfreaks.com/topic/238768-database-help/#findComment-1226905 Share on other sites More sharing options...
martin9t9 Posted June 8, 2011 Author Share Posted June 8, 2011 ok it worked on the first post that shows up but everyone after that it says: Warning: mysql_query() [function.mysql-query]: Can't connect to MySQL server on 'localhost' (10061) in D:\Hosting\7833904\html\announcement\admin\member-index.php on line 66 Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in D:\Hosting\7833904\html\announcement\admin\member-index.php on line 66 Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in D:\Hosting\7833904\html\announcement\admin\member-index.php on line 68 Warning: mysql_close(): no MySQL-Link resource supplied in D:\Hosting\7833904\html\announcement\admin\member-index.php on line 73 and right at the bottom it says: Warning: mysql_close(): no MySQL-Link resource supplied in D:\Hosting\7833904\html\announcement\admin\member-index.php on line 89 Quote Link to comment https://forums.phpfreaks.com/topic/238768-database-help/#findComment-1226906 Share on other sites More sharing options...
gristoi Posted June 8, 2011 Share Posted June 8, 2011 its more than likely because the sql is in a loop and referencing the same sql connection. try: <?php require_once('auth.php'); ?> <?php $id = $_SESSION['SESS_MEMBER_ID'] ?> <html> <head> <title>Announcement</title> </head> <body> <table border="0" width="100%" height="100%"> <tr height="15%"> <td colspan="5"><?php include "header.php"; ?></td> </tr> <tr> <td> <table border="0" width="100%" height="100%"> <tr> <td width="25%" valign="top"><div valign="top"><?php include "menu.php"; ?></div></td> <td width="50%" valign="top"><center> <center><div class="subheading"><b>All Networks</b></div></center> <?php $host="1*2.*0.1*3.2*"; // Host name $username="martin9t9"; // Mysql username $password="***"; // Mysql password $db_name="martin9t9"; // Database name $tbl_name="post"; // Table name // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect server "); $sql="SELECT * FROM martin9t9.$tbl_name ORDER BY id DESC"; $result=mysql_query($sql); $i = 0; while($rows=mysql_fetch_array($result)){ ?> <table border="0" width="100%"> <tr> <td rowspan="2" width="45" height="45"> </td> <td colspan="2"><a href="view-profile.php?uid=<?php echo $rows['uid'] ?>" class="postname"><? echo $rows['first']; ?> <? echo $rows['last']; ?></a> on <a class="postname"><? echo $rows['network']; ?></a></td> </tr> <?php $network = $rows['network']; $postid = $rows['id']; ?> <tr> <td><div class="post"><? echo $rows['post']; ?></div></td> <td width="45"></td> </tr> <tr> <td colspan="3" class="text" align="right"> <?php if($rows['user'] !== $_SESSION['SESS_USER']) { echo '<a href="like-exec.php?postid=' . $rows['id'] . '&url=member-index.php"><img src="resources/tick.png" alt="Like" border="0"></img></a><font class="like"> '; $sql.$i="SELECT SUM(yes) as total FROM martin9t9.rating WHERE postid='$postid'"; $result.$i=mysql_query($sql.$i); while($rows.$i=mysql_fetch_array($result.$i)){ echo $rows.$i['total']; } mysql_close(); //close database echo '</font> - <a href="dislike-exec.php?postid=' . $rows.$i['id'] . '&url=member-index.php&like=' . $rows.$i['no'] . '"><img src="resources/cross.png" alt="Dislike" border="0"></img></a> <font class="dislike"> ' . $rows.$i['no'] . '</font>'; } else { echo '<font class="like"> ' . $rows.$i['yes'] . '</font> - <font class="dislike"> ' . $rows.$i['no'] . '</font>'; ?> </td> </tr> </table> <hr color="#F0F0F0"> <? }} mysql_close(); //close database ?> </center></td> <td width="25%"></td> </tr> </table> </td> </tr> </table> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/238768-database-help/#findComment-1226907 Share on other sites More sharing options...
martin9t9 Posted June 8, 2011 Author Share Posted June 8, 2011 ok didnt work its showing up ALOT of Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in D:\Hosting\7833904\html\announcement\admin\member-index.php on line 68 it just keeps loading more and more and isnt showing any of my posts anymore Quote Link to comment https://forums.phpfreaks.com/topic/238768-database-help/#findComment-1226908 Share on other sites More sharing options...
martin9t9 Posted June 9, 2011 Author Share Posted June 9, 2011 are you there gristoi? Quote Link to comment https://forums.phpfreaks.com/topic/238768-database-help/#findComment-1227286 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.