简体   繁体   中英

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. 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, 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. 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 =" . $numb . "WHERE question ='" . $a . "'"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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