duandie0884 2011-01-24 08:58
浏览 278
已采纳

MySQL查询枚举

I have been working on this for a while now, I know it's simpler than what I am making it, but I just can't get it. I have some code where I am trying to query an enum either 1 or 0 from my table so this is exactly what I have to do this.

$username = 'test'
$passResult = mysql_query("SELECT usrSetPass FROM members WHERE usr='.$username.'");

Now I have all the connection stuff down I think, I get no errors there, but when I print this thing out in my echo I get this,

Heres my echo:

echo 'Hello, '.$username.', you Result is: '.$passResult.'!';

What I want to get is:

Hello, test, your Result is: 1

or 

Hello, test, your Result is: 0

Now what I get is:

Hello, test, your Result is: Resource id #6

Now no matter what I do I get the same thing, I have no idea what I'm doing wrong here guys if someone could point this out that would be awesome. What this enum is being use essentially for a boolean just to see if the user has personally set a password not the computer generated version.

  • 写回答

2条回答 默认 最新

  • doufu2396 2011-01-24 09:02
    关注

    mysql_query returns a result resource, essentially a pointer to the memory where the results are buffered. That result set can contain many rows, as you can select many rows, so you need to fetch the row(s) you want then the column(s) you want from those rows.

    /* execute the query and get a result resource back */
    $passResult = mysql_query("SELECT usrSetPass FROM members WHERE usr='" . mysql_real_escape_string($username) . "'");
    
    /* retrieve the first row from $passResult */
    $row = mysql_fetch_assoc($passResult); 
    
    /* assign the usrSetPass column's value from that row to $passed */
    $passed = $row['usrSetPass']; 
    

    Also, your query is wrong. You enclosed it in double quotes, so you're not actually breaking out of the string and concatenating $username when you use the single quotes and dots inside. I've corrected it above.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 arduino控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥85 maple软件,solve求反函数,出现rootof怎么办?
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题