参考書でphpを勉強中です。XAMPP v3.2.4、Notepad++,windows10を使っています。
ブラウザで「コード//localhost/これから始める人のPHP学習帖\Chapter4\coffee_break_A\4.php」
を実行すると以下のエラーが出て先に行けません。
ユーザーの取得に失敗しました!
rootでログインして現在のデータベースを表示すると以下になっています。
information_schema
mydb
mysql
performance_schema
phpmyadmin
test
testdb
追加したユーザー名でログインして、データベースを表示したいのですが。
期待される表示内容は以下です。
information_schema
test
testdb
// c:\XAMPP\mysql\data\mysql_error.log
2021-01-05 11:47:14 426 [Warning] Aborted connection 426 to db: 'unconnected' user: 'MyName' host: 'localhost' (init_connect command failed)
2021-01-05 11:47:14 426 [Warning] You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''SET NAMES utf8'' at line 1
2021-01-05 11:48:05 428 [Warning] Aborted connection 428 to db: 'unconnected' user: 'MyName' host: 'localhost' (init_connect command failed)
2021-01-05 11:48:05 428 [Warning] You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''SET NAMES utf8'' at line 1
となっています。書き方に問題あるようなのですが、わからないので
これを解決する方法を教えていただきたいです。
php
1 2//localhost/これから始める人のPHP学習帖\Chapter4\coffee_break_A\4.php 3<html> 4<head> 5<meta charset="utf-8"> 6</head> 7<body> 8<?php 9 10 $usr = 'root'; // ユーザー名 11 $pwd = 'SQL6naxxx'; // パスワード(USBWebserverの場合) 12 $host = 'localhost'; // データベース・ホスト名 13 14 $dbName = 'testdb'; // 利用するデータベース名 15 16 $User = 'MyName'; // 追加したいユーザー名 17 $Pass = 'MyPass'; // そのパスワード 18 19 $sql = "GRANT ALL PRIVILEGES ON {$dbName}.* TO {$User}@"; 20 21 $sql .= "{$host} IDENTIFIED BY '{$Pass}' WITH GRANT OPTION;"; 22 23 $link = mysqli_connect($host, $usr, $pwd); // サーバーに接続 24 25 if( mysqli_select_db($link, $dbName)){ // データベースを選択 26 27 mysqli_set_charset($link, 'utf8'); // 文字コードの設定 28 29 mysqli_query($link, $sql); // SQL文を実行 30 31 } else { 32 exit('ユーザーの追加に失敗しました!'); 33 } 34 35 mysqli_close($link); // 接続を切断 36 37 38 // 追加したユーザーで接続し、データベース一覧を表示させて確認 39 $usr = $User; 40 41 $pwd = $Pass; 42 43 $link = @mysqli_connect($host, $usr, $pwd); // サーバーに接続 44 45 if( mysqli_select_db($link, $dbName)){ // データベースを選択 46 47 mysqli_set_charset($link, 'utf8'); // 文字コードの設定 48 49 mysqli_query($link, $sql); // SQL文を実行 50 51 } else { 52 exit('ユーザーの取得に失敗しました!'); 53 } 54 55 // データベース一覧を表示させて確認 56 $sql = 'SHOW DATABASES;'; 57 58 if($result = mysqli_query($link, $sql)){ // SQL文を実行し、結果を取得 59 60 while($row = mysqli_fetch_assoc($result)){ // データを取り出す 61 62 echo $row['Database'] . '<br>'; // キーがDatabaseのデータです! 63 64 } 65 mysqli_free_result($result); // 結果をクリア 66 } 67 mysqli_close($link); // 接続を切断 68 69 70 71 72 /* 実行結果 73 http://localhost/C:\xampp\htdocs\これからはじめる人のPHP学習帖 74 \Chapter4\coffee_breakA/4.php で以下が表示される。 75 76 ユーザーの取得に失敗しました! 77 */ 78 79 80?> 81</body> 82</html>
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/11/27 07:42
退会済みユーザー
2019/11/27 07:44
2019/11/27 07:47
退会済みユーザー
2019/11/27 07:49
退会済みユーザー
2019/11/27 07:50
退会済みユーザー
2019/11/27 07:52
退会済みユーザー
2019/11/27 07:53
2019/11/27 08:06