1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PHP5からPHP7、mysql接続処理の変更点

Last updated at Posted at 2025-06-08

PHP5における記述

$conn_id = mysql_connect('localhost', 'root', 'password');
if (!$conn_id) {
    die('接続失敗です。'.mysql_error());
}

$db_selected = mysql_select_db($dbname, $conn_id);
if (!$db_selected){
    die('データベース選択失敗です。'.mysql_error());
}

$Result = mysql_query("SELECT * FROM table1");

if($Result){
    while ($row = mysql_fetch_assoc($Result)) {
        //処理
    }
}

PHP7で、undefined function mysql_connectとエラーとなる。
mysql_connectからmysqli_connectとなる。
PHP7以降の記述。

$conn_id = mysqli_connect('localhost', 'root', 'password','sample_db');
if (!$conn_id) {
    die('接続失敗です。'.mysql_error());
}

$Result = mysqli_query($conn_id,"SELECT * FROM table1");

if($Result){
    while ($row = mysql_fetch_assoc($Result)) {
        //処理
    }
}

参考サイト
https://qiita.com/okamoto0/items/a2568133064b74f837bb

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?