-1

Hye, i am new in PHP, i have error that can't connect with database, can anyone look at my code if i miss what code i was wrong.

    <?php
    $dbconnection = "localhost";
    $dbusername = "root";
    $dbpassword = "";
    $dbdatabase = "aduan";

    $conn = mysql_connect($dbconnection, $dbusername, $dbpassword, $dbdatabase);

    // Check connection
    if(! $conn ) {
    die('Could not connect: ' . mysql_error());
    }

    $sql = "SELECT * FROM unsafeact";
    $result = mysql_query($sql,$conn) or die(mysql_error());

    // output data of each row
    while($row = mysql_fetch_assoc($result)) {
    echo "<tr><td>" . $row["PEEViolation"]. "</td><td>" . $row["IgnoringSafetySignboard"] . "</td><td>"
    . $row["RuleViolation"] . $row["WrongToolEquipmentMachinery"] . "</td><td>" . $row["ImproperMaterialHandling"] . "</td><td>" . $row["UseFaultyToolsEquipmentVehicle"] . "</td><td>" . $row["PositiveFinding"] . "</td><td>" . $row["Others1"] . "</td></tr>";
  }
    mysql_close($conn);
    ?>

P/S: my company still using old code which is mysql, not mysqli. So i don't know what wrong in my code that can make it cannot connect with database. I'm really appreciate with your kindness. Thank you

Mr Haikal
  • 13
  • 6
  • 1
    The absolute **first thing** you want to do is get your company to upgrade from MySQL to MySQLi or PDO. MySQL was deprecated from PHP 5 in 2013 for security reasons, and was removed from PHP 7 (the [*only supported version*](http://php.net/supported-versions.php) of PHP) back in 2015. There's literally **no point** in trying to connect using the old protocol, as under MySQL your database has practically **zero** security until you upgrade; a hacker could get access to your entire database with ridiculous ease. – Obsidian Age Oct 09 '19 at 02:55
  • i want to tell that sir, but i'm only intern student :(. – Mr Haikal Oct 09 '19 at 02:59
  • but the add stuff into database still work, now im moving to getting data from database but it doesn't work. That why im ask in this community. Sorry about that sir :(. – Mr Haikal Oct 09 '19 at 03:01
  • is that PDO can work with old version PHP? i don't know if i can learn it in short time since i'm also new in PHP. – Mr Haikal Oct 09 '19 at 03:05
  • 1
    The 4th parameter of [mysql_connect()](https://www.php.net/manual/en/function.mysql-connect.php) function is not for select database, you have to use [mysql_select_db()](https://www.php.net/manual/en/function.mysql-select-db.php) function to select database. I think you mixing between `mysqli_*` and `mysql_*` lib – catcon Oct 09 '19 at 03:07
  • @catcon thank, it work. :) – Mr Haikal Oct 09 '19 at 03:12

2 Answers2

0

According to PHP manual for mysql_connect() the 4th parameter is not used for identify the database name but new_link:

If a second call is made to mysql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned. The new_link parameter modifies this behavior and makes mysql_connect() always open a new link, even if mysql_connect() was called before with the same parameters. In SQL safe mode, this parameter is ignored.

to select database using mysql_*, you have to use mysql_select_db()

$dbconnection = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbdatabase = "aduan";

$conn = mysql_connect($dbconnection, $dbusername, $dbpassword);
mysql_select_db($dbdatabase, $conn);

Ideally, you don't want to use mysql_* anymore since it deprecated and removed from php7.

catcon
  • 1,295
  • 1
  • 9
  • 18
  • I see, that the problem. Noted! thank you for great help :D. Yaa, the truth is i don't whether what i learn now can give benefit to me in future since it is old version and not all website can give old version example. So it give me confusing alot to understand how it work. – Mr Haikal Oct 09 '19 at 03:28
-1
  $sql = "SELECT * FROM aduan.unsafeact";

try this, or make a condition if your code is connected in your database. sorry if wrong. tnx :D

Jervz09
  • 121
  • 9