0

I'm using PHP and a MySQL database to build a website.

However, I find PHP quite horrible compared to other languages I have learnt previously. Largely because I seem to find a lot of instructions online which are over 5 years old and they just don't work.

For example the accepted answer here: select count(*) from table of mysql in php

I have this code so far, but it gives me error (undefined index $recordCount = $row['totalEntries'];).

// create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// check connection
if ($conn->connect_error) {
die("connection failed: " . $conn->connect_error);
}


$sql = "SELECT * FROM games ORDER BY id DESC LIMIT $startRow, $rowsPerPage";
$result = $conn->query($sql);

$sql = "SELECT COUNT(*) as totalEntries FROM games";
$results = $conn->query($sql);
$row = $result->fetch_assoc();
$recordCount = $row['totalEntries'];
echo "<br/> record count = " . $recordCount;



$totalPages = ceil($recordCount / $rowsPerPage);
$pagination = "<div class='pagination'>";

for ($i = 0; $i <= $totalPages; $i++) {
 $pagination .= "<a href='index.php?page=" . $i . "'>" . $i . "</a>";
echo 'wtf!';
}

$pagination .= "</div>";
echo ' <br/> pagination = ' . $pagination;

I also tried:

  $recordCount = Count($row);

and

   $recordCount = Count($result->num_rows);

and many other parameters passed into Count but they always return either 1 or 7(which is the number of COLUMNS)

I guess I find PHP so hard to learn because I don't have a good IDE for it and am using Notepad++. Can anyone tell me how we get the Count of all entries in the table after I have done the SELECT statement as in the above code?

halfer
  • 19,824
  • 17
  • 99
  • 186
Big T Larrity
  • 221
  • 2
  • 9
  • 1
    Hint: When you run queries, check for errors. Such practices have nothing to do with the language you are using. – Gordon Linoff Jun 30 '18 at 12:01
  • ok, well I have changed the SELECT wording many times over and got rather confused in the code now. I have also had to change the way the connection and query is made because many of the tutorials are wrong. this may be why I have errors. I've tried following w3 documentation but stilll can't find it. So do you know what the error is exactly that causing the array length count to be wrong (ie. always 1) – Big T Larrity Jun 30 '18 at 12:05
  • the method of running queries and basically everything I can see in PHP is extremely unelegant to put it politely and its very hard for me to debug the errors in any meaningful way – Big T Larrity Jun 30 '18 at 12:08
  • 1
    If you `print_r($row);` after the fetch what does it show you? – Dave Jun 30 '18 at 12:12
  • hi mate, it says this: "Array ( [id] => 74 [timestamp] => 2018-06-29 18:27:52 [title] => ........." – Big T Larrity Jun 30 '18 at 12:13
  • @SuperMegaBroBro . . . It is disappointing that you take a hint on good programming practice as offensive. You are not necessarily going to learn good practice from answers on any forum, unless you figure out how to discern the answers. But you can start with the documentation: http://php.net/manual/en/mysqli.error.php. – Gordon Linoff Jun 30 '18 at 12:14
  • Sorry Gordon, honestly just feels like that was mocking me and really it didnt help me one bit. I apologise if you were in fact trying to help me and i am just too stupid to see how ...also have been at this problem for at least 10 maybe 15 hours without any forward movement now. so my temper isnt great and again i apologise, not your fault ofc but if its just a preach about reading a manual i dont need it today cheers – Big T Larrity Jun 30 '18 at 12:15
  • @Dave please does the debug printout mean anything to you that can help me solve my problem? many thanks – Big T Larrity Jun 30 '18 at 12:20
  • 1
    That looks like the result of your 1st query not the 2nd one where you want a count of entries. Comment out your first query completely, leave the `print_r` where I suggested and see what you get. – Dave Jun 30 '18 at 12:26
  • thanks again Dave, it still says "Array ( [COUNT(id)] => 79 ) " however it doesnt now continue with all the other parts. So you where correct. BUT its weird becoz print_r($row) returns the above, but then '$recordCount = Count($row);' makes recordCount return as 1 – Big T Larrity Jun 30 '18 at 12:33
  • THE ANSWER IS HERE: https://stackoverflow.com/questions/51115033/how-to-debug-php-mysql-countid-returning-1-instead-of-total-entires-value – Big T Larrity Jun 30 '18 at 12:55

1 Answers1

1
    $conn = new \mysqli('127.0.0.1', 'dev', 'SoMuchDev', 'test', '3306');
    $result = $conn->query('select * from test');
    while ($row = $result->fetch_assoc()) {
        $res[] = $row;
    }
    $res['total'] = $result->num_rows;
    echo "<pre>";
    var_export($res);die();

http://php.net/manual/en/class.mysqli-result.php

As for editors, PhpStorm is widely used but afaik there's only a 30 day trial use. You could also give notepad ++ a try, I think it has some php plugins for autocomplete and what not.

halfer
  • 19,824
  • 17
  • 99
  • 186
Sebi Ilie
  • 86
  • 5
  • sorry for sounding so agressive and I take onboard what you are saying, it surely hasn't/won't help my case or increase my chances of receiving decent help. Also thanks for your answer, it looks a little different from my existing code and so I am going to go away and try to adapt it into my project. ....Also I'm using Notepad++ already and it is ok, I didnt realise about plugins I will search some up. might well check up phpstorm too as I think the more auto features I have the better becoz my basic mind has become reliant on them for c# and java and thats why i find php so difficult. thanks – Big T Larrity Jun 30 '18 at 12:47
  • SOMEONE HERE ANSWERED IT AND I ONLY HAD TO CHANGE A FEW THINGS (hoping it will help someone else one day!): https://stackoverflow.com/questions/51115033/how-to-debug-php-mysql-countid-returning-1-instead-of-total-entires-value – Big T Larrity Jun 30 '18 at 12:55