Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting error - SQLSTATE[21000]: Cardinality violation: 1241 Operand should contain 1 column(s)

Tags:

php

mysql

I am getting this error SQLSTATE[21000]: Cardinality violation: 1241 Operand should contain 1 column(s) can someone help me understand why?

Here is my code:

<?php


session_start();
$turk_number ='';
$serial='';
$version='';
if(isset($_POST['turk_number']))
{
    $_SESSION['turk_number'] = $_POST['turk_number'];
    $turk_number= $_POST['turk_number'];
}

$hostname = "localhost";
$username = "root";
$password = "";
$dbname = "resolver";

try 
{
    $dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);

    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $query1 = "SELECT ((SELECT * FROM participants ORDER BY serial DESC LIMIT 0, 1) % 10) + 1 as version";
    //$results = $dbh->query($query1);
        $results = $dbh->prepare($query1);
        $results->execute();
        $serial = $results->fetchColumn(0);
    if($test= $results->fetchColumn(1))     
    {
        $version = $test;   //last version on record + 1
    }
    else //if no user ever played any games
    {
        $version = 1;
    }
    $query2 = "INSERT INTO participants (version) VALUES (:version, :turk_number)";
        $stmt = $dbh ->prepare($query2);
        $stmt ->execute(array(':version' => $version,
                            ':turk_number' => $turk_number));

}
catch(PDOException $e)
{
    echo $e->getMessage();
}   

$_SESSION['serial']= $serial;
$_SESSION['version']=$version;


?>
serial <?php print $serial; ?><br />
version <?php print $version; ?><br />
turk <?php print $turk_number; ?>
like image 477
user1015214 Avatar asked Feb 13 '13 22:02

user1015214


1 Answers

you are selecting all columns in inner query and then trying to do %10, it gives you error because you cant %10 to multiple columns. Switch it to select one column instead.

(SELECT * FROM participants ORDER BY serial DESC LIMIT 0, 1) % 10

selects multiple columns and then tries to mod it by 10

like image 186
GGio Avatar answered Oct 19 '22 02:10

GGio