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);
}

 

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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;
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.