-2

I'm new to PHP and am looking for some beginner help. I don't understand why this simple while loop is not working. I've checked for syntax errors with various variations and don't think that's the case.

The error says:

Fatal error: Uncaught Error: Call to undefined function mysql_fetch_assoc() in /opt/lampp/htdocs/basic-procedural-php-project/index.php:77 Stack trace: #0 {main} thrown in /opt/lampp/htdocs/basic-procedural-php-project/index.php on line 77

The code is as follows:

<?php include 'db.php'; ?>

<?php
// Fetch data from db
$query = 'SELECT * FROM data';
$numbers = mysqli_query($con, $query);
?>

<?php while ($row = mysql_fetch_assoc($numbers)) : ?>
            <p>whatever <?php echo $row['t_name'] ?></p>
          <?php endwhile; ?>

The database is as follows:

DB NAME: timesNumbers
DB TABLE NAME: data
COLUMNS: t_id, t_name, t_firstNo, t_secondNo

Can someone explain what the error means, the problem and how to fix it? I feel like it might be something basic that I'm not getting. Thanks for and help here.

Also the db.php file includes:

<?php

// Connect
$con = mysqli_connect("localhost", "root", "", "timesNumbers");

// Test Connection
if (mysqli_connect_errno()) {
    echo 'Failed: '.mysqli_connect_error();
}

?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
user8758206
  • 2,106
  • 4
  • 22
  • 45

1 Answers1

2

You can't mix the usage of mysql and mysqli libraries/extensions. I guess you don't even have the mysql library/extension installed since you get that error.

You shouldn't use it anyway. Stick with mysqli or PDO.

To fetch data with mysqli, check the manual at http://php.net/manual/en/mysqli-result.fetch-assoc.php

PDO has my preference.

EDIT: Now you've mentioned the version of php is 7.1.9, it's for sure you don't have the mysql extension installed since its removed from php 7 after being deprecated for a long time.

DigiLive
  • 1,093
  • 1
  • 11
  • 28
  • ok thanks, is it ok to use mysqli_fetch_assoc instead of mysql_fetch_assoc? – user8758206 Oct 27 '18 at 19:57
  • 1
    *"I guess you don't even have the mysql library/extension installed since you get that error."* - [*"PHP version is 7.1.9 – user8758206"*](https://stackoverflow.com/questions/53025692/simple-while-loop-returning-a-call-to-undefined-function-error#comment92953230_53025692). That would be a definite "no", since mysql_ has been deleted in that version of PHP. – Funk Forty Niner Oct 27 '18 at 19:57
  • ok, thank you. Will read up on that – user8758206 Oct 27 '18 at 19:59