English 中文(简体)
MySQL从方案角度选择数据
原标题:select data from MySQL programmatically
  • 时间:2012-01-13 15:08:47
  •  标签:
  • php
  • mysql

我在MySQL中列出了数据储存情况:

table_1 
+-------+-------+  
|field_0|field_1|
+-------+-------+  
| WCON  |  A    |
+-------+-------+
| KROGR |  B    |
+-------+-------+
|       |  C    |
+-------+-------+
| HALL  |  D    |
+-------+-------+
| OZARK |  E    |
+-------+-------+
|       |  F    |
+-------+-------+
| MAR   |  G    |
+-------+-------+
| KROGR |  H    |
+-------+-------+
| MAR   |  I    |
+-------+-------+

有一些领域在实地具有空洞价值:0。

我的法典在php上运行。 有一种下降的菜单,所有外地名称都用手工方式编制,而菜单的数值是“所有......”、“......”、“......”、“......”、“......”、“......”。 如果选择“海法”,则从下降的菜单中显示G和I,但在选择“所有”时,需要显示所有外地价值1。

我的法典同样如此。

$field_value = "MAR";
$query = "select field_1 from table_1 where field_0 =  $field_value   ";

但是,我如何利用美元点——价值变量——选择所有价值?

最佳回答

只有在以下情况下才添加WHERE条款:$field_ Value/code>并非全部。 此外,确保你使用户对询问的任何投入更加灵敏。 即使从下降开始,如果价值从另一页移至网页,用户也能够在那里插入坏值。

$query = "SELECT field_1 FROM table_1";

if ($field_value != "all")
  $query .= " WHERE field_0 =  ". mysql_real_escape_string($field_value) ." ";
问题回答

添加变数,如:

mysql_query("
SELECT DISTINCT 
c.id AS courseid, 
".$entityString.", 
c.fullname,
c.shortname, 
c.startdate, 
u.id as userid, 

FROM mdl_course c
join mdl_course_categories ct on ct.id = c.category

".$whereClause." AND rl.id = 5 ".$orderClause."
");

通知“常识......”、“......美元”和“......美元”是所有变量。





相关问题
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 ...