1

在浏览帖子并使用 Google 之后,我想我已经掌握了答案的基本组成部分,尽管由于缺乏 MySQL 经验,我不完全确定如何将它们组合在一起。

---Edit Begin (2013/10/16@1110GMT--- 下面是代码当前的样子:

    $username="username"; $password="password"; $database="database";
    mysql_connect(localhost,$username,$password);@mysql_select_db($database) or die(
    "Unable to select database");

    $needle = 'sdfsdf';

    $checkUserID = mysql_query("SELECT * FROM order_option WHERE value LIKE '%$needle%' ");
    if (!$checkUserID) {
        die('Query failed to execute for some reason');
    }

    if (mysql_num_rows($checkUserId) > 0) {
        echo "<p>User id exists already.</p>";
        $user = mysql_fetch_array($checkUserId);
        print_r($user); // the data returned from the query
    }
    mysql_close();

我想知道我是否误解了 MySQL,尽管我知道应该从这个 MySQL TRUNCATE TABLE 中提取查询order_option

    INSERT INTO `order_option` (`order_option_id`, `order_id`, `order_product_id`, `product_option_id`, `product_option_value_id`, `name`, `value`, `type`) VALUES ('13', '2', '2', '237', '0', 'Additional Information', 'sdfsdf', 'textarea');
    INSERT INTO `order_option` (`order_option_id`, `order_id`, `order_product_id`, `product_option_id`, `product_option_value_id`, `name`, `value`, `type`) VALUES ('14', '2', '2', '228', '0', 'Website', 'fsdfsd', 'text');

---编辑结束---

脚本如下:

$user="user"; $password="password"; $database="mydatabase";
mysql_connect(localhost,$user,$password);
@mysql_select_db($database) or die("Unable to select database");

$needle = 'sdfsdf';

$checkUserID = mysql_query("SELECT value from order_option WHERE value = '$needle'");
if (!$checkUserID) {
    die('Query failed to execute for some reason');
}

if (mysql_num_rows($checkUserId) > 0) {
    echo "User id exists already.";
    $user = mysql_fetch_array($checkUserId);
    print_r($user); // the data returned from the query
}

mysql_close();

如果上面的代码是正确的,那么我需要知道如何使用它

if ($result == $needle) {
    echo('found'); 
} else { 
    echo('not found'); 
}
4

1 回答 1

0

您的代码已经测试了要找到的 $needle

if (mysql_num_rows($checkUserId) > 0) {

这条小线查找与您的查询匹配的结果数。如果有超过 0 个(换句话说:如果找到匹配项!)它会继续。

为了满足您的要求:

if (mysql_num_rows($checkUserId) > 0) 
  {
  echo "found";
  {
else
  {
  echo "not found";
  }
于 2013-10-16T09:31:44.407 回答