Forum Moderators: coopster

Message Too Old, No Replies

PHP query two different DB.

experts help please

         

rscrsc

8:19 pm on Apr 10, 2006 (gmt 0)

10+ Year Member



Hi I was wondering if someone could please point me in the right direction or maybe give me some other ways to do this if any. I am trying to connect to two different mysql DB's on two different servers. I would like to be able to print both results from each one of the queries.

A users is going to be inserting an IP address which will get put into a $POST variable which we are going to be using in the queries. We would like to search both databases for the IP and print out both results. Can someone help me out I am getting an error stating that [Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource on line 31] line 31 is
while ($row = mysql_fetch_array($result) ).

thanks for the help.

//Connection to both MYSQL databases
$conn1 = mysql_connect($Dragon_host,$Dragon_user,$Dragon_pass) or die (mysql_errno().": ".mysql_error()."<BR>");
$conn2 = mysql_connect($Snort_host,$Snort_user,$Snort_pass) or die (mysql_errno().": ".mysql_error()."<BR>");
//mysql query that is going
//to be performed
$result = mysql_query("select datetime, sensor, signature, inet_ntoa(source), inet_ntoa(dest), sourceport, destport, protocols from dragon where inet_ntoa(source) like '" .$Drgsrc."' or inet_ntoa(dest) like '" .$Drgsrc."'", $conn1);
echo "connected successfully";
while ($row = mysql_fetch_array($result) )
{
$innerresult = mysql_query("select inet_ntoa(ip_src), inet_ntoa(ip_dst), layer4_sport, layer4_dport, timestamp, sig_name, sid, cid from acid_event where inet_ntoa(ip_src) like '" .$Src."' or inet_ntoa(ip_dst) like '" .$Src."'", $conn2);

echo '<tr>';
echo $row['rowname'];
echo '<td align="center">',$row['datetime'],'</td>';
echo '<td align="center">',$row['sensor'],'</td>';
echo '<td align="center">',$row['signature'],'</td>';
echo '<td align="center">',$row['inet_ntoa(source)'],'</td>';
echo '<td align="center">',$row['inet_ntoa(dest)'],'</td>';
echo '<td align="center">',$row['sourceport'],'</td>';
echo '<td align="center">',$row['sig_name'],'</td>';

alce

8:40 pm on Apr 10, 2006 (gmt 0)

10+ Year Member



A good place to start is to check your SQL syntax.

On line 31 you are passing the value of $result to the function mysql_fetch_array()

A few lines avobe, you set the value of $result:

$result = mysql_query("select datetime, sensor, signature, inet_ntoa(source), inet_ntoa(dest), sourceport, destport, protocols from dragon where inet_ntoa(source) like '" .$Drgsrc."' or inet_ntoa(dest) like '" .$Drgsrc."'", $conn1);

Check that the name of the fields from your query match the names on your database. eg. inet_ntoa(source), inet_ntoa(dest) and so on.

Try running the query directly in Mysql command line to see if it returns the desired results.

rscrsc

9:33 pm on Apr 10, 2006 (gmt 0)

10+ Year Member



thanks for the reply. Before I entered the query in PHP I did make sure that it spit out results. I have tested the MYSQL query and I am sure that it is working corrently.

rscrsc

2:31 am on Apr 11, 2006 (gmt 0)

10+ Year Member



If anyone thinks there is a better way to connect to two db on different servers and also run two query in one Else statement please also advice as I am new to PHP

Habtom

6:45 am on Apr 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try the syntax mysql_query('Your Query', $connection);
* Notice the single quotes

Also check your fields - that is where the error seems to come from. Check the query. Try it by querying in your SQL section of your MySQL DB.

Habtom

alce

2:41 pm on Apr 11, 2006 (gmt 0)

10+ Year Member



I believe that the fact that you are connecting to two databases has nothing to do with the error since you are handling each connection independently.

As the error says, there is something wrong with the value of $result. If you double checked the query syntax
then the error must be in the value of $conn1.

$conn1 = mysql_connect($Dragon_host,$Dragon_user,$Dragon_pass) or die (mysql_errno().": ".mysql_error()."<BR>");

It is usally a little mistake or something you are not seeing...try swapping $conn1 and $conn2 for example. You might be sending the query to the wrong database.

whoisgregg

6:20 pm on Apr 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Maybe it's a privileges problem? :)

Birdman

9:17 pm on Apr 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You never selected a database...for either connection.

Try this one:

//Connection
$conn1 = mysql_connect($Dragon_host,$Dragon_user,$Dragon_pass) or die (mysql_errno().": ".mysql_error()."<BR>");
mysql_select_db('conn1_db_name', $conn1);
$conn2 = mysql_connect($Snort_host,$Snort_user,$Snort_pass) or die (mysql_errno().": ".mysql_error()."<BR>");
mysql_select_db('conn2_db_name', $conn2);
//mysql query that is going
//to be performed
$result = mysql_query("select datetime, sensor, signature, inet_ntoa(source), inet_ntoa(dest), sourceport, destport, protocols from dragon where inet_ntoa(source) like '" .$Drgsrc."' or inet_ntoa(dest) like '" .$Drgsrc."'", $conn1);
echo "connected successfully";
while ($row = mysql_fetch_array($result) )
{
$innerresult = mysql_query("select inet_ntoa(ip_src), inet_ntoa(ip_dst), layer4_sport, layer4_dport, timestamp, sig_name, sid, cid from acid_event where inet_ntoa(ip_src) like '" .$Src."' or inet_ntoa(ip_dst) like '" .$Src."'", $conn2);

echo '<tr>';
echo $row['rowname'];
echo '<td align="center">',$row['datetime'],'</td>';
echo '<td align="center">',$row['sensor'],'</td>';
echo '<td align="center">',$row['signature'],'</td>';
echo '<td align="center">',$row['inet_ntoa(source)'],'</td>';
echo '<td align="center">',$row['inet_ntoa(dest)'],'</td>';
echo '<td align="center">',$row['sourceport'],'</td>';
echo '<td align="center">',$row['sig_name'],'</td>';