doutan1875 2014-11-10 20:27
浏览 30
已采纳

确定PHP mysqli fetch是否返回数据

I'm trying to take the following code from the PHP website and rewrite to echo out if the returns my SQL data is null.

<?php
 $mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
   printf("Connect failed: %s
", mysqli_connect_error());
   exit();
}

 $city = "Amersfoort";

 /* create a prepared statement */
 if ($stmt = $mysqli->prepare("SELECT District FROM City WHERE Name=?")) {

/* bind parameters for markers */
$stmt->bind_param("s", $city);

/* execute query */
$stmt->execute();

/* bind result variables */
$stmt->bind_result($district);

/* fetch value */
$stmt->fetch();

printf("%s is in district %s
", $city, $district);

/* close statement */
$stmt->close();
}

/* close connection */
$mysqli->close();
?>

I'm having issues figuring out the right conditional to do this. Just looking for idea's on how to further optimize this code.

  • 写回答

2条回答 默认 最新

  • duan117890 2014-11-10 20:38
    关注
    $rows = $stmt->rowCount();
    echo $rows;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 论文AlphaTensor复现(有偿)
  • ¥15 (有偿)在ANSYS中 .anf文件
  • ¥45 关于#芯片#的问题:组合逻辑电路设计
  • ¥15 基与机器学习和时间序列分析预测养老服务需求趋势
  • ¥100 求连续两帧图像在水平和垂直上偏移
  • ¥15 mysql全文索引查找指定必须关键词word无效
  • ¥15 Verilog hdl密码锁设计
  • ¥35 基于python的有ssl加密传输的socket聊天室
  • ¥15 数码管亮度控制器设计
  • ¥15 kafka客户端跨网段访问,看日志提示连接的还剩内网地址,且访问不通