English 中文(简体)
PHP & MySQL query Value question
原标题:PHP & MySQL query value question
  • 时间:2010-05-28 04:15:07
  •  标签:
  • php
  • mysql

在我进行第二次盘问之后,我又如何使用第一种盘问值<代码>。 下面是我试图做的事情的样本。

我希望我对此作了解释。

这里是法典。

    $mysqli = mysqli_connect("localhost", "root", "", "sitename");
    $dbc = mysqli_query($mysqli,"SELECT users.*
                                 FROM users
                                 WHERE user_id = 4");

    if (!$dbc) {
        // There was an error...do something about it here...
        print mysqli_error($mysqli);
    }

    while($row = mysqli_fetch_assoc($dbc)) {

     echo  <div>User:   . $row[ id ] .  </div> ;
     echo  <div>Link To User  . $row[ id ] .  </div> ;


            $mysqli = mysqli_connect("localhost", "root", "", "sitename");
            $dbc2 = mysqli_query($mysqli,"SELECT COUNT(cid) as num
                                          FROM comments
                                          WHERE comments_id = $row[id]");

            if (!$dbc2) {
                // There was an error...do something about it here...
                print mysqli_error($mysqli);
            }  else {
                while($row = mysqli_fetch_array($dbc2)){ 
                    $num = $row[ num ];
                }
            }

     echo  <div>User   . $row[ id ] .   Comments#   . $num .  </div> ;

    }
问题回答

Uh.... 所以,你为什么不只使你的第二个“增长”变成其他变化? 美元增长2 我很想一想你们的问题......

你们每次都看到 que问——你们都知道你做错事。 无例外。

你的问询必须如此(我不敢肯定一栏名称,但希望你们有想法):

$sql = "SELECT users.*, COUNT(cid) as num 
    FROM users LEFT JOIN comments ON comments_id = id 
    WHERE user_id = 4
    GROUP BY cid";
$dbc = mysqli_query($sql);
if (!$dbc) trigger_error(mysqli_error($mysqli).$sql);

(注:我首先对一个变数提出疑问。) 这对走私目的来说非常重要。 这样做的方法

虽然我根本不理解第一 lo的含义。 您的首批问是否只回到一行? 为什么会 lo? 只是一行,然后用于产出。

And your main problem, as Jan noted in comments, is you reopen a database connection. While it must be opened only once. You don t have to connect to the database every time you run a query. Do it only once.
What is the book you re learning from?

简而言之,我避免使用通用变量,例如通用变量和通用变量,以及这种理由——你是:你是到这里来的,而你是这样,而且,一般而言,具有良好惯例的变量,如:

而且,@Keyo关于使用分局的点将有助于大大减少你对数据库的询问。

I think you are asking the wrong question here. Yes you could use $row[ id ] in the nested query. You can concatenate it to the query string using the . operator.

$dbc2 = mysqli_query($mysqli,"SELECT COUNT(cid) as num
                                      FROM comments
                                      WHERE comments_id = ".$row[ id ]);

然而,这是可怕的。 你们也应只使用一条连接线,把变数存放在中央。

你们应该把问询变成这样的东西(没有测试):

SELECT 
  users.*, 
  (SELECT count(*) 
  FROM comments 
  WHERE comments.user_id = users.user_id) AS  comment_count 
FROM users
LEFT JOIN comments ON 
  comments.user_id = user.user_id
WHERE users.user_id = 4;

By adding a select statement as the count field you are getting everything you want in a single query. That should avoid all those ugly loops. If you are doing these queries inside loops it will be very slow and there is usually a way to do the same thing in one more complex query.

我不知道您的表格的结构,因此我将假设,id实际上是user_id。 既然我相信你正试图将用户与其意见联系起来。 难道你会犯错误,重写该法典,把它放在这里? 无论如何,我的最后一点将以这些假设为基础。

  1. 无需与同一数据库重新连接。

  2. 你们的第二种选择是,不要超过美元增长。 而是使用2美元防止头痛。

  3. 如上文所述,你可在与LEFT JOIN或分局的单一查询中做所有工作。

  4. 阁下的第一项问答是一份单一的记录,因此没有必要通过记录。

  5. 这里是你完全改写的法典,没有经过测试,假设你想要根据我在第一段中的假设显示用户数据。

    $mysqli = mysqli_connect("localhost", "root", "", "sitename"); $dbc = mysqli_query($mysqli,"SELECT users.*, COUNT(cid) AS num FROM users LEFT JOIN comments ON comments_id = user_id WHERE user_id = 4 GROUP BY users.user_id");

    if (!$dbc) { // There was an error...do something about it here... print mysqli_error($mysqli); }

    if($row = mysqli_fetch_assoc($dbc)){ echo User: . $row[ user_id ] . ; echo Link To User . $row[ user_id ] . ; echo User . $row[ user_id ] . Comments# . $num . ; }

下次请你提供桌椅结构,以便我们能够更准确地解决你的问题。

Quick Reference: Using Quotes

我将在这里解释一下,因为我看到你当中有些人犯了同样错误,在你对这个职位的答复中多次引用。

这正是你在引用时所说的话:

Single Quotes: Do not parse here.
Double Quotes: Parse the contents inside the quotes, please.
No Quotes: This is a constant or number, please parse if it s a constant only.

当你与阵列重新工作时,如果对方括号内的内容不作区分,就应当单独引用。 如果有数字或固定数字的话,你可以放弃报价。 例如:

// If your string is double quoted, you use regular brackets to encapsulate the array
$sql = "SELECT * FROM tableA WHERE id = {$myarray[ id ]}";

或者,还有另一种类似做法:

$sql = "SELECT * FROM tableA WHERE id = ".$myarray[ id ];

也可以写成:

$sql =  SELECT * FROM tableA WHERE id =  .$myarray[ id ];

另一个例子:

// This is correct
echo $myarray[5];

// If you are using constants, this is correct too
define( MYCONSTANT , 5);  
echo $myarray[MYCONSTANT];

// This shouldn t be used, because  name  isn t a constant...
echo $myarray[name];

// Use this instead...
echo $myarray[ name ];

// Or, like I explained above, escape the array
echo "My name is {$myarray[ name ]}";

希望会有所助益。





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

热门标签