Phiên bản trước của cả hai bênPhiên bản trướcPhiên bản sau | Phiên bản trướcPhiên bản cuốiPhiên bản sau của cả hai bên |
mysql_nv4 [2021/04/21 11:21] – [3. Delete] hoaquynhtim99 | mysql_nv4 [2021/04/21 11:27] – [Truy vấn 1 trường csdl lấy 1 record duy nhất] hoaquynhtim99 |
---|
| |
$db->sqlreset() | $db->sqlreset() |
->select( 'COUNT(*)' ) | ->select('COUNT(*)') |
->from( NV_PREFIXLANG . '_' . $module_data . '_rows' ) | ->from(NV_PREFIXLANG . '_' . $module_data . '_rows') |
->where( 'status= 1 AND inhome=1' ); | ->where('status= 1 AND inhome=1'); |
$all_page = $db->query( $db->sql() )->fetchColumn(); | $all_page = $db->query($db->sql()) |
$db->select( 'id, catid, listcatid, topicid, admin_id, author, sourceid, addtime, edittime, publtime, title, alias, hometext, homeimgfile, homeimgalt, homeimgthumb, allowed_rating, hitstotal, hitscm, total_rating, click_rating' ) | ->fetchColumn(); |
->order( 'id DESC' ) | $db->select('id, catid, listcatid, topicid, admin_id, author, sourceid, addtime, edittime, publtime, title, alias, hometext, homeimgfile, homeimgalt, homeimgthumb, allowed_rating, hitstotal, hitscm, total_rating, click_rating') |
->limit( $per_page ) | ->order('id DESC') |
->offset( page ); | ->limit($per_page) |
$result = $db->query( $db->sql() ); | ->offset(page); |
| $result = $db->query($db->sql()); |
| |
</code> | </code> |
<code php > | <code php > |
| |
$result = $db->query( "SELECT * FROM " . $db_config['prefix'] . "_" . $lang_data . "_modules where title='news'" ); | $result = $db->query("SELECT * FROM " . $db_config['prefix'] . "_" . $lang_data . "_modules where title='news'"); |
if( $result->rowCount() ) | if ($result->rowCount()) { |
{ | // |
// | |
} | } |
| |
<cpde> | </code> |
| |
Cần viết thành | Cần viết thành |
<code php > | <code php > |
| |
$result = $db->query( "SELECT COUNT(*) FROM " . $db_config['prefix'] . "_" . $lang_data . "_modules where title='news'" ); | $result = $db->query("SELECT COUNT(*) FROM " . $db_config['prefix'] . "_" . $lang_data . "_modules where title='news'"); |
if( $result->fetchColumn() ) | if ($result->fetchColumn()) { |
{ | |
// | // |
} | } |
<code php > | <code php > |
| |
$sth = $db->prepare( 'UPDATE nv3_vi_news_bodyhtml SET bodyhtml = :bodyhtml WHERE id=4' ); | $sth = $db->prepare('UPDATE nv3_vi_news_bodyhtml SET bodyhtml = :bodyhtml WHERE id=4'); |
$len = strlen($bodyhtml); | $len = strlen($bodyhtml); |
$sth->bindParam( ':bodyhtml', $bodyhtml, PDO::PARAM_STR, $len ); | $sth->bindParam(':bodyhtml', $bodyhtml, PDO::PARAM_STR, $len); |
$sth->execute(); | $sth->execute(); |
| |
<code php> | <code php> |
| |
<?php | $stmt = $db->prepare('INSERT INTO ' . $db_config['prefix'] . '_' . $module_data . ' SET |
$stmt = $db->prepare( 'INSERT INTO ' . $db_config['prefix'] . '_' . $module_data . ' SET | |
send_to = :send_to, | send_to = :send_to, |
subject = :subject, | subject = :subject, |
message = :message, | message = :message, |
status = ' . intval( $data['status'] ) . ', | status = ' . intval($data['status']) . ', |
finish = ' . intval( $data['finish'] ) . '' ); | finish = ' . intval($data['finish']) . ''); |
| |
| |
$stmt->bindParam( ':send_to', $data['send_to'], PDO::PARAM_STR ); | |
$stmt->bindParam( ':subject', $data['subject'], PDO::PARAM_STR ); | |
$stmt->bindParam( ':message', $data['message'], PDO::PARAM_STR, strlen( $data['message'] ) ); | |
$stmt->execute(); | |
$stmt->closeCursor(); | |
| |
if( $data['newsletter_id'] = $db->lastInsertId() ) | $stmt->bindParam(':send_to', $data['send_to'], PDO::PARAM_STR); |
{ | $stmt->bindParam(':subject', $data['subject'], PDO::PARAM_STR); |
| $stmt->bindParam(':message', $data['message'], PDO::PARAM_STR, strlen($data['message'])); |
| $stmt->execute(); |
| $stmt->closeCursor(); |
| |
| if ($data['newsletter_id'] = $db->lastInsertId()) { |
| } |
| |
| |
} | |
?> | |
| |
</code> | </code> |
<code php> | <code php> |
| |
<?php | $stmt = $db->prepare('UPDATE ' . $db_config['prefix'] . '_' . $module_data . ' SET |
| subject = :subject, |
$stmt = $db->prepare( 'UPDATE ' . $db_config['prefix'] . '_' . $module_data . ' SET | |
subject = :subject, | |
send_to = :send_to, | send_to = :send_to, |
message = :message, | message = :message, |
status = ' . intval( $data['status'] ) . ', | status = ' . intval($data['status']) . ', |
WHERE newsletter_id=' . $data['newsletter_id'] ); | WHERE newsletter_id=' . $data['newsletter_id']); |
| |
$stmt->bindParam( ':subject', $data['subject'], PDO::PARAM_STR ); | $stmt->bindParam(':subject', $data['subject'], PDO::PARAM_STR); |
$stmt->bindParam( ':send_to', $data['send_to'], PDO::PARAM_STR ); | $stmt->bindParam(':send_to', $data['send_to'], PDO::PARAM_STR); |
$stmt->bindParam( ':message', $data['message'], PDO::PARAM_STR, strlen( $data['message'] ) ); | $stmt->bindParam(':message', $data['message'], PDO::PARAM_STR, strlen($data['message'])); |
if( $stmt->execute() ) | if ($stmt->execute()) { |
{ | } |
| $stmt->closeCursor(); |
} | |
$stmt->closeCursor(); | |
?> | |
| |
</code> | </code> |
<code php> | <code php> |
| |
<?php | // Sử dụng mysqli |
| $db->query('DELETE FROM ' . $db_config['prefix'] . '_' . $module_data . ' WHERE newsletter_id=' . intval($data['newsletter_id'])); |
| |
//Sử dụng mysqli | // Sử dụng PDO theo phương thức bindParam đối với string send_to |
$db->query( 'DELETE FROM ' . $db_config['prefix'] . '_' . $module_data . ' WHERE newsletter_id=' . intval( $data['newsletter_id'] ) ); | $stmt = $db->prepare('DELETE FROM ' . $db_config['prefix'] . '_' . $module_data . ' WHERE send_to=: send_to, newsletter_id=' . intval($data['newsletter_id'])); |
| $stmt->bindParam(':send_to', $data['send_to'], PDO::PARAM_STR); |
// Sử dụng PDO theo phương thức bindParam đối với string send_to | $stmt->execute() |
$stmt = $db->prepare( 'DELETE FROM ' . $db_config['prefix'] . '_' . $module_data . ' WHERE send_to=: send_to, newsletter_id=' . intval( $data['newsletter_id'] ) ); | $stmt->closeCursor(); |
$stmt->bindParam( ':send_to', $data['send_to'], PDO::PARAM_STR ); | |
$stmt->execute() | |
$stmt->closeCursor(); | |
| |
// Sử dụng PDO theo phương thức $db->quote đối với string send_to | |
$stmt = $db->prepare( 'DELETE FROM ' . $db_config['prefix'] . '_' . $module_data . ' WHERE send_to= ' . $db->quote( $data['send_to'] ) . ', newsletter_id=' . intval( $data['newsletter_id'] ) ); | |
$stmt->bindParam( ':send_to', $data['send_to'], PDO::PARAM_STR ); | |
$stmt->execute() | |
$stmt->closeCursor(); | |
| |
| |
?> | // Sử dụng PDO theo phương thức $db->quote đối với string send_to |
| $stmt = $db->prepare('DELETE FROM ' . $db_config['prefix'] . '_' . $module_data . ' WHERE send_to= ' . $db->quote($data['send_to']) . ', newsletter_id=' . intval($data['newsletter_id'])); |
| $stmt->bindParam(':send_to', $data['send_to'], PDO::PARAM_STR); |
| $stmt->execute() |
| $stmt->closeCursor(); |
| |
</code> | </code> |
<code php> | <code php> |
| |
<?php | $per_page = 30; |
| |
$per_page = 30; | $page = $nv_Request->get_int('page', 'get', 1); |
| |
$page = $nv_Request->get_int( 'page', 'get', 1 ); | |
| |
$sql = $db_config['prefix'] . '_' . $module_data; | $sql = $db_config['prefix'] . '_' . $module_data; |
| |
$num_items = $db->query( 'SELECT COUNT(*) FROM ' . $sql )->fetchColumn(); | |
| |
$base_url = NV_BASE_ADMINURL . 'index.php?' . NV_NAME_VARIABLE . '=' . $module_name . '&' . NV_OP_VARIABLE . '=main&per_page=' . $per_page; | |
| |
$generate_page = nv_generate_page( $base_url, $num_items, $per_page, $page ); | |
| |
$db->sqlreset()->select( '*' )->from( $sql )->order( 'weight ASC' )->limit( $per_page )->offset( ( $page - 1 ) * $per_page ); | |
| |
$result = $db->query( $db->sql() ); | $num_items = $db->query('SELECT COUNT(*) FROM ' . $sql)->fetchColumn(); |
| |
$array_content = array(); | $base_url = NV_BASE_ADMINURL . 'index.php?' . NV_NAME_VARIABLE . '=' . $module_name . '&' . NV_OP_VARIABLE . '=main&per_page=' . $per_page; |
| |
while( $rows = $result->fetch() ) | $generate_page = nv_generate_page($base_url, $num_items, $per_page, $page); |
{ | |
$array_content[] = $rows; | $db->sqlreset() |
} | ->select('*') |
| ->from($sql) |
?> | ->order('weight ASC') |
| ->limit($per_page) |
| ->offset(($page - 1) * $per_page); |
| |
| $result = $db->query($db->sql()); |
| |
| $array_content = array(); |
| |
| while ($rows = $result->fetch()) { |
| $array_content[] = $rows; |
| } |
| |
</code> | </code> |
<code php> | <code php> |
| |
<?php | $sql = 'SELECT newsletter_id, send_to FROM ' . $db_config['prefix'] . '_' . $module_data . ' WHERE newsletter_id = 1 ORDER BY newsletter_id ASC LIMIT 0, 20'; |
| |
$sql='SELECT newsletter_id, send_to FROM ' . $db_config['prefix'] . '_' . $module_data .' WHERE newsletter_id = 1 ORDER BY newsletter_id ASC LIMIT 0, 20'; | $result = $db->query($sql); |
| |
$result = $db->query( $sql ); | $array_content = array(); |
| |
$array_content = array(); | while (list ($newsletter_id, $send_to) = $result->fetch(3)) { |
| $array_content[] = array( |
while( list( $newsletter_id, $send_to ) = $result->fetch(3) ) | 'newsletter_id' => $newsletter_id, |
{ | 'send_to' => $send_to |
$array_content[] = array( 'newsletter_id'=> $newsletter_id, 'send_to'=> $send_to ); | ); |
} | } |
$result->closeCursor(); | $result->closeCursor(); |
?> | |
| |
</code> | </code> |
<code php> | <code php> |
| |
<?php | $sql = 'SELECT newsletter_id, send_to FROM ' . $db_config['prefix'] . '_' . $module_data . ' WHERE newsletter_id = 1 ORDER BY newsletter_id ASC LIMIT 0, 20'; |
| |
$sql='SELECT newsletter_id, send_to FROM ' . $db_config['prefix'] . '_' . $module_data .' WHERE newsletter_id = 1 ORDER BY newsletter_id ASC LIMIT 0, 20'; | $result = $db->query($sql); |
| |
$result = $db->query( $sql ); | $array_content = array(); |
| |
$array_content = array(); | while ($rows = $result->fetch()) { |
| $array_content[] = $rows; |
while( $rows = $result->fetch() ) | } |
{ | $result->closeCursor(); |
$array_content[] = $rows; | |
} | |
$result->closeCursor(); | |
?> | |
| |
</code> | </code> |
<code php> | <code php> |
| |
<?php | $sql = 'SELECT newsletter_id, send_to FROM ' . $db_config['prefix'] . '_' . $module_data . ' WHERE newsletter_id = 1 ORDER BY newsletter_id ASC LIMIT 0, 1'; |
| |
$sql='SELECT newsletter_id, send_to FROM ' . $db_config['prefix'] . '_' . $module_data .' WHERE newsletter_id = 1 ORDER BY newsletter_id ASC LIMIT 0, 1'; | $result = $db->query($sql); |
| |
$result = $db->query( $sql ); | list ($newsletter_id, $send_to) = $result->fetch(3); |
| |
list( $newsletter_id, $send_to ) = $result->fetch(3); | $result->closeCursor(); |
| |
$result->closeCursor(); | |
?> | |
| |
</code> | </code> |
<code php> | <code php> |
| |
<?php | $result = $db->query('SELECT COUNT(*) FROM ' . $db_config['prefix'] . '_' . $module_data); |
| |
$result = $db->query( 'SELECT COUNT(*) FROM ' . $db_config['prefix'] . '_' . $module_data ); | list ($total) = $result->fetch(3); |
| |
list( $total ) = $result->fetch(3); | $result->closeCursor(); |
| |
$result->closeCursor(); | |
?> | |
| |
</code> | </code> |