phpでエラーが表示される

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
sinzan

phpでエラーが表示される

#1

投稿記事 by sinzan » 12年前

ツリーカテゴリを作成中なのですが、カテゴリが表示されません
ホスト名、パスワードなどを入力して接続すると「DB Prepare Error...」と表示されます。
原因おわかりのかたご教示の程よろしくお願いします。
SQLは以下です。

create table category (
category_id int not null auto_increment,
parent_id int,
category_name varchar(255),
primary key(category_id)
);


insert into category ( parent_id, category_name )
values ( 0, "すべて" );
insert into category ( parent_id, category_name )
values ( 1, "スポーツ" );
insert into category ( parent_id, category_name )
values ( 2, "サッカー" );
insert into category ( parent_id, category_name )
values ( 2, "ゴルフ" );
insert into category ( parent_id, category_name )
values ( 1, "グルメ" );
insert into category ( parent_id, category_name )
values ( 5, "レストラン(外食)" );



require ( "config.php" );

$index = category_sub_list( $_GET['cat'] );
$value = array();
foreach ( $index as $value ){
echo $value['name'] . " : " . $value['level'] . " <br>";
}

function category_sub_list( $id ){
$ptr_i = 0;
$ptr_s = 1;
$stack = array();
$index = array();

$stack[$ptr_s]['id'] = $id;
$stack[$ptr_s]['level'] = 0;
$stack[$ptr_s]['name'] = '';

$db = new mysqli ( localhost, root, 12345, ec )
or exit( "DB Connect Error..." );
$stmt = $db->prepare( "select category_id, category_name from category
                           where parent_id = ?
                           order by category_id desc" )
or exit( "DB Prepare Error..." );

while ( $ptr_s > 0 ){
$index[$ptr_i] = $stack[$ptr_s];
$ptr_s = $ptr_s - 1;
$stmt->bind_param( 'i', $index[$ptr_i]['id'] )
or exit( "DB Parameter Bind Error..." );
$stmt->execute()
or exit( "DB Access Execute Error..." );
$stmt->bind_result( $r_category_id, $r_category_name );
while ( $stmt->fetch() ){
$ptr_s = $ptr_s + 1;
$stack[$ptr_s]['id'] = $r_category_id;
$stack[$ptr_s]['name'] = $r_category_name;
$stack[$ptr_s]['level'] = $index[$ptr_i]['level'] + 1;
}
$ptr_i = $ptr_i + 1;
}
$stmt->close();
return $index;
}

アバター
みけCAT
記事: 6734
登録日時: 15年前
住所: 千葉県
連絡を取る:

Re: phpでエラーが表示される

#2

投稿記事 by みけCAT » 12年前

PHPはよくわからないのですが、new mysqliのパラメータの指定が違うのではないでしょうか?
http://www.php.net/manual/ja/class.mysqli.phpを見ると、

コード:

$db = new mysqli ( "localhost", "root", "password", "ec", 12345 )
or exit( "DB Connect Error..." );
とするべきのような気がしました。

また、コードはcodeタグで囲んでいただき、きちんとインデントをしていただくと、見やすくて嬉しいです。
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

アバター
h2so5
副管理人
記事: 2212
登録日時: 15年前
住所: 東京
連絡を取る:

Re: phpでエラーが表示される

#3

投稿記事 by h2so5 » 12年前

まずmysqlに正しく接続できているか確かめてください。

このコードでは接続できているかどうかかが判定できません。
phpではnewは必ずオブジェクトを返し、オブジェクトはtrueとして判定されるので exit の部分が実行されることはありません。

コード:

$db = new mysqli ( localhost, root, 12345, ec )
or exit( "DB Connect Error..." );
エラー処理についてはリファレンスを見てください。
http://www.php.net/manual/ja/mysqli.construct.php

sinzan

Re: phpでエラーが表示される

#4

投稿記事 by sinzan » 12年前

$db = new mysqli ( "localhost", "root", "password", "ec", 12345 )
or exit( "DB Connect Error..." );

上記のように変更してみましたが以下のようになりました。
111行目のorder by category_id desc" )にも問題ありでしょうか?

Warning: mysqli::mysqli() expects parameter 5 to be long, string given in C:\xampp\htdocs\ec\include\left_pane.php on line 107

Warning: mysqli::prepare(): Couldn't fetch mysqli in C:\xampp\htdocs\ec\include\left_pane.php on line 111
DB Prepare Error...

アバター
みけCAT
記事: 6734
登録日時: 15年前
住所: 千葉県
連絡を取る:

Re: phpでエラーが表示される

#5

投稿記事 by みけCAT » 12年前

passwordの部分には本当のroot(MySQLのユーザーの話です、念のため)のパスワードをいれてください。
line 107及びline 111はどの部分に当たりますか?

【追記】
sinzan さんが書きました:111行目のorder by category_id desc" )にも問題ありでしょうか?
この発言から考えて、line 107が

コード:

$db = new mysqli ( localhost, root, 12345, ec )
であり、line 111が

コード:

                           order by category_id desc" )
であると推測できました。
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

sinzan

Re: phpでエラーが表示される

#6

投稿記事 by sinzan » 12年前

107が$db = new mysqli ( localhost, root, 12345, ec )
111がorder by category_id desc" ) になります。

YuO
記事: 947
登録日時: 15年前
住所: 東京都世田谷区

Re: phpでエラーが表示される

#7

投稿記事 by YuO » 12年前

ポートの指定までしていますが,正しいポートを指定していますか。
つまり,MySQLのポートは本当に12345ですか。
デフォルトでは,MySQLは3306を使いますが……。

アバター
みけCAT
記事: 6734
登録日時: 15年前
住所: 千葉県
連絡を取る:

Re: phpでエラーが表示される

#8

投稿記事 by みけCAT » 12年前

私が型推論に失敗していて、パスワードが12345なのかもしれません。
だとしたら、

コード:

$db = new mysqli ( "localhost", "root", "12345", "ec" )
とするべきだと思います。
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

アバター
みけCAT
記事: 6734
登録日時: 15年前
住所: 千葉県
連絡を取る:

Re: phpでエラーが表示される

#9

投稿記事 by みけCAT » 12年前

order by category_id descの後に;がついていないのは大丈夫でしょうか?
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

sinzan

Re: phpでエラーが表示される

#10

投稿記事 by sinzan » 12年前

ポートは以下のようになっていました。
なにかが間違ってているのでしょうか?
[Ports]
Port80=BLOCKED
Port443=BLOCKED
Port3306=BLOCKED
Port21=FREE
Port14147=FREE
Port8080=FREE

order by category_id desc" )の後に;をつけるとエラーは消えたのですが
112行目のor exit( "DB Prepare Error..." );が以下のように表示されるようになりました。

Parse error: syntax error, unexpected 'or' (T_LOGICAL_OR) in C:\xampp\htdocs\ec\include\left_pane.php on line 112

YuO
記事: 947
登録日時: 15年前
住所: 東京都世田谷区

Re: phpでエラーが表示される

#11

投稿記事 by YuO » 12年前

sinzan さんが書きました:ポートは以下のようになっていました。
なにかが間違ってているのでしょうか?
何を調べたのかはわかりませんが,MySQLの設定なので,my.iniを調べましょうよ。
現在,MySQLをインストーラーで普通にインストールすると,my.iniは

コード:

%AllUsersProfile%\MySQL\MySQL Server 5.6\my.ini
にあります (MySQL community Server 5.6の場合)。
# xamppではどうなるかは不明。MySQLサービスのコマンドラインを見れば書いてあるかと。

で,my.iniのあるディレクトリで

コード:

find "port" my.ini
とやれば,ポート情報が得られます。
# 正確には,my.iniの[mysqld]セクションのport。
みけCAT さんが書きました:order by category_id descの後に;がついていないのは大丈夫でしょうか?
SQLの末尾に;は不要です。
;は対話型シェルにおける,SQLの終端を表す為に使われている記号にすぎません。
sinzan さんが書きました:order by category_id desc" )の後に;をつけるとエラーは消えたのですが
112行目のor exit( "DB Prepare Error..." );が以下のように表示されるようになりました。

Parse error: syntax error, unexpected 'or' (T_LOGICAL_OR) in C:\xampp\htdocs\ec\include\left_pane.php on line 112
そもそもみけCATさんが指摘された場所と付ける場所が違っています。
PHP文の;は文の終端を意味するので,
sinzan さんが書きました:

コード:

$stmt = $db->prepare( "select category_id, category_name from category
                           where parent_id = ?
                           order by category_id desc" )
or exit( "DB Prepare Error..." )
の,
sinzan さんが書きました:

コード:

$stmt = $db->prepare( "select category_id, category_name from category
                           where parent_id = ?
                           order by category_id desc" )
sinzan さんが書きました:

コード:

or exit( "DB Prepare Error..." )
が別の文になってしまいます。
この結果,後ろの文のor演算子の左側の式がなくなったために,文法エラーが発生しています。
# つまりは,コードの実行すらされていない。

根本的に,or exitとかやってはいけない典型例です。
実行に失敗した場合は,エラーをログに出力するなどしないと,デバッグがそもそもできません。

みけCATさん,h2so5さんが挙げられているmysqliのコンストラクタのページをちゃんと読みましたか。
例1の「オブジェクト指向型」にコンストラクタでのエラーの取得の方法がちゃんと書いてあります。
同様の方法で,prepareでのエラーも対処できます。
# dieすべきかどうかは状況に依るのでここでは評価しない。

sinzan

Re: phpでエラーが表示される

#12

投稿記事 by sinzan » 12年前

my.iniは以下のようになっておりました。

# Example MySQL config file for small systems.
#
# This is for a system with little memory (<= 64M) where MySQL is only used
# from time to time and it's important that the mysqld daemon
# doesn't use much resources.
#
# You can copy this file to
# C:/xampp/mysql/bin/my.cnf to set global options,
# mysql-data-dir/my.cnf to set server-specific options (in this
# installation this directory is C:/xampp/mysql/data) or
# ~/.my.cnf to set user-specific options.
#
# In this file, you can use all long options that a program supports.
# If you want to know which options a program supports, run the program
# with the "--help" option.

# The following options will be passed to all MySQL clients
[client]
# password = your_password
port = 3306
socket = "C:/xampp/mysql/mysql.sock"


# Here follows entries for some specific programs

# The MySQL server
[mysqld]
port= 3306
socket = "C:/xampp/mysql/mysql.sock"
basedir = "C:/xampp/mysql"
tmpdir = "C:/xampp/tmp"
datadir = "C:/xampp/mysql/data"
pid_file = "mysql.pid"
# enable-named-pipe
key_buffer = 16M
max_allowed_packet = 1M
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
log_error = "mysql_error.log"

# Change here for bind listening
# bind-address="127.0.0.1"
# bind-address = ::1 # for ipv6

# Where do all the plugins live
plugin_dir = "C:/xampp/mysql/lib/plugin/"

# Don't listen on a TCP/IP port at all. This can be a security enhancement,
# if all processes that need to connect to mysqld run on the same host.
# All interaction with mysqld must be made via Unix sockets or named pipes.
# Note that using this option without enabling named pipes on Windows
# (via the "enable-named-pipe" option) will render mysqld useless!
#
# commented in by lampp security
#skip-networking
skip-federated

# Replication Master Server (default)
# binary logging is required for replication
# log-bin deactivated by default since XAMPP 1.4.11
#log-bin=mysql-bin

# required unique id between 1 and 2^32 - 1
# defaults to 1 if master-host is not set
# but will not function as a master if omitted
server-id = 1

# Replication Slave (comment out master section to use this)
#
# To configure this host as a replication slave, you can choose between
# two methods :
#
# 1) Use the CHANGE MASTER TO command (fully described in our manual) -
# the syntax is:
#
# CHANGE MASTER TO MASTER_HOST=<host>, MASTER_PORT=<port>,
# MASTER_USER=<user>, MASTER_PASSWORD=<password> ;
#
# where you replace <host>, <user>, <password> by quoted strings and
# <port> by the master's port number (3306 by default).
#
# Example:
#
# CHANGE MASTER TO MASTER_HOST='125.564.12.1', MASTER_PORT=3306,
# MASTER_USER='joe', MASTER_PASSWORD='secret';
#
# OR
#
# 2) Set the variables below. However, in case you choose this method, then
# start replication for the first time (even unsuccessfully, for example
# if you mistyped the password in master-password and the slave fails to
# connect), the slave will create a master.info file, and any later
# change in this file to the variables' values below will be ignored and
# overridden by the content of the master.info file, unless you shutdown
# the slave server, delete master.info and restart the slaver server.
# For that reason, you may want to leave the lines below untouched
# (commented) and instead use CHANGE MASTER TO (see above)
#
# required unique id between 2 and 2^32 - 1
# (and different from the master)
# defaults to 2 if master-host is set
# but will not function as a slave if omitted
#server-id = 2
#
# The replication master for this slave - required
#master-host = <hostname>
#
# The username the slave will use for authentication when connecting
# to the master - required
#master-user = <username>
#
# The password the slave will authenticate with when connecting to
# the master - required
#master-password = <password>
#
# The port the master is listening on.
# optional - defaults to 3306
#master-port = <port>
#
# binary logging - not required for slaves, but recommended
#log-bin=mysql-bin


# Point the following paths to different dedicated disks
#tmpdir = "C:/xampp/tmp"
#log-update = /path-to-dedicated-directory/hostname

# Uncomment the following if you are using BDB tables
#bdb_cache_size = 4M
#bdb_max_lock = 10000

# Comment the following if you are using InnoDB tables
#skip-innodb
innodb_data_home_dir = "C:/xampp/mysql/data"
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = "C:/xampp/mysql/data"
#innodb_log_arch_dir = "C:/xampp/mysql/data"
## You can set .._buffer_pool_size up to 50 - 80 %
## of RAM but beware of setting memory usage too high
innodb_buffer_pool_size = 16M
innodb_additional_mem_pool_size = 2M
## Set .._log_file_size to 25 % of buffer pool size
innodb_log_file_size = 5M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50

## UTF 8 Settings
#init-connect=\'SET NAMES utf8\'
#collation_server=utf8_unicode_ci
#character_set_server=utf8
#skip-character-set-client-handshake
#character_sets-dir="C:/xampp/mysql/share/charsets"

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates

[isamchk]
key_buffer = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M

[myisamchk]
key_buffer = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout

YuO
記事: 947
登録日時: 15年前
住所: 東京都世田谷区

Re: phpでエラーが表示される

#13

投稿記事 by YuO » 12年前

sinzan さんが書きました:my.iniは以下のようになっておりました。
(snip)
sinzan さんが書きました:# The MySQL server
[mysqld]
port= 3306
なので,サーバーポートはデフォルトの3306です。
クライアント(PHP)側は3306に繋げないといけません。
おそらく,ポートを省略すれば3306に繋げると思いますが。

sinzan

Re: phpでエラーが表示される

#14

投稿記事 by sinzan » 12年前

php側を3306に接続するにはどのようにすればよいのでしょうか?

アバター
へにっくす
記事: 634
登録日時: 13年前
住所: 東京都

Re: phpでエラーが表示される

#15

投稿記事 by へにっくす » 12年前

sinzan さんが書きました:php側を3306に接続するにはどのようにすればよいのでしょうか?
貴方は、これまでのレスの、何を見てるんでしょうね?
YuO さんが書きました:おそらく,ポートを省略すれば3306に繋げると思いますが。
と明記されていて、そのレスでは、ちょっと何も考えてないな~というか。
mysqli::__construct
をちゃんと見てますか?

コード:

$db = new mysqli ( "localhost", "root", "password", "ec", 12345 )
or exit( "DB Connect Error..." );
とりあえず、上記ですとポート12345に接続する処理になってます。後は分かるよね。
また、or exit~の書き方もよくないとレスされてるので、ちゃんとヘルプを見てね。
written by へにっくす

sinzan

Re: phpでエラーが表示される

#16

投稿記事 by sinzan » 12年前

コード:


$mysqli = new mysqli('localhost', 'root', '12345', 'ec');

$stmt = $db->prepare( "select category_id, category_name from category
                           where parent_id = ?
                           order by category_id desc" )
if ($mysqli->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') '
            . $mysqli->connect_error);
}

上記のようにしてみましたが表示されませんでした。

アバター
みけCAT
記事: 6734
登録日時: 15年前
住所: 千葉県
連絡を取る:

Re: phpでエラーが表示される

#17

投稿記事 by みけCAT » 12年前

エラーメッセージを教えてください。
MySQLのrootアカウントのパスワードは本当に12345ですか?
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

sinzan

Re: phpでエラーが表示される

#18

投稿記事 by sinzan » 12年前

以下のように表示されました。

Parse error: syntax error, unexpected 'if' (T_IF) in C:\xampp\htdocs\ec\include\left_pane.php on line 110

if ($mysqli->connect_error) {の箇所になります。パスワードに間違いはございません

アバター
みけCAT
記事: 6734
登録日時: 15年前
住所: 千葉県
連絡を取る:

Re: phpでエラーが表示される

#19

投稿記事 by みけCAT » 12年前

コード:

order by category_id desc" )
の後にセミコロンを付けて、

コード:

order by category_id desc" );
としてください。
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

sinzan

Re: phpでエラーが表示される

#20

投稿記事 by sinzan » 12年前

セミコロンをつけましたら以下でエラーが表示されました。

Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs\ec\include\left_pane.php on line 106

$stmt = $db->prepare( "select category_id, category_name from category 106行目

何度もすみません。

アバター
みけCAT
記事: 6734
登録日時: 15年前
住所: 千葉県
連絡を取る:

Re: phpでエラーが表示される

#21

投稿記事 by みけCAT » 12年前

コード:

$stmt = $db->prepare(
を、

コード:

$stmt = $mysqli->prepare(
としてみてください。

自分でエラーメッセージを解釈する努力はしていますか?
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

アバター
へにっくす
記事: 634
登録日時: 13年前
住所: 東京都

Re: phpでエラーが表示される

#22

投稿記事 by へにっくす » 12年前

初めの投稿は$dbで統一されてるのになんで修正したら最初だけ$mysqliになってるんだか。
それだからエラーをなくす努力してるのかな?と思われるのですよ。
サンプルコードをもってきて自分の環境にあわせたのは分かるけど、ちゃんと変数名もチェックしてね。
みけCATさんのように修正するのであれば、他の$dbもちゃんと$mysqliに置き換えてね。
それくらいは分かるよね?
written by へにっくす

sinzan

Re: phpでエラーが表示される

#23

投稿記事 by sinzan » 12年前

以下のエラーが表示されました。

Fatal error: Call to a member function bind_param() on a non-object in C:\xampp\htdocs\ec\include\left_pane.php on line 124

$stmt = $mysqli->prepare( "select category_id, category_name from categoryがなにか原因でしょうか?

コード:

<?php
require ( "config.php" );

$index = category_sub_list( $_GET['cat'] );
$value = array();
foreach ( $index as $value ){
echo $value['name'] . " : " . $value['level'] . " <br>";
}

function category_sub_list( $id ){
$ptr_i = 0;
$ptr_s = 1;
$stack = array();
$index = array();

$stack[$ptr_s]['id'] = $id;
$stack[$ptr_s]['level'] = 0;
$stack[$ptr_s]['name'] = '';

$mysqli = new mysqli('localhost', 'root', '12345', 'ec');
if ($mysqli->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') '
            . $mysqli->connect_error);
}
$stmt = $mysqli->prepare( "select category_id, category_name from category
                           where parent_id = ?
                           order by category_id desc" );

if ($mysqli->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') '
            . $mysqli->connect_error);
}

while ( $ptr_s > 0 ){
$index[$ptr_i] = $stack[$ptr_s];
$ptr_s = $ptr_s - 1;
$stmt->bind_param( 'i', $index[$ptr_i]['id'] );

if ($mysqli->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') '
            . $mysqli->connect_error);
}

$stmt->execute()
or exit( "DB Access Execute Error..." );

$stmt->bind_result( $r_category_id, $r_category_name );
while ( $stmt->fetch() ){
$ptr_s = $ptr_s + 1;
$stack[$ptr_s]['id'] = $r_category_id;
$stack[$ptr_s]['name'] = $r_category_name;
$stack[$ptr_s]['level'] = $index[$ptr_i]['level'] + 1;
}
$ptr_i = $ptr_i + 1;
}
$stmt->close();
return $index;
}
?> 

アバター
みけCAT
記事: 6734
登録日時: 15年前
住所: 千葉県
連絡を取る:

Re: phpでエラーが表示される

#24

投稿記事 by みけCAT » 12年前

$mysqli->connect_errorは接続(オブジェクト生成)時のエラーしかチェックしない可能性を感じました。
http://www.php.net/manual/ja/mysqli.connect-error.php

$mysqli->prepareの後、$stmtがFALSEになっていないかを確認してみてください。
http://www.php.net/manual/ja/mysqli.prepare.php
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

アバター
へにっくす
記事: 634
登録日時: 13年前
住所: 東京都

Re: phpでエラーが表示される

#25

投稿記事 by へにっくす » 12年前

sinzan さんが書きました:$stmt = $mysqli->prepare( "select category_id, category_name from categoryがなにか原因でしょうか?
全角のスペース含んでたらそりゃエラーになるでしょ。まずは半角に置き換えましょう。
それと文字列で"を使うときは、複数行にできたっけ?一行にしましょう。
文字列リテラル-文字列-PHP入門
written by へにっくす

アバター
みけCAT
記事: 6734
登録日時: 15年前
住所: 千葉県
連絡を取る:

Re: phpでエラーが表示される

#26

投稿記事 by みけCAT » 12年前

へにっくす さんが書きました:それと文字列で"を使うときは、複数行にできたっけ?
バージョン依存かもしれませんが、Ideoneでは大丈夫そうです。
http://ideone.com/u5BeXn
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

YuO
記事: 947
登録日時: 15年前
住所: 東京都世田谷区

Re: phpでエラーが表示される

#27

投稿記事 by YuO » 12年前

みけCAT さんが書きました:$mysqli->connect_errorは接続(オブジェクト生成)時のエラーしかチェックしない可能性を感じました。
http://www.php.net/manual/ja/mysqli.connect-error.php
$mysqli->prepareの後、$stmtがFALSEになっていないかを確認してみてください。
http://www.php.net/manual/ja/mysqli.prepare.php
その通りで,prepareは失敗するとFALSEを返します。
エラーの確認はmysqli::$errorを利用するようです。
# 普段PDOをエラー時に例外を送出するモードで使っているので,毎回の例外処理とか面倒臭い……。

閉鎖

“C言語何でも質問掲示板” へ戻る