I'm a total newbie when it comes to php and mysql/mysqli. I have this code and I get a PHP Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /home/byeroman/public_html/register.php on line 17. Here's the code:
$stmt = mysqli->prepare("SELECT COUNT(*) FROM users WHERE username=? LIMIT 1" or die($db->error()));
$stmt->bind_param("s", $username);
$stmt->execute();
$stmt->store_result();
$count=$stmt->num_rows;
$stmt->close();
if($count>0) exit();
what's wrong? thanks guys
That's silly syntax issue, you just forgot closing brace.
To make such things less possible and also to make your code readable, divide your statements into separate lines:
$sql = "SELECT COUNT(*) FROM users WHERE username=? LIMIT 1";
$stmt = $mysqli->prepare($sql) or trigger_error($mysqli->error()));
You also need to make your mind which variable you're using ($mysqli or $db)
Also, num_rows() is wrong function to use, you need regular fetch instead.
By the way, consider to use some database abstraction library. It can make your life a lot easier and code - shorter, like this (it replaces ALL your code, mind you):
$num = $db->getOne("SELECT COUNT(*) FROM users WHERE username=?",$username);
if($num) { ...
Related
Can anybody please help me achieving the result and solve my problem. I'm working in codeigniter where i want to fetch records from a mysql database using OR statement. I'm using custom model extension by Avenirer. I want to know how to use or operator in where function of this MY_MODEL:
https://github.com/avenirer/CodeIgniter-MY_Model/blob/version-3.0/core/MY_Model.php
You can do it in two ways.
You use or_where
$this->db->where('column1', 'like');
$this->db->or_where('column2', 'like');
or you can write both in a where
$this->db->where("(column1='like' OR column2='like')");
Hope it helps you out.
I can't give you an exact answer for your code, because I don't know exactly where the error is. If you could write the line to it, that would be useful.
I figured out the answer by studying the code in MY_MODEL file.
The right way to write where with OR clause was to use their special syntax.
As per the code in the file MY_MODEL, it states the supported arguments for where function, which are:
where($field_or_array = NULL, $operator_or_value = NULL, $value = NULL, $with_or = FALSE, $with_not = FALSE, $custom_string = FALSE)
Setting the 4th argument true will change the final syntax to OR statement.
Right statement in my case will be:
$this->Table_model->where('field1','value1')->where('field2','value2',null,true)->get_all();
This statement will be processed to following SQL statement.
SELECT * FROM Table WHERE 'field1'='value1' or 'field2'='value2'
Thanks all for their cooperation.
I'm trying to do this query but I got an error (Malformed UTF-8 characters, possibly incorrectly encoded):
DB::table('my_table')->select(DB::raw("id"))
->whereRaw('UPPER(name)','=', $upper_name)
->pluck('id')->first();
I'm trying to add the UPPER sql function to the query. With direct sql, the query should be:
select * from my_table
where UPPER(name) = 'HELLO'
Where $upper_name = HELLO.
Simpliest Way to do that. Hope will help you
DB::table('my_table')->select('id')
->where(DB::raw("UCASE(name)"), $upper_name)
->first();
UCASE - Convert the text to upper-case
Bah... I got it:
DB::table('my_table')->select(DB::raw("id"))
->whereRaw('UPPER(name) = ?', $upper_name)
->pluck('id')->first();
I have a login system for my webapp that works well using the Zend auth adapter but the problem is I want the email to be case insensitive when a user logs in. I am using Oracle as the back end DB and normally I would user the LOWER(EMAIL)=LOWER(:email) method. I tried to pass that Oracle function in the setIdentityColumn() but I get the error:
The supplied parameters to Zend_Auth_Adapter_DbTable failed to produce
a valid sql statement, please check table and column names for
validity.
protected function _getAuthAdapter()
{
//$dbAdapter = Zend_Db_Table::getDefaultAdapter();
$db = Zend_Registry::get('db');
$authAdapter = new Zend_Auth_Adapter_DbTable($db);
$authAdapter->setTableName('USER_TABLE')
->setIdentityColumn('LOWER(EMAIL)') //Tried to pass LOWER()
->setCredentialColumn('ENCODED_PW')
->setCredentialColumn('PASSWORD');
return $authAdapter;
}
The error is coming from the function _authenticateCreateSelect() in the Zend_Auth_Adapter_DbTable class. The problem is this part of the script:
$dbSelect->from($this->_tableName, array('*', $credentialExpression))
->where($this->_zendDb->quoteIdentifier($this->_identityColumn, true) . ' = ?', $this->_identity);
The quoteIdentifier() method is like PHP quote() and is turning a query like this:
select * from LOWER(:email)
into this:
select * from "LOWER(:email)"
Anyone see a way around this?
Kind Regards
Nathan
Try something like this:
$authAdapter->setTableName('USER_TABLE')
->setIdentityColumn(new Zend_Db_Expr('LOWER(USERID)'))
->setCredentialColumn('PASSWORD');
The problem is that if you pass 'LOWER(USERID)' as a simple string, Zend will put quotes around it, causing it to create an invalid query. Using Zend_Db_Expr will stop Zend doing this.
trying to execute a Doctrine query that is invariably not happening:
The code I have:
$q = Doctrine_Query::Create()->select('l.lid')->from('lessons l')->where('l.topic =?','Title of topic')
$result = $q->fetchOne() ;
The funny thing is, this returns the wrong column, that is not l.lid but l.someOtherColumn
So not sure where I am goofing up, your comments and critics are much appreciated.
Thanks!
Try to use Create('table_name t ') instead of ->from() ....
I cannot understand what is wrong with following query:
var tmp = dbset.AsEnumerable<mytable>().Where(
c=>c.Barcode.Contains("11") && c.Description.Contains("EW") );
return tmp;
Should this be same as:
select * from mytable
where Barcode like '%11%' and Description like '%EW%';
If I run this in sql server i get four rows as it should be, but not when I run the linq query
i get 0 rows.
Can please someone help me. It is rather a simple query and it is giving me such a headache. Thank you very much
You forget fetch data, do this:
var tmp = dbset.AsEnumerable<mytable>().Where(
c=>c.Barcode.Contains("11") && c.Description.Contains("EW") );
return tmp.ToList();
Also do not call AsEnumerable soon, use it as below:
var tmp = ctx.mytable.Where(
c=>c.Barcode.Contains("11") && c.Description.Contains("EW") );
return tmp.ToList();
dbset.AsEnumerable<mytable>()...
Don't do that!
You are causing all your data to get pulled from the database before checking the Where condition.
Other than that, it's not really clear why your query isn't working. Your syntax is correct. I'm guessing the data doesn't actually look like you think it does. Either that, or you're expecting like %EW% to match something in a case-insensitive manner and since the Where clause is being evaluated by LINQ to Objects you're not getting that behavior.
Run a query with only one condition? " c.Barcode.Contains("11") ".
That code should run fine.