0

edit: my table schema below is poorly normalized as suggested and I have revamped it.

schema

11
  • What happens when you run the query? Commented Mar 28, 2012 at 12:59
  • 2
    wait, why is there varieble after select? what is the column you are trying to get? Do you have special column for every user? Commented Mar 28, 2012 at 13:01
  • It tells me that "It's NULL!". I tried to echo back ($result) but it gives me nothing at all? Commented Mar 28, 2012 at 13:04
  • I am using PHP to write these SQL statements. The variables are values extracted from an android form and that side works, as do the variables. My SQL, though, isn't very good and I think that is where the error is coming from. Commented Mar 28, 2012 at 13:06
  • can you please add your table structure, i believe problem is wrong query Commented Mar 28, 2012 at 13:08

2 Answers 2

2

Try to use mysql_num_rows() in your condition

if (mysql_num_rows($result) == 0) {
    echo (" It's NULL!");
    // if null, run a query!
} else {
echo (" It's not null...");
// if not null, echo back meessage saying not null.
}

EDIT: nevermind

EDIT2: Gosh, I see it now, try

$row = mysql_fetch_assoc($result);

if ($row[$userid] == NULL) {
    echo (" It's NULL!");
    // if null, run a query!
} else {
echo (" It's not null...");
// if not null, echo back meessage saying not null.
}
Sign up to request clarification or add additional context in comments.

4 Comments

weirdly, i've made each individual user have individual columns so that i can store their question responses in each one. Check this image of my table to see if you get what I mean... s21.photobucket.com/albums/b259/acidburn942/…
Your last example will probably work, but this is a really bad idea to store users in columns. OP: You might want to reconsider your data schema because this is badly normalized and not scalable at all.
I know, just trying to solve the current problem, not even pointing out possible sql injection vulnerability. Redesigning that db would be best solution
Pierre-OlivierBourgeois and Uriel_SVK thank you very much for both your input. Uriel's solution does now solve the problem but I am always learning! I will look more into my database structure and try to make it better. Thank you for the tips Pierre!
1

Try using mysql_num_rows() for checking how many rows are returned,

if (mysql_num_rows($result) == 0)
{
    echo (" It's NULL!");
    // if null, run a query!
}
else
{
    echo (" It's not null...");
    // if not null, echo back meessage saying not null.
}

Also, what column are you trying to select ?

4 Comments

here is my table structure: s21.photobucket.com/albums/b259/acidburn942/… the column i am trying to select is userid, to check if it is null according to a certain question id....
You used a column per user ? This is poorly normalized. Take a look at this: asciiflow.com/#5355087028660164263/2004018477
And then, you could do a SELECT COUNT(1) from userAnswers WHERE id = $qid AND userid = $userid which will return you the number of rows for this specific condition.
Thank you very much, this is my first attempt at a questions/answer database. I will look more into my database structure and try to improve it! :)

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.