I am using adapter in zend framework 2. My code is for mysql update query which is not working, can any one suggest me. Array result is ok but its not display in query. Just shows update tablename set.
I have tried all suggestions of SO and also from Google, but I am failed to solve that problem.
Code is here:
public function updatebhkdetail($bhkupdate)
{
$WHERE = 'project_id='.$bhkupdate['project_id'];
$sql = new Sql($this->adapter);
$update1bhk = $sql->update('tablename', array($bhkupdate), $WHERE);
$statementUpdate = $sql->getSqlStringForSqlObject($update1bhk);
$sectorName = $this->adapter->query($statementUpdate,
\Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
}
I'm not to familiar with getSqlStringForSqlObject. But this should work:
$sql = new Sql( $this->adapter );
$update = $sql->update();
$update->table( <yourTableName> );
$update->set( $keyValues );
$update->where( array( 'project_id' => $bhkupdate['project_id'] ) );
$statement = $sql->prepareStatementForSqlObject( $update );
$results = $statement->execute();
Related
PHP 7.2.21
Database is on MS SQL, and the database connection is working
I have found and read sqlsrv_num_rows Not Returning Any Value. I have updated the $options based on the solution, but my code still does not return any results.
If I run the query in SQL, I get 8 records, so I know the data is there.
$sql = "SELECT * FROM Question WHERE Category = 'HD';";
$params = array();
$options = array( "Scrollable" => 'keyset' );
$stmt = sqlsrv_query( $conn, $sql, $params, $options );
if( $stmt === false ) { reportSQLError($sql, $params); }
$maxHDQ = sqlsrv_num_rows($stmt);
The code itself is ok and should work. What are the values for $stmt and $maxHDQ?
It is suddenly working
I just removed 'HD' from the query and added it to $params and this seems to have resolved everything.
I am not sure why it is working this way as the final query sent to SQL comes out the same either way I run it, however it does work.
$sql = "SELECT * FROM Question WHERE Category = ?;";
$params = array('HD');
$options = array( 'Scrollable' => 'buffered' );
$stmt = sqlsrv_query( $conn, $sql, $params, $options );
if( $stmt === false ) { reportSQLError($sql, $params); }
$maxHDQ = sqlsrv_num_rows($stmt);
I have a problem with my query, it returns nothing.
if($champ == "type_id")
{
$bdd = new PDO("mysql:dbname=maruecondi_db;host=localhost","root","");
$request = $bdd->prepare('SELECT * FROM type_commercant WHERE type=:old');
$request->execute(array(':old' => $old));
while($row = $request->fetch())
{
$bdd1 = new PDO("mysql:dbname=maruecondi_db;host=localhost","root","");
$request1 = $bdd1->prepare('UPDATE commercant SET type_id=:type_id WHERE id=:id');
$request1->execute(array(':type_id' => $row['id'],':id' => $id));
}
}
I'm getting variables from ajax request (JQUERY) and i initialize them before, i avoid you the code.
Other requests on the page works.
I have currently no way to see if somethings got wrong, due to ajax call. (No php orange boxes / pdo message)
I tried to solve to problems, and i discovered that we go into the if.
I deleted the first query which contains the while, i replaced $row['id'] by a value, and i worked.
Since the beginning, i keep copying and pasting the connection to my database so no problem.
So my problem is here:
$request = $bdd->prepare('SELECT * FROM type_commercant WHERE type=:old');
$request->execute(array(':old' => $old));
while($row = $request->fetch())
I don't see what i've done wrong...
$request = $bdd->prepare('SELECT * FROM type_commercant');
$request->execute();
while($row = $request->fetch())
This works, so i tried this:
$request = $bdd->prepare('SELECT * FROM type_commercant');
$request->execute();
while($row = $request->fetch())
{
if($row['type'] == $old)
{
$request1 = $bdd->prepare('UPDATE commercant SET type_id=:type_id WHERE id=:id');
$request1->execute(array(':type_id' => $row['id'],':id' => $id));
}
}
We don't go in the condition if($row['type'] == $old), but i delete this condition, and when i replace with something like this:
while($row = $request->fetch())
{
$request1 = $bdd->prepare('UPDATE commercant SET adresse=:type_id WHERE id=:id');
$request1->execute(array(':type_id' => $row['id'],':id' => $id));
}
It works... i checked $row['type'], $row['id'], $old in array(':type_id' => $row['id'], all variable got the string attented. So what's the problem?
Thanks in advance !
Hmm, how to say that...
I was updating the data with the old data i explain myself:
$bdd = new PDO("mysql:dbname=maruecondi_db;host=localhost","root","");
$request = $bdd->prepare('SELECT id FROM type_commercant WHERE type=:old');
$request->execute(array(':old' => $old));
was getting the old ID from type_commercant, and i was doing this:
$request1 = $bdd1->prepare('UPDATE commercant SET type_id=:type_id WHERE id=:id');
$request1->execute(array(':type_id' => $row['id'],':id' => $id));
So i replaced the old ID by... the old ID.
Sorry for that mistake, thanks anyway for reading me and trying so solve this problem :D
Try to call PDO::exec() for doing UPDATE or DELETE .
$bdd = new PDO("mysql:dbname=maruecondi_db;host=localhost","root","");
if($champ == "type_id")
{
$stmt = $bdd->prepare('SELECT * FROM type_commercant WHERE type=:old');
$stmt->execute(array(':old' => $old));
$rows = $stmt->fetchAll();
// Uncomment this to know what you get
// var_dump($rows);
foreach ( $rows as $row ) {
$bdd->exec(
"UPDATE commercant
SET type_id = " . $bdd->quote($row['id']) .
" WHERE id = " . $bdd->quote($id)
);
}
}
You can use this code for debugging.
var_dump( $bdd->errorInfo() );
I'm working with database with a lot of columns with 'enum' and 'set' type. My point is to get values of column in Phalcon controller. I found some snippets, but nothing in Phalcon and when I tried to execute them it seems that Phalcon have some problems.
public function getEnumValues(){
$sql = "SHOW COLUMNS FROM profiles LIKE 'eyes_color'";
$query = new \Phalcon\Mvc\Model\Query($sql, $this->getDI());
$result = $query->execute();
return $result;
}
Returns:
Syntax error, unexpected token IDENTIFIER(SHOW), near to ' COLUMNS FROM profiles LIKE 'eyes_color'', when parsing: SHOW COLUMNS FROM profiles LIKE 'eyes_color'
Approach 2:
public function getEnumValues(){
$sql = "SELECT COLUMN_TYPE FROM COLUMNS WHERE TABLE_NAME = 'profiles' AND COLUMN_NAME = 'hair_color'";
$query = new \Phalcon\Mvc\Model\Query($sql, $this->getDI());
$result = $query->execute();
return $result;
}
Returns:
Model 'COLUMNS' could not be loaded
I would be grateful for any help.
This is working version for your needs:
$config = $this->getDI()->get('config');
$pdo = new \Phalcon\Db\Adapter\Pdo\Mysql([
"host" => $config->database->host,
"username" => $config->database->username,
"password" => $config->database->password,
"dbname" => $config->database->dbname,
]);
$sql = "
SELECT TRIM(TRAILING ')' FROM SUBSTRING(COLUMN_TYPE,6)) AS enum_list
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA='my_db_name'
AND TABLE_NAME='my_table_name'
AND COLUMN_NAME='my_column_name'
";
$result = $pdo->query($sql)->fetch();
$enumArray = array();
if (!empty($result['enum_list'])) {
$enumArray = explode(',', $result['enum_list']);
foreach ($enumArray as &$value) {
$value = trim($value, "'");
}
}
echo "<pre>"; var_dump($enumArray); die;
Could you get what you want from the columnMap function in the model?
$model = new Profile();
$columns = $model->columnMap();
This will return an array of all columns. Possible create another similar function to just return the columns you want? Perhaps what you're trying to achieve is more involved.
BTW I got the same error using the modelManager.
Here is my model code below,
public function insertme()
{
$sel = new Sql($this->adapter);
$s = $sel->insert('users');
$data = array(
'fname'=>'fisdsds',
'lname'=>'sdsdsdme',
'email'=>'sdsdsds',
'pword'=>'dsdsds'
);
$s->values($data);
$statement = $sel->prepareStatementForSqlObject($s);
$comments = $statement->execute();
$resultset = new ResultSet();
$resultset->initialize($comments);
$result = $resultset->toArray();
//print_R($result);
return $result;
}
it is inserting data into database table users but iam also getting an error SQLSTATE[HY000]: General error what could be the problem?
There's no need to try and make a ResultSet from an insert, it's not going to give you back any resultset data.
public function insertme()
{
$sel = new Sql($this->adapter);
$s = $sel->insert('users');
$data = array(
'fname'=>'fisdsds',
'lname'=>'sdsdsdme',
'email'=>'sdsdsds',
'pword'=>'dsdsds'
);
$s->values($data);
$statement = $sel->prepareStatementForSqlObject($s);
$result= $statement->execute();
//print_R($result);
return $result;
}
The problem is on :soundid if I type manually soundid='soundidfromPOST' received from POST, the row is updated, but with soundid=:soundid ... nothing. Why?
PDO::ATTR_ERRMODE to PDO::ERRMODE_EXCEPTION and error_reporting enabled.
public function save($args) {
$userid = Controller::getUserConnection();
if ($userid) {
$soundid = $_POST['soundid'];
$track_title = $_POST['track_title'];
$track_artist = $_POST['track_artist'];
$track_album = $_POST['track_album'];
$track_genre = $_POST['track_genre'];
$track_description = $_POST['track_description'];
$played = 1;
$statement = $this->_db->prepare("UPDATE sounds SET title=:track_title, artist=:track_artist, album=:track_album, genre_id=:track_genre, description=:track_description, played=:played WHERE soundid=:soundid AND userid=:userid AND ip=:ip");
$statement->bindParam(':soundid',$soundid,PDO::PARAM_STR);
$statement->bindParam(':userid',$userid,PDO::PARAM_INT);
$statement->bindParam(':track_title',$track_title,PDO::PARAM_STR);
$statement->bindParam(':track_artist',$track_artist,PDO::PARAM_STR);
$statement->bindParam(':track_album',$track_album,PDO::PARAM_STR);
$statement->bindParam(':track_genre',$track_genre,PDO::PARAM_INT);
$statement->bindParam(':track_description',$track_description,PDO::PARAM_STR);
$statement->bindParam(':ip',$_SERVER['REMOTE_ADDR'],PDO::PARAM_STR);
$statement->bindParam(':played',$played,PDO::PARAM_INT);
$statement->execute();
echo 'saved!';
}
}
I would do the following to make it cleaner and because you don't need to bind everything explicitly (please note I didn't use all your variables):
Assign all your post data that you want to use in the query to an array:
$data = array(
'userid' => $userid,
'sounddid' => $_POST['soundid'],
'track_title' => $_POST['track_title'],
'track_artist' => $_POST['track_title'],
'ip' => $_SERVER['REMOTE_ADDR'],
);
Write you query:
$sth = $this->_db->prepare("
UPDATE sounds SET
title = :track_title,
artist = :track_artist
WHERE soundid = :soundid
AND userid = :userid
AND ip = :ip
");
Pass in your data array to be executed:
$result = $sth->execute($data);