CodeIgniter Forums

Full Version: Query Bindings driving me crazy
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]skunkbad[/eluser]
Getting this error:

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 '? LIMIT 1' at line 3

SELECT `some-field` FROM `some-table` WHERE `other-field` = ? LIMIT 1

Code:
public function get_field_from_other( $name )
{
    // query database for readable name
    $sql = $this->db->query('SELECT  `some-field`
                            FROM `some-table`
                            WHERE `other-field` = ?
                            LIMIT 1
                            ');

    $query = $this->db->query( $sql, array( $name ) );

    if ( $query->num_rows() == 1 )
    {
        $row = $query->row_array();
        return $row['some-field'];
    }

}

In this case, $name is Some-Name-1.

I can't see what's wrong here...

El Forum

[eluser]d1a8lo24[/eluser]
Not sure what is wrong and everyone has their own way of coding or writing their sql syntax. but try the following it might work.

Code:
// One line
$query = $this->db->select('something')->where('some_name', $name)->limit(1)->get('some_table');

// Or call it in segments
$this->db->select('something');

$this->db->where('some_name', $name);

$this->db->limit(1);

$query = $this->db->get('some_table');


// then do your if statement
if ($query->num_rows() == 1)
{
  // do something
}

Now on your code your calling the get query twice?
It should look like the following but then again like I said we all have our own way of coding.

Code:
public function get_field_from_other( $name )
{
    // query database for readable name
    $sql = 'SELECT  `some-field`
            FROM `some-table`
            WHERE `other-field` = ?
            LIMIT 1';

    $query = $this->db->query( $sql, array( $name ) );

    if ( $query->num_rows() == 1 )
    {
        $row = $query->row_array();
        return $row['some-field'];
    }

}

El Forum

[eluser]skunkbad[/eluser]
[quote author="d1a8lo24" date="1303640706"]Not sure what is wrong and everyone has their own way of coding or writing their sql syntax. but try the following it might work.

Code:
// One line
$query = $this->db->select('something')->where('some_name', $name)->limit(1)->get('some_table');

// Or call it in segments
$this->db->select('something');

$this->db->where('some_name', $name);

$this->db->limit(1);

$query = $this->db->get('some_table');


// then do your if statement
if ($query->num_rows() == 1)
{
  // do something
}

Now on your code your calling the get query twice?
It should look like the following but then again like I said we all have our own way of coding.

Code:
public function get_field_from_other( $name )
{
    // query database for readable name
    $sql = 'SELECT  `some-field`
            FROM `some-table`
            WHERE `other-field` = ?
            LIMIT 1';

    $query = $this->db->query( $sql, array( $name ) );

    if ( $query->num_rows() == 1 )
    {
        $row = $query->row_array();
        return $row['some-field'];
    }

}
[/quote]

YOU'RE RIGHT!!! Thanks. I was calling ->query() twice...