php - Shorten the code without a prepared statement -
i want check if number present in column col
, run mysqli_num_rows
. far working following code:
//$conn = connection mysql. $row1sql = "select * chktable col='1'"; $row2sql = "select * chktable col='2'"; $row3sql = "select * chktable col='3'"; $row4sql = "select * chktable col='4'"; $query1sql = $conn->query($row1sql); $query2sql = $conn->query($row2sql); $query3sql = $conn->query($row3sql); $query4sql = $conn->query($row4sql); $num1 = mysqli_num_rows($query1sql); $num2 = mysqli_num_rows($query2sql); $num3 = mysqli_num_rows($query3sql); $num4 = mysqli_num_rows($query4sql);
i edit other code based on $num
.
how can shorten code? can use for
statement? how use if applicable? or other way shorten code?
thanks.
this col value , number of rows of it, col values 1,2,3,4
$query = $conn->query("select col, count(*) num chktable group col col in (1,2,3,4);"); while($row = $query->fetch_object()) echo "col: ".$row->col.", num:".$row->num;
you can whatever check want inside while loop
Comments
Post a Comment