We help IT Professionals succeed at work.

PHP MYSQL Whats wrong with this code?

NeoAshura
NeoAshura asked
on
208 Views
Last Modified: 2012-05-11
Hi there experts,

I have this code, (see attached) and it does not work how its intended to work.

I have a table called "marks data" this is the correct source of data.

I need to compare what is in "marks data" to what is in "o2 data" and see if the mobile numbers in "marks data" are also in "o2's data".

o2data is also a table in the database. All i need to do is find out which numbers are in marksdata but NOT in o2data.

My code does not seem to work it just produces a table with no data in it.

see below. for image.


<?php 
error_reporting (E_ALL ^ E_NOTICE);
include 'myphp2.php';







$query = "SELECT * 
FROM marksdata
LEFT JOIN o2data ON marksdata.MMobile_number = o2data.OMobile_number
WHERE o2data.OMobile_number IS NULL 
OR marksdata.MMobile_number <> o2data.OMobile_number";

$result = mysql_query($query) or die(mysql_error() . " IN $query");

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

echo "<table border='1'>";
echo "<tr> <th>Marks Mobile</th> <th>Elaines Mobile</th> </tr>";
// Print out result
while($row = mysql_fetch_array($result)){
	

$marks_number=$row['MMobile_number'];
$elaines_number=$row['OMobile_number'];

	
	// Print out the contents of each row into a table
echo "<tr><td>"; 
	echo "".$row['MMobile_number'];
	echo "</td><td>"; 
	echo "".$row['OMobile_number'];
	echo "</td>"; 
	
}

   ?>

Open in new window

Untitled.png
Comment
Watch Question

The above query will not work as you expect.

You need to get the resul of the left join and chack for o2data.OMobile_number = null in php or use sub query

Author

Commented:
Hi thanks for your reply, How would i go about doing that?

Sorry im still learning php only been doing it for about 1 year.
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
ok getting there that returns all the data. which matches.

I need all the data that does not match.

The query you provided returns all data where the mobile numbers are the same. I need the mobile numbers returning which EXIST in MarksDATA but DON'T EXIST in the o2 data is that possible?

Author

Commented:
Ive even tried the following,

which returns 65 pages of data which i know is wrong otherwise there would be serious problems.

SELECT * FROM marksdata
WHERE Mmobile_number NOT IN (SELECT Omobile_number FROM o2data)

UNION

SELECT * FROM o2data
WHERE Omobile_number NOT IN (SELECT Mmobile_number FROM marksdata)
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Yes that is the case. I think i need to see my manager.

The reason why i dont think this code is correct And it proberly is correct is that it returns nearly 1800 records and if that is the case then they are looseing a serious amount of money.

I will close this question. I think its not the code at question but the data source.

many thanks
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.