1

Im using XAMPP. and im trying to dump my db_data on the webpage. the error message is "call to undefined function mysql_connect". should i import some file?

<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);

if(! $conn ) {
  die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT *
  FROM uam';

mysql_select_db('demo');
$retval = mysql_query( $sql, $conn );

if(! $retval ) {
  die('Could not get data: ' . mysql_error());
}

while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
  echo "Tutorial ID :{$row['id']}  <br> ".
     "username: {$row['username']} <br> ".
     "email: {$row['email']} <br> ".
     "password : {$row['password']} <br> ".
     "--------------------------------<br>";
} 
echo "Fetched data successfully\n";
mysql_close($conn);
?>

Or should i make changes in my code??

7
  • 6
    Don't use the mysql functions. It's undefined because it's been removed from PHP 7. Use mysqli or PDO instead. Commented Sep 11, 2017 at 14:45
  • @Don'tPanic This should be an answer. Commented Sep 11, 2017 at 14:46
  • 1
    There's also 0 need for you to ever output a password, encrypted or not. Commented Sep 11, 2017 at 14:46
  • @Mark The comment was just a placeholder while I found the duplicate to CV. Commented Sep 11, 2017 at 14:47
  • @Don'tPanic My only concern about linking to that duplicate is that we don't know if he's really using PHP7. Commented Sep 11, 2017 at 14:48

3 Answers 3

1

avoid using MySQL functions, they are now depreciated that is why you get the undefined error, and its also not more supported in PHP 7.

use MySQLI function by changing your config to the code below

$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = '';
$dbname = 'dbname';

$conn = new mysqli($dbhost , $dbuser, $dbpass,  $dbname);

if(! $conn ) {
  die('Could not connect: ' . mysql_error());
}
Sign up to request clarification or add additional context in comments.

Comments

-1

That happened because the new versions of PHP no support mysql_connect try using mysqli_connect

$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = '';
$dbname = 'demo';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);

3 Comments

OR you could/should use PDO.
Yes I use PDO because it is more protect bu he wanted to work with mysqli_connect so I answered by this
No, he was working with an outdated API. You're making an assumption.
-1
     <?php
     $dbhost = 'localhost:3306';
     $dbuser = 'root';
     $dbpass = '';
     $dbname = 'TUTORIALS';
     $conn = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname);

     if(! $conn ) {
        die('Could not connect: ' . mysqli_error());
     }
     echo 'Connected successfully<br>';
     $sql = 'SELECT name FROM tutorials_inf';
     $result = mysqli_query($conn, $sql);

     if (mysqli_num_rows($result) > 0) {
        while($row = mysqli_fetch_assoc($result)) {
           echo "Name: " . $row["name"]. "<br>";
        }
     } else {
        echo "0 results";
     }
     mysqli_close($conn);
  ?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.