Here's my code:
$host = "localhost";
$user = "root";
$pw = "";
$dbName = "mathgame";
$tblName = "fragen";
// mit mysql db verbinden
$con = mysqli_connect($host, $user, $pw, $dbName);
if ($con->connect_error) {
die ("Connection failed: " . $con->connect_error);}
// Datenanfrage an db
$result = mysqli_query($con, "select id from $tblName");
$array = mysqli_fetch_array($result);
echo count($array["id"]);
Comments are in German, but I think you get what the program does. So my problem is, that there are 8 "elements" in the fragen table. But when I count the array it returns one. What did I do wrong?
mysqli_fetch_array
fetches a single row as an array, where each element is a column. You could use mysqli_num_rows
to get the number of rows from the current query:
$result = mysqli_query($con, "select id from $tblName");
echo mysqli_num_rows($result);
Or, better yet, let the database do the heavy lifting for you:
$result = mysqli_query($con, "SELECT COUNT(*) FROM $tblName");
$array = mysqli_fetch_array($result);
echo $array[0];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.