PHPでmysqlの値が表示できません
- 評価
- クリップ 1
- VIEW 229
前提・実現したいこと
phpで自作で作ったデータベースにアクセスしてデータを表示したいです.
発生している問題・エラーメッセージ
mysqlにアクセスできていないみたいです.ローカルサーバーにアクセスすると真っ白の状態
エラーメッセージ
該当のソースコード
データベースはこのような感じです
mysql> select * from user;
+------+
| name |
+------+
| yuma |
+------+
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8"/>
<title>Test_PHP</title>
</head>
<body>
<?php
$hostname = "localhost";
$username = "root";
$password = "パスワード";
$dbname = "test";
$connect = mysql_connect($hostname, $username, $password);
mysql_select_db($dbname);
$sql = "select * from user";
$sqlq = mysql_query($sql, $connect);
while($row = mysql_fetch_array($sqlq)){
echo $row['col1'];
}
mysql_free_result($sqlq);
mysql_close($connect);
?>
</body>
</html>
vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.
# Every Vagrant development environment requires a box. You can search for
# boxes at https://vagrantcloud.com/search.
config.vm.box = "CentOS72"
# Disable automatic box update checking. If you disable this, then
# boxes will only be checked for updates when the user runs
# `vagrant box outdated`. This is not recommended.
# config.vm.box_check_update = false
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
# NOTE: This will enable public access to the opened port
# config.vm.network "forwarded_port", guest: 80, host: 8080
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine and only allow access
# via 127.0.0.1 to disable public access
# config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"
# Create a private network, which allows host-only access to the machine
# using a specific IP.
config.vm.network "private_network", ip: "192.168.33.10"
# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
# config.vm.network "public_network"
# Share an additional folder to the guest VM. The first argument is
# the path on the host to the actual folder. The second argument is
# the path on the guest to mount the folder. And the optional third
# argument is a set of non-required options.
# config.vm.synced_folder "../data", "/vagrant_data"
# Provider-specific configuration so you can fine-tune various
# backing providers for Vagrant. These expose provider-specific options.
# Example for VirtualBox:
#
# config.vm.provider "virtualbox" do |vb|
# # Display the VirtualBox GUI when booting the machine
# vb.gui = true
#
# # Customize the amount of memory on the VM:
# vb.memory = "1024"
# end
#
# View the documentation for the provider you are using for more
# information on available options.
# Enable provisioning with a shell script. Additional provisioners such as
# Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
# documentation for more information about their specific syntax and use.
# config.vm.provision "shell", inline: <<-SHELL
# apt-get update
# apt-get install -y apache2
# SHELL
config.vm.synced_folder ".", "/vagrant", mount_options:['dmode=777','fmode=777']
end
試したこと
sqlを用いずに<echo "hello"; ?>
などはちゃんと表示されます.
phpinfo();でも調べたところmysqlの項目があったため,インストールはされてると思います・
補足情報(FW/ツールのバージョンなど)
mysqlのバージョンを確認しようと思ったら下記のような表示がされました.これが原因なんでしょうか?どなたかお願いします.
[vagrant@localhost ~]$ mysqld -v
2018-05-11T10:09:33.117204Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 5000)
2018-05-11T10:09:33.117831Z 0 [Warning] Changed limits: table_open_cache: 431 (requested 2000)
2018-05-11T10:09:33.506578Z 0 [ERROR] --verbose is for use with --help; did y ou mean --log-error-verbosity?
2018-05-11T10:09:33.506848Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (s ee documentation for more details).
2018-05-11T10:09:33.508534Z 0 [Warning] Can't create test file /var/lib/mysql /localhost.lower-test
2018-05-11T10:09:33.509207Z 0 [Note] mysqld (mysqld 5.7.22) starting as proce ss 15421 ...
2018-05-11T10:09:33.535138Z 0 [Warning] Can't create test file /var/lib/mysql /localhost.lower-test
2018-05-11T10:09:33.535257Z 0 [Warning] Can't create test file /var/lib/mysql /localhost.lower-test
2018-05-11T10:09:33.536854Z 0 [ERROR] Could not open file '/var/log/mysqld.lo g' for error logging: Permission denied
2018-05-11T10:09:33.537036Z 0 [ERROR] Aborting
2018-05-11T10:09:33.537133Z 0 [Note] Binlog end
2018-05-11T10:09:33.537857Z 0 [Note] mysqld: Shutdown complete
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
checkベストアンサー
0
がると申します。
エラーを確認する、簡単な手順を1つ。
まず、今あるPHPファイルを仮に「mysql.php」である、と仮定します。
今あるPHPファイルとは別のファイルを用意してください。test.phpとしておきましょう。mysql.phpと同じフォルダに置きます。
<?php
ini_set('display_errors', 1 );
error_reporting(-1);
//
include('./mysql.php');
このようにして、1つファイルで包んで起きます。
あとはブラウザで「test.php」を呼ぶと、おそらくなにがしかのエラーメッセージが出てくると思いますので。
それを貼り付けると、多分、もう少し有効な回答が得られるのではないか、と思います。
以上、何かの参考にでもなれば幸いです。
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
15分調べてもわからないことは、teratailで質問しよう!
- ただいまの回答率 90.36%
- 質問をまとめることで、思考を整理して素早く解決
- テンプレート機能で、簡単に質問をまとめられる
質問への追記・修正、ベストアンサー選択の依頼
mts10806
2018/05/11 19:15
パスワードなどの情報は適当にマスクかけるか空にされた方が良いです。あまりにリアルなものだと何かで悪用される可能性がないとも言い切れません。(できれば削除依頼後、新しく質問を立て直されるのをすすめます)
mts10806
2018/05/11 19:17
回答がすぐつきそうな内容ですが…col1→name
Lulucom
2018/05/11 19:57
mysqlのバージョンを表示するときのオプションは -v ではなく --version だと思います。
muscle1100
2018/05/11 19:58
ご指摘ありがとうございます.削除リクエストをしました.名前をnameに変えたのですが,やはりうまくいかないです...
muscle1100
2018/05/11 20:00
そうでしたか.すみません!!
date
2018/05/11 20:02
PHPのバージョンを入れてください mysql_connect が使えないかも
muscle1100
2018/05/11 20:07
phpのバージョンは5.4.16です.
muscle1100
2018/05/11 20:08
phpのモジュールからmysqlが消えていたんですが,勝手に消えることなどありますか?
mts10806
2018/05/11 21:45
var_dump($row); の出力結果を教えてください。 あと非推奨となっているmysql_*系ではなくmysqli_*系に修正しましょう(mysql_*系だから情報がとれてないというわけではありません)
mts10806
2018/05/11 21:46
MySQLは起動してますよね?PHPから実行したときに他にエラーが出ていないか確認してください。
muscle1100
2018/05/12 07:08
反応遅れて申し訳ないです.var_dump($row);をしても何もはんのうがありませんでした.
muscle1100
2018/05/12 07:22
ini_set('display_errors',1); error_reporting(E_ALL): とphpの中に入れるとこのページは動作していませんとでます.