English 中文(简体)
其中两项声明[重复]
原标题:Two WHERE statements [duplicate]
  • 时间:2012-05-26 14:52:07
  •  标签:
  • php
  • mysql
This question already has answers here:
Closed 11 years ago.

Possible Duplicate:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result

我搜索了一下 找不到任何能特别帮助我的东西

我希望你能帮忙

我想执行 MySQL 查询, 该查询为符合两个标准( 类型 = 绿色 和 = 是) 的条目搜索表格。

我收到了:警告: Mysql_fetch_array (): 提供的参数不是一个有效的 MySQL 的结果资源, / link/ to/my/ file.php 载于第36行

以下是代码摘要(第36行):

`$green = "SELECT * FROM homepage_vars WHERE type =  green  AND on =  yes ";
 $green = mysql_query($green);
 $green = mysql_fetch_array($green);`
问题回答

ON < ON is a MySQL 保留关键字 。如果使用它作为列或表格标识符,您需要将其以后引号附加 :

SELECT * FROM homepage_vars WHERE type =  green  AND `on` =  yes 

一旦查询语法被更正, 你就会出现另一个问题 。 您已经覆盖了变量 < code>$green 几次 。 最初, 它控制了您的查询 SQL, 但后来被用于查询结果资源 。 罚款, 但是随后您会用 < code> mysql_ fetch_ array () 所获取的行覆盖它, 其内容将是数组或 < code> FALSE 。 之后获取行的尝试将会失败, 因为 < code> $ green 不再是一个结果资源 。

在试图获取行前总是测试查询是否成功或失败。 呼叫 mysql_error () 以查看 MySQL 服务器报告的错误, 这将会指向 < em> 无效语法在 < em > 或类似的 < em > 或类似的词典附近 。

$green = "SELECT * FROM homepage_vars WHERE type =  green  AND on =  yes "; 
$query = mysql_query($green); 
if ($query) {
  // Don t overwrite your query resource!
  // Use a new variable!
  $row = mysql_fetch_array($query);
}
else {
  // Failure!
  echo mysql_error();
}




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...