1

I'm new to MySQL and PHP and am having trouble with this code. Please do tell me where I am going wrong:

Here's the code:

<?php

include('connection.php');

$num1  = '1';
$num2  = '2';

// Get all the data from the  table

$sql = mysql_query("SELECT * FROM table1 WHERE num1 = '$num1' AND num2 = '$num2'");

$row = mysql_fetch_assoc($sql) or die("No rows returned by query");

while ($row = mysql_fetch_assoc($sql)) {
echo $row["num1"];
echo '</br>';
echo $row["num2"];
echo '</br>';
}

?>

If I change

$sql = mysql_query("SELECT * FROM table1 WHERE num1 = '$num1' AND num2 = '$num2'");

to

$sql = mysql_query("SELECT * FROM table1 WHERE num1 > '$num1' AND num2 > '$num2'");

it works. It doesn't work with the equal sign although there are records that should be printed out.

Thank you for your time.

5
  • Please show the schema and (relevant) content of your table1 table. Commented May 11, 2012 at 6:24
  • Is there an error or just no result? If there is an error, show us the error message please Commented May 11, 2012 at 6:25
  • What are the values of the returned rows if you change the query? Commented May 11, 2012 at 6:25
  • I don't believe the quotes are your problem, unless I'm missing something? For instance I can run SELECT * FROM orders WHERE order_id = '81837' AND customer_id = '13'; in a database where both those fields are INTs and it works fine. Can you provide you SCHEMA and any error messages/returned values? Commented May 11, 2012 at 6:29
  • @DKSan If I change the equal sign to greater than I get the results. There are only 5 records now, just numbers between 1 and 10 in all fields. Commented May 11, 2012 at 7:04

3 Answers 3

1

your problem is you are fetching the results twice, so remove the fetch statement outside the while loop**($row = mysql_fetch_assoc($sql) or die("No rows returned by query");)** and it should work. Please update if this works.

Sign up to request clarification or add additional context in comments.

Comments

1

Since you are comparing numbers,try using them without quotes.

SELECT * FROM table1 WHERE num1=$num1 AND num2=$num2

1 Comment

The table's design is as below: serial | type: int| length - 4| Decimals 0|Primary Key| Auto increment num1| type: int| length - 4| Decimals 0| Allow null num2| type: int| length - 4| Decimals 0| Allow null num3 | type: int| length - 4| Decimals 0| Allow null
1

Try again

SELECT * FROM table1 WHERE num1 = $num1 AND num2 = $num2

1 Comment

in any database? or I need to turn on some option?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.