我想要一些简短的脚本帮助我创建用户ID。我使用今天的日期创建了唯一的ID。我对数据库运行查询以检查是否存在包含今天日期的任何ID。
如果今天的日期没有唯一身份证,那么我今天将当天的第一个ID插入日期。如果有包含今天日期的ID,我将该ID增加1并将该ID插入数据库。
这很简单。我遇到的唯一问题是获取包含今天日期的行数组。
//use today's date for ID
$idDate = date(Ymd);
$param = "{$idDate}%";
//retrieve all rows with today's date in ID
$find = $mysqli->prepare("SELECT * FROM users WHERE userid LIKE ? ");
$find->bind_param("s", $param);
$findresult = $find->execute();
if(!$findresult){
die('Invalid query : ' . mysqli_error($mysqli));
}
$find->store_result();
查询正在检索某些内容,因为num_rows
至少计算了1行,而mysqli_error没有为该特定查询提供任何错误。
//count the rows
$idcount = $find->num_rows();
//if no rows contain today's date in IDs
if($idcount == 0){
$userid = $idDate . '00';
}
else{
这给了我一个错误:mysqli_fetch_array()期望参数1是mysqli_result,给定对象
//store today's IDs in array
while($found = mysqli_fetch_array($find)){
$todaysids[] = array($found['userid']);
}
//find highest value
$hival = max($todaysids);
//add 1 to highest value
$userid = $hival++;
}
如何检索查询结果并获取数组?
答案 0 :(得分:1)
您在代码中混合使用OO和过程样式。您可以使用get_result
并坚持OO风格:
$findresult = $find->execute();
$result = $find->get_result();
while ($found = $result->fetch_array(MYSQLI_ASSOC))
...
答案 1 :(得分:0)
您可以自己使用查询中的计数行。
$find = $mysqli->prepare("SELECT col1,col2,col3,col4 , count(*) as counts FROM users WHERE userid LIKE ? ");
^^^^^^^^^^^^^^^^^--// for counting rows
$find->bind_param("s", $param);
$find->execute();
$find->store_result();
$find->bind_result( $col1 , $col2,$col3,$col4,$counts); // bind your variables here
$find->fetch() ;
echo $counts ; // will give you number rows here.
echo $col1 ;
编辑:
while($row = $find->fetch() ;)
{
echo $counts ; // will give you number rows here.
echo $col1 ;
}