我一直在尝试将php页面转换为mysqli,并且遇到了一些问题。鉴于下面的代码,以及我命令工作的方式,我想知道使用mysqli方法的更好方法。
是否有mysql_num_rows的mysqli替代方法,或者是计算所需行数的另一种方法?
如何使用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;
}
}
}
答案 0 :(得分:1)
我通常在执行后直接使用它:
$query->execute();
// store the result first
$query->store_result();
$rows = $query->num_rows;
答案 1 :(得分:1)
实际上,回答第一个问题:
$data = mysql_query($countQuery) or die(mysql_error());
$rowcount = mysql_num_rows($data);
如果您想使用mysqli执行此操作,mysqli_num_rows可以正常工作。唯一的区别是,记得将连接参数放在查询中。
$data = mysqli_query($dbc,$countQuery) or die(mysql_error());
答案 2 :(得分:0)
答案 3 :(得分:0)
我已使用正确的属性/函数调用重写了您的代码示例,以修复您在问题中提到的两个问题:
<?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));
// Here is the property that you can reference to determine the affected rows from the previous query. -- gabriel
$rowcount = $data->num_rows;
$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; Globals are BAD!! Please don't use.
$con = mysqli::connet("localhost", "user", "", "ebay");
$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();
// Notice the adjusted function call to retrieve the associate array for each record within the result set. -- gabriel
while($row = $result->fetch_assoc()) {
$rows[] = $row;
}
return $rows;
}
}
}
但是,我想补充一点,dusoft在评估中是正确的,即MySQLi是错误的并且已知会产生问题。正是出于这个原因,我们不得不从MySQLi切换到PDO(至少是5.1的PHP原生),从那时起我们就没有任何问题了。我强烈建议您查看PDO或者其中一个Pear库来实际提供数据库连接接口。