开发者

PHP/MYSQL Query returning reference ID instead of INT value

开发者 https://www.devze.com 2023-03-19 22:07 出处:网络
I\'ve got a php script with this statement:开发者_运维技巧 $query = \"SELECT type FROM users WHERE username = \'$username\'\";

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#?


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";
}


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;
}


try

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消