dongmo6937 2013-11-19 10:58
浏览 44
已采纳

从MySQL切换到MYSQLi - 循环不起作用

I'm changing my site from using mysql to mysqli. So far the changes have been quite simple but I have a calendar page with for loops which doesn't work after the switch. I'm pretty sure it's the loop which is causing the error, but I can't seem to find a fix.

Here's a selection of the code, which works with the mysql_query (which I have commented out) but doesn't with the mysqli->query

<?php include("conn.php");

$today = mktime(12,0,0,date("m"),1,date("Y"));
$currentmonth = date(m, $today);
$currentyear = date(Y, $today);
$prevmonth = $currentmonth - 1;
$prevyear = $currentyear;
$prevdate = mktime(12, 0, 0, $prevmonth, 1, $prevyear);
$pdim = date("t", $prevdate);

function showMonth($month, $year, $pdims, $prevmonths, $prevyears)
{
$caldate = mktime(12, 0, 0, $month, 1, $year);
$offset = date("w", $caldate);
$pdimsplus = $pdims + 1;
$enddays = $pdimsplus - $offset;

for($i = 1; $i <= $offset; $i++)
{   
$qryhead1 = "SELECT * FROM photos WHERE day(date) = $enddays AND month(date) = $prevmonths AND year(date) = $prevyears LIMIT 1";

//$res1 = $mysqli->query($qryhead1);
//$noofrec1 = $res1->num_rows;
//if ($noofrec1 > 0) {    
//while ($row1 = mysqli_fetch_object($res1)) {

$res1 = mysql_query($qryhead1);
$noofrec1 = mysql_num_rows($res1); 
if ($noofrec1 > 0) {
while ($row1 = mysql_fetch_object ($res1)) {
    $id = $row1->id;
    $image = $row1->image; ?>
    <div><?php print("$enddays"); ?><a href="<?php print("$id"); ?>"><img src="/images/<?php print("$image"); ?>"></a></div>
    <?php } 
    } else { ?>
    <div><?php print("$enddays"); ?></div>
    <?php }
    $enddays++;
    }
}
?>

<div>
<?php showMonth($currentmonth, $currentyear, $pdim, $prevmonth, $prevyear); ?>
</div>
  • 写回答

1条回答 默认 最新

  • donglei1616 2013-11-19 11:02
    关注

    If you have a $mysqli object (I'm assuming that you created one in conn.php), you're not passing it in as a function parameter for your custom function showMonth(). Have a read up on variable scope.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题