【问题标题】:PHP printf function "supplied argument is not a valid MySQL result resource" [duplicate]PHP printf函数“提供的参数不是有效的MySQL结果资源” [重复]
【发布时间】:2013-03-08 08:43:12
【问题描述】:

您好,我的以下代码在一台服务器上运行良好,但在我使用的免费服务器上出现以下错误

Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/a9645958/public_html/tteeth/staffslot.php on line 75

我的代码如下:

// Starting of top line showing name of the day's slots
echo "<form method='post' name='time' action='timeinsert.php'>
<table align='center' width='380' border='1'>
<tr>
<th>Select</th>
<th>Appointment Times</th>
</tr>";

// Starting of the slots
for($i=0;$i<=31;$i++){ // this shows 32 x 15 minute slots from 9:00am - 17:00pm
$shr=9+floor($i/4); //calculates the start hour of a slot
$smin=($i%4)*15; //calculates the start minutes of a slot
$time=mktime($shr,$smin,00,0,0,0); //creates full php time from hours and minutes above
$displayTime2=date("H:i:s",$time); // creates correct time format for MySQL database hrs, mins, seconds

$pagetime = date("H:i a", strtotime($displayTime2)); //converts the time to just hours and minutes
$timenow = date('H:i'); // get's the current time

$chosendate = date(mktime(0,0,0,$month,$day,$year)); // converts the date picked to php time (seconds since 01 01 1971)
$todayDate = date(mktime()); // converts today's date to php time so it can be compared with the chosen date

while ($myrow = mysql_fetch_row($result)) {
        printf("<tr><td>%s</td><td>%s</td></tr>\n",
        $myrow[0]);
}
$result=mysql_query("SELECT * FROM appointment where Appointment_Time='$displayTime2' AND Appointment_Date='$insertdate' AND Practice_ID='$practice'");
$myrow = mysql_fetch_row($result);
    if($pagetime<$timenow AND $chosendate<$todayDate) //if the display time is less than the time now, and the date is less than todays date
        { //do not display a row 
        }
    elseif($myrow)// if the values in the database match the row slot time
        {
        echo "<td></td><td align='center'>$pagetime</td>";//just display the slot as a time
        }
    else
        {
        echo    "<td align='center'><input type='checkbox' name='$displayTime2' onclick='KeepCount()'></td>
                <td align='center'>$pagetime</td>";
        }
    echo "</td></tr>";          
        }
    echo "</table><br>
        <input type='submit' value='Submit'>
</form>";

我很确定问题出在 print f 函数上,因为它要求为 $myrow 中的值打印表头,但它还没有得到它的值...但是如果我将它移到 select 语句之后,它会弄乱我的其余代码(它不会将时间与数据库中的内容进行比较,并且在下一页上插入数据库时​​存在问题)

【问题讨论】:

  • 您没有将代码放在这里,因为错误在第 75 行,而该代码只有 44 行。如果错误在您发布的代码中,那是因为查询失败。使用: if(!$result = mysql_query("your query...")) print mysql_error();

标签: php mysql arrays printf fetch


【解决方案1】:

在这里,您正在尝试获取当时不存在的结果。

while ($myrow = mysql_fetch_row($result)) {
        printf("<tr><td>%s</td><td>%s</td></tr>\n",
        $myrow[0]);
}

你应该把那个while循环放在这个之后

$result=mysql_query("SELECT * FROM appointment where Appointment_Time='$displayTime2' AND Appointment_Date='$insertdate' AND Practice_ID='$practice'");

【讨论】:

  • 我已经提到......当我把它放在 select 语句之后,下一部分不能正常工作:$myrow = mysql_fetch_row($result); if($pagetime
  • 为什么会出现这个循环:while ($myrow = mysql_fetch_row($result)) { printf(" %s %s \n", $myrow[0]); }。 mysql_fetch_row() 从表中只返回 1 行。这可以返回多少行(“SELECT * FROMointment where Appointment_Time='$displayTime2' AND Appointment_Date='$insertdate' AND Practice_ID='$practice')?一个或多个?
  • 它可以返回与数据库中符合该条件的约会一样多的约会...理论上应该只有 1 条记录!
  • 如果它可以返回多条记录,你应该使用 mysql_fetch_array() 而不是 mysql_fetch_row()。下一个顺序应该是第一个结果 = mysql_query(...),第二行 = mysql_fetch_row() / _array()。如果这不能正常工作,那么您的问题出在其他地方。
猜你喜欢
  • 1970-01-01
  • 2011-04-11
  • 1970-01-01
  • 2013-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-30
  • 1970-01-01
相关资源
最近更新 更多