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.

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

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100