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

Wisam Ahmed
  • 145
  • 1
  • 1
  • 13

3 Answers3

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());
}
Adeojo Emmanuel IMM
  • 2,104
  • 1
  • 19
  • 28
-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);
Mohamed hesham
  • 130
  • 1
  • 12
-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);
  ?>
pramod24
  • 1,096
  • 4
  • 17
  • 38