Jump to content

[SOLVED] getting database count from mysql query


heldenbrau

Recommended Posts

I want to count the amount of rows in a database and display them, then increment the number and pass it to a filename.  The filename needs to be called case1, case2, case3, case4, etc.  depending on how many cases are already in the database.  I have tried the following code but I keep getting Parse error: syntax error, unexpected T_VARIABLE

 

$mysqli = new mysqli("localhost", "username", "pasword", "cases");
    if ($mysqli === false) {
      die("ERROR: Could not connect to database. " . mysqli_connect_error());
}
  $sql = "SELECT COUNT(*) FROM 'cases';
    if ($result = $mysqli->query($sql)) {
      $row = $result->fetch_assoc();
      $add = $row;
      $add ++;
      echo "$add";
      $newcase = "case"$add;
    }else{
      die("ERROR: Could not execute query: $sql. " . $mysqli->error);
}

 

Well, you have a lot of errors in that script. First, it only get the rows 1 time since you don't have any sort of loops. Second, there's a parse error: "case"$add; which should be concatenated with a dot:  "case".$add; Third, you try to select the table with its name surrounded in quotes. Remove the quotes after FROM.

 

Here's a much better code. But, right now, every loops overwrites the variable $newcase which you should handle to your likings.

 

<?php
$mysqli = new mysqli("localhost", "username", "pasword", "cases");
    if ($mysqli === false) {
      die("ERROR: Could not connect to database. " . mysqli_connect_error());
}

$sql = "SELECT * FROM cases";
$count = 0;
$result = $mysqli->query($sql);

while ($row = mysqli_fetch_array($result)) {
    $count++;
    echo $count;
    $newcase = 'case'.$count;
}

// die("ERROR: Could not execute query: $sql. " . $mysqli->error);
?>

I changed the code to below, but the $count isn't incrementing, it just stays at 0 even when there is an entry in the database.

 

<?php

$mysqli = new mysqli("localhost", "username", "password", "cases");
    if ($mysqli === false) {
      die("ERROR: Could not connect to database. " . mysqli_connect_error());
}

  $sql = "SELECT * FROM cases";
  $count = 0;
  $result = $mysqli->query($sql);

  while ($row = mysqli_fetch_array($result)) {
    $count++;
}
    if ($count==0){
    $count++;
    }
    $newcase = 'case'.$count;
    echo $newcase;
?>

a much easier way to get the count of rows in your table is to use the mysqli_num_rows function

this will get your count:

$sql = "SELECT * FROM cases";

$result = $mysqli->query($sql);
$count = mysqli_num_rows($result)

 

EDIT: syntax error and also link to function: http://us2.php.net/manual/en/mysqli-result.num-rows.php

well it doesn't get incremented because in your code

 

if ($count==0){
    $count++;
    }

 

this never runs because count is not zero. if you want to always increment the count function, then remove the if statement and simply have $count++

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.