【发布时间】:2009-02-28 17:21:08
【问题描述】:
我一直在尝试将 php 页面转换为 mysqli,但遇到了一些问题。鉴于下面的代码,以及我订购的工作方式,我想知道使用 mysqli 方法的更好方法是什么。
是否有 mysqli 替代 mysql_num_rows 或者是计算所需行数的不同方法?
我将如何使用 mysqli 执行以下操作?:
$data = mysql_query($countQuery) or die(mysql_error());
$rowcount = mysql_num_rows($data);
mysql_fetch_assoc 的替代方案是什么?我觉得我不应该使用我正在使用的当前行方法,即使有替换功能,那么正确的方法是什么?
对于这些问题,我深表歉意,但到目前为止我自己还无法确定答案。
<?php
$con = mysqli_connect("localhost", "user", "", "ebay");
if (!$con) {
echo "Can't connect to MySQL Server. Errorcode: %s\n". mysqli_connect_error();
exit;
}
$con->query("SET NAMES 'utf8'");
$cmd = "word";
//normally retrieved from GET
if($cmd=="deleterec") {
$deleteQuery = "DELETE FROM AUCTIONS1 WHERE ARTICLE_NO = ?";
if ($delRecord = $con->prepare($deleteQuery)) {
$delRecord->bind_param("s", $pk);
$delRecord->execute();
}
}
$table = 'AUCTIONS';
$brand = "test";
$countQuery = "SELECT ARTICLE_NO FROM ? WHERE upper(ARTICLE_NAME) LIKE '% ? %'";
if ($numRecords = $con->prepare($countQuery)) {
$numRecords->bind_param("ss", $table, $brand);
$numRecords->execute();
$data = $con->query($countQuery) or die(print_r($con->error));
$rowcount = mysql_num_rows($data);
$rows = getRowsByArticleSearch($query, $table, $max);
$last = ceil($rowcount/$page_rows);
}
$self = htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'utf-8');
foreach ($rows as $row) // print table rows {
echo '<tr>' . "\n";
echo '<td><a href="#" onclick="doThings(\'Layer2\', \'' . $pk . '\')">'.$row['USERNAME'].'</a></td>' . "\n";
// repeated for each column
}
function getRowsByArticleSearch($searchString, $table, $max) {
global $con;
$recordsQuery = "SELECT ARTICLE_NO, USERNAME, ACCESSSTARTS, ARTICLE_NAME, date_format(str_to_date(ACCESSSTARTS, '%d/%m/%Y %k:%i:%s'), '%d %m %Y' ) AS shortDate FROM ? WHERE upper(ARTICLE_NAME) LIKE '%?%' ORDER BY str_to_date(ACCESSSTARTS, '%d/%m/%Y %k:%i:%s')" . $max;
if ($getRecords = $con->prepare($recordsQuery)) {
$getRecords->bind_param("ss", $searchString, $table);
$getRecords->execute();
$getRecords->bind_result($ARTICLE_NO, $USERNAME, $ACCESSSTARTS, $ARTICLE_NAME, $shortDate);
while ($getRecords->fetch()) {
$result = $con->query($recordsQuery);
$rows = array();
while($row = mysql_fetch_assoc($result)) {
$rows[] = $row;
}
return $rows;
}
}
}
【问题讨论】:
-
我建议使用 mysql,因为 mysqli 有问题。
-
@dusoft,您为什么建议使用旧的、不再处于活动开发中、本质上是过程而非面向对象且不支持准备好的语句的库?如果您要推荐反对 mysqli,那么至少选择另一个实际上更好的库。 :-(