duan2891 2016-05-26 01:49
浏览 87

使用PHP在mysql数据库中搜索重复的user_id

I'm trying to do something that I think should be pretty basic, but I'm not having any luck. I want to create a unique user id for each user. I have the code written for creating a random, 7-digit number, and I need to just check the number against the database to see if that user id already exists (unlikely since I'm using a 7-digit number, but I have to check anyway).

Here's the relevant portion of the code:

 <?php

require("config.inc.php");

$test_number = "1234567"; 

$query = mysql_query("SELECT user_id FROM users WHERE user_id ='$test_number'");

echo $test_number; 

if(mysql_num_rows($query) != 0){
   echo "
 number already exists.";
}else{
   echo "
 number not found"; 
}

?>

I'm just using test number as a placeholder. I've manually added the number 1234567 to my database, but every time I run the code, the second prong of the if-statement gets called, so the out put is always "1234567 number not found" even though that number does exist for a user in my database.

The require ("config.inc.php") line works because I have other .php files that use that same file and connect to the database just fine, so I don't think it's a connection issue.

  • 写回答

1条回答 默认 最新

  • doudong4532 2016-05-26 03:54
    关注

    Please try with code

    <?php
    
    $link = mysql_connect('localhost', 'root', '');
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    }
    echo 'Connected successfully';
    
     mysql_select_db('tsg_userlogin_dev', $link) or die('Could not select database.');
    
    $test_number = "88888"; 
    
    $query = mysql_query("SELECT EmployeeId FROM tsguser WHERE EmployeeId ='$test_number'");
    
    echo $test_number; 
    
    if(mysql_num_rows($query) != 0){
       echo "
     number already exists.";
    }else{
       echo "
     number not found"; 
    }

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算