2

I've got a php script with this statement:

$query = "SELECT type 
            FROM users 
           WHERE username = '$username'";

$result = $database->query($query);
if($result == 1) {
  echo "whatever";
  continue;
}

The problem is that the if never runs and when I created a print statement to print $result before the if runs, it prints a Reference ID#. So result is never == 1 because it is being assigned the reference ID #.

What the heck am I doing wrong? How do I assign the value of 'type' which is an INT, instead of it's contents Reference ID#?

3
  • What is $database is it your own custom object, amysqli connection, something else? Commented Jul 13, 2011 at 18:10
  • $database is an object used to open a connection to the database Commented Jul 13, 2011 at 19:36
  • Thank you all for your quick assistance! Commented Jul 13, 2011 at 19:51

4 Answers 4

7

you have to fetch that line first ...

$query = "SELECT type FROM users WHERE username = '$username'";
$result = $database->query($query);
$row = mysql_fetch_assoc($result);
if($row['type'] == 1)
{
      echo "whatever";
}
Sign up to request clarification or add additional context in comments.

3 Comments

I tried this and got an error stating: PHP Fatal error: Cannot break/continue 1 level
it's not my fault, I thought you got it in for or something like that. Answer edited ;)
hehe didn't mean to point fingers, I thought that continue was supposed to be there. I'm giving this a try, thank you.
3

You need to use mysql_fetch_assoc() or mysql_fetch_array() to get the result into an array:

$query = $database->query("SELECT type FROM users WHERE username = '$username'");
$result = mysql_fetch_assoc($query);

if($result['type'] == 1)
{
    echo "whatever";
    continue;
}

Comments

2

try

<?php
$result = mysql_fetch_assoc($database->query($query));
$id = $result['type'];
if($type == 1)
{

Comments

0

Yes. You have to check it like:

$query = "SELECT type FROM users WHERE username = '$username'";
$result = $database->query($query);
if($result)
{
    echo "whatever";
    continue;
}

This is a Boolean check, but... In php we have weak types - everything is true, except on false, 0, '' (empty string), null, etc.

If you want to make a good Boolean check including type, then you have:

if(bool_val === true)
{
..
}
// or
if(bool_val && is_bool(bool_val))
{
..
}

This query object returns a result resource in php. And you probably receive #Resource ID, which is some kind of pointer in php code.

If you receive something like this (#Resource ID) - this means your query has passed correctly and no error occured.

Comments

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.