Wordpress update query not working - php

I have an update query which is not working for me. I am able to make selects quite happily on the same page but I cannot get an update statement to work.
The table is not a part of wordpress so Im wondering if that could be it or if I have just got something wrong.
$query = "UPDATE login_count SET `count` = '100' WHERE `user_id` = $userID ";
$insrt = $wpdb->query($query);

Try this,
$insrt = $wpdb->update(
'login_count', //table_name
array('count'=>'100'),//data
array('user_id'=>$userID),//where
array('%s'),//data format
array('%s')
);

$insrt = $wpdb->update(
'login_count', //table_name
array(
'count' => '100', // string
),
array( 'user_id' => $user_id ), //Where Condition
array(
'%d', // value1
),
array( '%d' )
);

Try this ..user_id = {$userID} ";
Edit
See: Use a $variable inside a SQL string?

Related

How to get single element from $row[id]

I'm working on a project for PHPBB. I have a problem that I get some $row types. And they have some "id"s from MySQL, But I want theese "id"s by one by.
It's SQL query ;
$sql = $db->sql_build_query('SELECT', array(
'SELECT' => 'u.*, z.friend, z.foe, p.*',
'FROM' => array(
USERS_TABLE => 'u',
POSTS_TABLE => 'p',
),
'LEFT_JOIN' => array(
array(
'FROM' => array(ZEBRA_TABLE => 'z'),
'ON' => 'z.user_id = ' . $user->data['user_id'] . ' AND z.zebra_id = p.poster_id'
)
),
'WHERE' => $db->sql_in_set('p.post_id', $post_list) . '
AND u.user_id = p.poster_id'
));
$result = $db->sql_query($sql);
And then for user_id's
$rowset[$row['post_id']] = array(
'hide_post' => ($row['foe'] && ($view != 'show' || $post_id != $row['post_id'])) ? true : false,
'user_id' => $row['user_id'],
.
.
I logged $row[user_id] for in a topic and it return ;
97777
97778
97779
97783
But I want just first id i mean -> "97777" . So how can i pass just "97777" to a variable?
Dont suggest about sql level because I can't change any SQL command.
Dont suggest about sql level because I can't change any SQL command.
This is the problem though, your PHP is clearly selecting all 9 rows;
Using WHERE user_id = '97777' you will only select the record you want.
Why can you not edit the SQL? aren't you familiar with SQL or is the code your are using your own?
Without any code it is hard to say what you want to do:
reset($row);
$first_key = key($row);
or
reset($rows);
$first_key = key($rows);
Hard to guess without seeing any code.
You can limit your records using LIMIT clause.
SELECT column_name FROM table_name LIMIT 1;
It will return the first record of that table.

CakePHP Mysql query with AND condition

I am running a Mysql query in CakePHP but it is not outputting any result though no error is displayed.
Mysql query is this:
SELECT `isbn`, `title`, `author_name` FROM `books` WHERE `author_name` = 'William Rice' AND `title` LIKE '%Course%' ORDER BY `title` ASC
and it outputs results.
And I have written this in CakePHP find function:
$books = $this->Book->find('all', array('fields'=>array('Book.isbn', 'Book.title', 'Book.author_name'),
'conditions'=>array('Book.author_name'=>'William Rice',
'AND'=>array('Book.title'=>'LIKE %Course%')),
'order'=>'Book.title ASC',
));
But the query doesn't run. Is AND condition specified like this? Please have a look at it and tell me where is it wrong. Thanks.
Do not use AND array:
You have to change your query as given below:
$books = $this->Book->find('all',
array('fields'=>array('Book.isbn', 'Book.title', 'Book.author_name'),
'conditions'=>array('Book.author_name'=>'William Rice',
'Book.title LIKE'=>'%Course%'),
'order'=>'Book.title ASC'
));
$books = $this->Book->find('all', array(
'fields' => array('Book.isbn', 'Book.title', 'Book.author_name'),
'conditions' => array(
'Book.author_name'=>'William Rice',
'Book.title LIKE'=>'%Course%'
),
'order'=>'Book.title ASC',
));
You have done only one mistake , Remove 'AND' from your query, after removing 'AND' your query will be like below query.
$books = $this->Book->find('all', array(
'fields' => array('Book.isbn', 'Book.title', 'Book.author_name'),
'conditions' => array(
'Book.author_name'=>'William Rice',
'Book.title LIKE'=>'%Course%'
),
'order'=>'Book.title ASC',
)
);

CodeIgniter display last id

I want to display as a value of textbox the last id of my record and then increment it.
This is what I have:
view.php
<?php
$id = $this->db->insert_id();
$newId = $id + 1;
$data = array(
'name' => 'customercode',
'id' => 'inputCustomerCode',
'type' => 'text',
'readonly' => 'true',
'class' => 'form-control',
'value' => 'CUST0000' . $newId
);
echo form_input($data);
?>
But it's just displaying CUST00001? What am I doing wrong in here? Help is much appreciated and needed. Thanks.
you can get last inserted id with $this->db->insert_id(); if you want to use this for multiple places, please take this in a variable and use that variable.
you can use $this->db->insert_id() after insert query.
$id = $this->db->insert_id();
you can use a simple query to get max id
$query = $this->db->query("SELECT MAX (id) FROM table");
$id = $query->row()->id;
and use $id in form
'value' => 'CUST0000' . $id + 1
Instead of using $id = $this->db->insert_id(); you may use following code:
$query = $this->db->query("SELECT id FROM mytable ORDER BY id DESC LIMIT 1");
$id = $query->row()->id;
$newId = $id + 1;
$data = array(
'name' => 'customercode',
'id' => 'inputCustomerCode',
'type' => 'text',
'readonly' => 'true',
'class' => 'form-control',
'value' => 'CUST' . str_pad($id+1, 5, '0', STR_PAD_LEFT)
);
echo form_input($data);
Instead of
$this->db->insert_id()+1
You need to display $newId.
Make sure you have queried for the last Id. $this->db->insert_id() returns only when you have inserted some record. If you haven't , then you need to query to get the last inserted id. You can use this simple query.
SELECT id FROM mytable ORDER BY id DESC LIMIT 1
$this->db->insert_id()
The insert ID number when performing database inserts.
Looked at http://ellislab.com/codeigniter/user_guide/database/helpers.html and the first function is $this->db->insert_id();
This also works with active-record inserts...

How to get all table columns without defining them in query?

I have two tables, User and Company. I have joining them like this:
$table = $this->getDbTable();
$select = $table->select();
$select->setIntegrityCheck( false );
$select->from( array('User'), array( 'id' => 'id',
'name' => 'User.name',
'gender' => 'User.gender',
'Company_id' => 'User.Company_id'
));
$select->join( 'Company', 'Company.id = User.Company_id',
array( 'Company_name' => 'Company.name' ,
'Company_address' => 'Company.address'
));
$rows = $table->fetchAll( $select );
It is working and giving me accurate result. Problem is that I have to mentions column names in above codes. I want to get all columns without mentioning them in above piece of code.
For example I want something like this that get all columns(But it is not providing all column values):
$table = $this->getDbTable();
$select = $table->select();
$select->setIntegrityCheck( false );
$select->from( array('User') );
$select->join( 'Company', 'Company.id = User.Company_id' );
$rows = $table->fetchAll( $select );
Thanks
Leaving away the second parameter to the from call should work: http://framework.zend.com/manual/en/zend.db.select.html#zend.db.select.building.columns

Passing MySQL's CURRENT_TIMESTAMP to Zend_DB update statement

How do I pass mysql's CURRENT_TIMESTAMP when using Zend_DB's update statement? The following doesnt seem to be working.
I have something like this:
$update = array(
'Name' => 'John',
'DT_Modified' => 'CURRENT_TIMESTAMP'
);
$db->update('usertable', $update );
to run a query that is represented like this:
UPDATE usertable SET Name='John', DT_Modified = CURRENT_TIMESTAMP
Try using Zend_Db_Expr to avoid unnecessary quoting:
$update = array(
'Name' => 'John',
'DT_Modified' => new Zend_Db_Expr('CURRENT_TIMESTAMP')
);
$db->update('usertable', $update );

Categories