将查询结果存储在数组中 - 出了什么问题?

时间:2021-10-30 06:22:37

I have a query:

我有一个问题:

mysql_select_db($database_dbconfig, $dbconfig);
$query_Holidays = "SELECT substring(data,9,2) AS days_holidays FROM database_holidays WHERE database_holidays.data <> ''";
$Holidays = mysql_query($query_Holidays, $dbconfig) or die(mysql_error());
$row_Holidays = mysql_fetch_assoc($Holidays);
$totalRows_Holidays = mysql_num_rows($Holidays);

Results are like this:

结果是这样的:

14    15    21    30

and

$totalRows_Holidays = 4

if I add this: (in order to get an array)

如果我添加这个:(为了得到一个数组)

$days_calendar = array();
while ($row_Holidays = mysql_fetch_assoc($Holidays)) {
    $days_calendar[] = $row_Holidays['days_holidays'];}
print_r($days_calendar);

I get as result:

我得到的结果是:

Array([0]=>15[1]=>21[2]=>30)

Where is 14?

14在哪里?

5 个解决方案

#1


4  

When you do mysql_fetch_assoc($Holidays) you recover one row, if you are executing this before the while loop, then the first row (14) will be escaped.

当你执行mysql_fetch_assoc($ Holidays)时,你恢复了一行,如果你在while循环之前执行它,那么第一行(14)将被转义。

Remove this first mysql_fetch_assoc and all will be ok.

删除第一个mysql_fetch_assoc,一切都会好的。

And do not use mysql_* functions, they are deprecated and will not be included in future versions, use PDO or mysqli_* functions

并且不要使用mysql_ *函数,它们已被弃用,将来不会包含在内,使用PDO或mysqli_ *函数

PS: Maybe this is a comment but i don't have permission

PS:也许这是评论,但我没有获得许可

#2


2  

When you call the first time

当你第一次打电话的时候

 $row_Holidays = mysql_fetch_assoc($Holidays);

You are actually skipping the first row. So, the resulting code should be

你实际上正在跳过第一行。所以,结果代码应该是

 mysql_select_db($database_dbconfig, $dbconfig);
 $query_Holidays = "SELECT substring(data,9,2) AS days_holidays 
      FROM database_holidays     
      WHERE database_holidays.data <> ''";
 $Holidays = mysql_query($query_Holidays, $dbconfig) or die(mysql_error());
 $totalRows_Holidays = mysql_num_rows(Holidays);
 $days_calendar = array();
 while ($row_Holidays = mysql_fetch_assoc($Holidays)) 
 {
     $days_calendar[] = $row_Holidays['days_holidays'];
 }
 print_r($days_calendar);

Ok, and now that you get it working, please consider using mysqli_ functions!! :)

好的,现在你可以使用它,请考虑使用mysqli_ functions !! :)

#3


1  

try this one

试试这个

  $days_calendar = array();
    while ($row_Holidays = mysql_fetch_array($Holidays)) {
        $days_calendar[] = $row_Holidays['days_holidays'];}
    print_r($days_calendar);

#4


1  

Replace everything from

替换所有东西


$row_Holidays = mysql_fetch_assoc($Holidays);

with


$totalRows_Holidays = mysql_num_rows(Holidays);
if ($totalRows_Holidays > 0){
  while ($row_Holidays = mysql_fetch_assoc($Holidays)) {
    $days_calendar[] = $row_Holidays['days_holidays'];
  }
}
print_r($days_calendar);

That first line is where the 14 was called. Using my code resolves the issue.


第一行是14号召唤的地方。使用我的代码解决了这个问题。

Hope this answer helps.

希望这个答案有所帮助

#5


1  

try this ...

尝试这个 ...

    mysql_select_db($database_dbconfig, $dbconfig);
    $query_Holidays = "SELECT substring(data,9,2) AS days_holidays FROM database_holidays   WHERE database_holidays.data <> ''";
    $Holidays = mysql_query($query_Holidays, $dbconfig) or die(mysql_error());
    //$row_Holidays = mysql_fetch_assoc($Holidays);
    //$totalRows_Holidays = mysql_num_rows(Holidays);
    $days_calendar = array();
while ($row_Holidays = mysql_fetch_assoc($Holidays)) {
    $days_calendar[] = $row_Holidays['days_holidays'];}
print_r($days_calendar);

#1


4  

When you do mysql_fetch_assoc($Holidays) you recover one row, if you are executing this before the while loop, then the first row (14) will be escaped.

当你执行mysql_fetch_assoc($ Holidays)时,你恢复了一行,如果你在while循环之前执行它,那么第一行(14)将被转义。

Remove this first mysql_fetch_assoc and all will be ok.

删除第一个mysql_fetch_assoc,一切都会好的。

And do not use mysql_* functions, they are deprecated and will not be included in future versions, use PDO or mysqli_* functions

并且不要使用mysql_ *函数,它们已被弃用,将来不会包含在内,使用PDO或mysqli_ *函数

PS: Maybe this is a comment but i don't have permission

PS:也许这是评论,但我没有获得许可

#2


2  

When you call the first time

当你第一次打电话的时候

 $row_Holidays = mysql_fetch_assoc($Holidays);

You are actually skipping the first row. So, the resulting code should be

你实际上正在跳过第一行。所以,结果代码应该是

 mysql_select_db($database_dbconfig, $dbconfig);
 $query_Holidays = "SELECT substring(data,9,2) AS days_holidays 
      FROM database_holidays     
      WHERE database_holidays.data <> ''";
 $Holidays = mysql_query($query_Holidays, $dbconfig) or die(mysql_error());
 $totalRows_Holidays = mysql_num_rows(Holidays);
 $days_calendar = array();
 while ($row_Holidays = mysql_fetch_assoc($Holidays)) 
 {
     $days_calendar[] = $row_Holidays['days_holidays'];
 }
 print_r($days_calendar);

Ok, and now that you get it working, please consider using mysqli_ functions!! :)

好的,现在你可以使用它,请考虑使用mysqli_ functions !! :)

#3


1  

try this one

试试这个

  $days_calendar = array();
    while ($row_Holidays = mysql_fetch_array($Holidays)) {
        $days_calendar[] = $row_Holidays['days_holidays'];}
    print_r($days_calendar);

#4


1  

Replace everything from

替换所有东西


$row_Holidays = mysql_fetch_assoc($Holidays);

with


$totalRows_Holidays = mysql_num_rows(Holidays);
if ($totalRows_Holidays > 0){
  while ($row_Holidays = mysql_fetch_assoc($Holidays)) {
    $days_calendar[] = $row_Holidays['days_holidays'];
  }
}
print_r($days_calendar);

That first line is where the 14 was called. Using my code resolves the issue.


第一行是14号召唤的地方。使用我的代码解决了这个问题。

Hope this answer helps.

希望这个答案有所帮助

#5


1  

try this ...

尝试这个 ...

    mysql_select_db($database_dbconfig, $dbconfig);
    $query_Holidays = "SELECT substring(data,9,2) AS days_holidays FROM database_holidays   WHERE database_holidays.data <> ''";
    $Holidays = mysql_query($query_Holidays, $dbconfig) or die(mysql_error());
    //$row_Holidays = mysql_fetch_assoc($Holidays);
    //$totalRows_Holidays = mysql_num_rows(Holidays);
    $days_calendar = array();
while ($row_Holidays = mysql_fetch_assoc($Holidays)) {
    $days_calendar[] = $row_Holidays['days_holidays'];}
print_r($days_calendar);