简体   繁体   English

有关MySQL查询的正确语法的帮助

[英]Help on correct Syntax for a MySQL Query

I really could not understand what I am missing then I thought it would nice to ask you guys. 我真的不明白我想念的是什么,于是我想问问你们会很好。 Here is my code sample. 这是我的代码示例。

    foreach ($disArray as $a) {

        $query = "SELECT num FROM ".$tableName." WHERE question='" . $a."'";
        $result = mysql_query($query, $this->conn) or die('Error: '.mysql_error());
        $row = mysql_fetch_array($result);
        //$row['num'] = $row['num'] + 1;

        $numb = $row['sayi'] + 1;

        $query = "UPDATE ".$tablename." SET `num`=" . $numb . "WHERE `question`=" . $a . "\"";
        mysql_query($query, $this->conn);
    }

Here disArray is an array :) And I am looping through it and it stores "senteces" in it. 这里disArray是一个数组:)我遍历了它,并在其中存储了“ senteces”。 What I am trying to do is comparing those sentence with the ones in the table but I guess I have an error in my this $query = "SELECT num FROM ".$username." WHERE question='" . $a."'"; 我想做的就是将那些句子与表中的句子进行比较,但是我猜我在$query = "SELECT num FROM ".$username." WHERE question='" . $a."'"; $query = "SELECT num FROM ".$username." WHERE question='" . $a."'"; query, as it gives and error like this; 查询,因为它给出了这样的错误;

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE question='Where are you from?'' at line 1

Also I was getting an error that says mysql_fetch_array requires resource but you are giving booling etc. But I do not know what it stops giving that error as well. 我也收到一条错误消息,提示mysql_fetch_array需要资源,但您却在进行嘘声等。但是我不知道它也停止发出该错误。 What do you think guys? 你们觉得怎么样? What would be the problem? 有什么问题吗? Thanks in advance! 提前致谢!

Instead of: 代替:

$query = "SELECT num FROM ".$tableName." WHERE question='" . $a."'";

Use this: 用这个:

$query = ' SELECT `num` FROM `'.$tableName.'` WHERE `question` = "'.$a.'" ';

而不是$ username,您应该提供数据库表名来检查

$query = "SELECT num FROM  '".$username."' WHERE question='" . $a."'";

显然问题出在您的用户名变量中,请检查该值是否设置正确。

I recomend add quote as shown below! 我建议添加报价,如下所示!

$query = "UPDATE ".$tablename." SET num =" . $ query =“ UPDATE”。$ tablename。“ SET num =”。 $numb . $ numb。 "WHERE question ='" . question哪里='”。 $a . $ a。 "'" “”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM