PHP+MYSQLについての質問|重複する電話番号の件数を表示する方法

このQ&Aのポイント
  • PHP+MYSQLを使用して、入力された電話番号がデータベースに既に登録されている場合、重複する件数を表示する方法について質問です。
  • 現在、実装しようとしているコードではうまくいかないため、解決策を探しています。
  • 具体的な実装例として、test.class.phpとtest.phpのコードを示しました。
回答を見る
  • ベストアンサー

PHP+MYSQLについて質問です。

PHP+MYSQLについて質問です。 フォームに入力した電話番号が、 データベースに登録されている電話番号と重複している場合重複している件数を表示するようにしたいのですが、どうにもうまくいきません。 どなたかわかる方いらっしゃいませんか? 現状がこんな感じになっています。 よろしくお願いします。 ■test.class.php class test{ public function counttest($tel){ $qstring = "select (*) from _test where tel = '$tel'"; $qresult = mysql_query($qstring); if(!$fresult = mysql_fetch_row($qresult)){ $this->errorm = "該当データなし"; return false; }else{ $this->telcount = $fresult[0]; } } } ■test.php require_once ("test.class.php"); require_once ("Smarty.class.php"); $settings = new test; $smarty = new Smarty(); $telcount = $test->counttest($tel); $smarty->assign("telcount",$telcount); $smarty->display("top.tpl");

noname#244246
noname#244246
  • PHP
  • 回答数2
  • ありがとう数16

質問者が選んだベストアンサー

  • ベストアンサー
  • Li2CO3
  • ベストアンサー率54% (6/11)
回答No.2

counttest()が該当データ有のとき値を返さないからでしょう。

その他の回答 (1)

  • duron
  • ベストアンサー率77% (73/94)
回答No.1

mysql_num_rowsで結果の行数を取得、もしくはSQLをselect count(*)~に変えて該当行数を取得してください。

noname#244246
質問者

お礼

$this->telcount = $fresult[0]をretrunで返したらうまくいきました。 ありがとうございます。

関連するQ&A

  • Smartyに関しまして

    require_once("Smarty/Smarty.class.php"); $smarty = new Smarty(); $smarty->template_dir = "./templates"; $smarty->compile_dir = "./templates_c"; $smarty->assign("message", "こんばんは"); $a = array('hello','php','html'); $smarty->assign('a',$a); $smarty->display("index.tpl"); これでindex.tplで出力すると日本語部分が「?????」になってしまいます。 何か設定がいるんでしょうか??

    • ベストアンサー
    • PHP
  • Smartyについて教えてください。

    Smartyについて教えてください。 参考書によってテンプレートの存在するディレクトリを指定している場合(1)と していない場合(2)がありますが、 どちらが正しいのでしょうか? (1) ********************************************* require_once('smarty/libs/Smarty.class.php'); $smarty = new Smarty(); $smarty->template_dir = "template02/"; $smarty->compile_dir = "compile/"; $smarty->display("content.tpl"); ********************************************* (2) ********************************************* require_once('smarty/libs/Smarty.class.php'); $smarty = new Smarty(); $smarty->display("content.tpl"); *********************************************

    • ベストアンサー
    • PHP
  • こんにちは、PHP初心者です。

    こんにちは、PHP初心者です。 Smartyを利用してWebサイトを構築中です。 Smarty.classの派生クラスを作成し、コンストラクタ内でDB接続オブジェクトを privateプロパティ($_db)に設定しているのですが$_dbの値が アクセサメソッドを通じてgetすると空白になってしまいます。 色々やってみましたが理由が分からず、 ためしにコンストラクタ内でクエリを実行したところテーブルから値を取得できました。 なぜコンストラクタ外に出るとprivateプロパティの値が取得できなくなるのか分かりません。 どなたかご教示ください。 どうぞよろしくお願いします。 下記ソースです。 *(Smarty.classの派生クラス)********************************************** <?php require_once("DB.php"); require_once("Smarty.class.php"); class MySmarty extends Smarty{ private $_db; public function __construct(){ parent::__construct(); $this->template_dir="C:/xampp/modules/access_control/templates"; $this->compile_dir="C:/xampp/modules/access_control/templates_c"; $this->config_dir="C:/xampp/config"; $this->config_load("access_control.conf",basename($_SERVER['SCRIPT_NAME'],".php")); $tihs->_db=DB::connect($this->get_config_vars('db_string')); if (PEAR::isError($db)) { die($db->getMessage()); } } public function __destruct(){ $this->_db->disconnect(); } public function get_db(){return $tihs->_db;} } ?> *********************************************** *(メインのPHP)********************************************** <?php require_once("../../modules/access_control/class/MySmarty.class.php"); require_once("../../modules/access_control/class/name_search.class.php"); $o_smarty=new MySmarty(); $db=$o_smarty->get_db(); ←ここで値が取得できない $o_smarty->assign("toolname",name_search::getToolName($db)); $o_smarty->display(basename($_SERVER['SCRIPT_NAME'],".php").".tpl"); ?> ***********************************************

    • ベストアンサー
    • PHP
  • Smartyでテンプレートが読み込めない

    下記のソースを実行すると、テンプレートが読み込めずエラーになってしまいます。 うーん、何故なのか分からず困っております。 phpのバージョンは4.4.6なのですが、Ver5以降でないとサポートされないのでしょうか。 [source] <?php require_once("Smarty.class.php"); class MySmarty extends Smarty { function _MySmarty() { $this->Smarty(); $this->template_dir="../templates"; $this->compile_dir="../templates_c"; } } $o_smarty=new MySmarty(); $o_smarty->display("hello.tpl"); ?> [error message] Warning: Smarty error: unable to read resource: "hello.tpl"

    • ベストアンサー
    • PHP
  • こんにちは、PHP初心者です。

    こんにちは、PHP初心者です。 Smartyを利用してWebサイトを構築中です。 Smarty.classの派生クラスを作成し、コンストラクタ内でDB接続オブジェクトをprivateプロパティ($_db)に設定しているのですが,query文のところでエラーSELECT * FROM books [nativecode=1046 ** No database selected]となります。ためしにコンストラクタ内でクエリを実行したところテーブルから値を取得できました。なぜコンストラクタ外に出るとエラーになるのかわかりません。どなたかご教示ください。どうぞよろしくお願いします。 下記ソースです。 =====派生クラス======================= <?php $user = "dbuser"; $pass = "dbpassword"; $name = "mobilephp"; $host = "localhost"; require_once("DB.php"); require_once("Smarty/libs/Smarty.class.php"); class MySmarty extends Smarty { private $_db; public function __construct() { $this->Smarty(); $this->template_dir="../templates"; $this->compile_dir="../templates_c"; $this->_db=DB::connect("mysql://$user:$pass@$host/$dbname"); } public function __destruct() { $this->_db->disconnect(); } public function get_db() {return $this->_db;} } ?> ======end===================================== ==========メインのPHP=================================== <?php require_once("../MySmarty.class.php"); $o_smarty=new MySmarty(); $db=$o_smarty->get_db(); $stt=$db->query("SELECT * FROM books");←ここでエラーになる $data=array(); while($row=$stt->fetchRow(DB_FETCHMODE_ASSOC)){ $data[]=array("isbn"=>$row['isbn'],"title"=>$row['title'], "publish"=>$row['publish'],"price"=>$row['price']); } $o_smarty->assign("data",$data); $o_smarty->display("structure.html"); ?>

    • ベストアンサー
    • PHP
  • Smarty コンバートエラー??

    はじめまして。 PHP初心者です。 テンプレートにオブジェクト配列を渡して、表示させたいのですが、 テンプレートをコンパイルする際にエラーがでてしまいます。 呼び出し元のtest.phpに”echo $objArray[0] -> get_id();”と書くと正常に表示されるのでテンプレートと問題と思うのですが、原因が解らずこまっております。。。 ご指導お願い致します。 ■環境 OS:Fedora 4 php-5.2.0 Smarty-2.6.13 ■ブラウザに表示されるエラー Catchable fatal error: Object of class User could not be converted to string in /home/dev/board/templates_c/%%6D^6D7^6D7C5625%%test.tpl.php on line 3 ■テンプレート(test.tpl) {$users[0] -> get_id()} ■テンプレートの呼び出し元(test.php) require_once("DB.php"); require_once("Smarty/libs/Smarty.class.php"); $o_smarty = new Smarty(); $o_smarty -> template_dir = "../templates"; $o_smarty -> compile_dir = "../templates_c"; $o_smarty -> debugging = TRUE; $db = DB::connect("mysql://*********************"); $objArray = User::getUserInfos($db); echo $objArray[0] -> get_id(); $o_smarty -> assign("users",$objArray); $o_smarty -> display("test.tpl"); class User { private $_id; public function get_id(){return $this -> _id;} public function set_id($_id){$this -> _id = $_id;} public static function getUserInfos($db){ ・・(中略)・・ } return $aryUsr; } }

    • ベストアンサー
    • PHP
  • smartyテンプレート読み込みについて

    Smarty動的Webサイト構築入門という書籍を見ながらSmartyを学習しているのですが、書籍のCD-ROMから落としたサンプルファイルを保存しているのですがどうしてもテンプレートを読み込むことができません。 03_01.php <?php require_once("../smarty/libs/Smarty.class.php"); $smarty = new Smarty(); $smarty->template_dir = "templates"; $smarty->compile_dir = "templates_c"; $smarty->assign("name", "Smartyさん"); $smarty->display("03_01.tpl"); ?> 03_01.tpl Hello, {$name}!! エラーメッセージ Warning: Smarty error: unable to read resource: "03_01.tpl" in C:\xampp\htdocs\smartybook\smarty\libs\Smarty.class.php on line 1092 03_01.phpは、C:\xampp/htdocs/smartybook/ren/に保存しています。 03_01.tplは、C:\xampp/htdocs/smartybook/templates/に保存しています。 どこが駄目なのか教えてください。 宜しくお願いします。

    • 締切済み
    • PHP
  • tplを読みに行けません。

    PHP+smartyを学んでいます。 Apache2-htdocs-taro-hanako.php とファイルを置き、一方、 Apache2-php_libs-smarty の下に -libs/ (中にSmarty.class.php あり) と -templates/ (中にtest.tpl あり)を並列的におきました。 hanako.php に、 require_once "../../php_libs/smarty/libs/Smarty.class.php"; $smarty = new Smarty; $smarty->templete_dir = "../../php_libs/smarty/templates";   $smarty->compile_dir = "../../php_libs/smarty/templates_c"; $smarty->cache_dir = "../../php_libs/smarty/cache"; 以下省略・・・ と書いてブラウザで開くと、次のエラーが出て、tpl を読み込むことが出来ません。 Warning: Smarty error: unable to read resource: "test.tpl" in C:\Program Files\Apache Group\Apache2\php_libs\smarty\libs\Smarty.class.php on line 1088 どこを間違えているのでしょうか。

    • ベストアンサー
    • PHP
  • さくらレンタルサーバでのsmartyの設定

    プログラム初心者なのですが、以下教えて頂けないでしょうか。 さくらレンタル(スタンダード)にsmartyを設置を試みているのですが、エラーメッセージが出てしまいます。 環境:PHP 5.2.6 / Smarty 2.6.19 /home/(アカウント)/www/libs/smartyの直下にSmarty.class.php以下、公式サイトからダウンロードしたものをそのままアップし、/home/アカウント/www/smartytest/helloの直下に以下のファイルをアップしました。 (index.php) <?php define('SMARTY_DIR','/home/アカウント/www/libs/smarty/'); define('SMARTY_TEST','/home/アカウント/www/smartysample/hello/'); require_once(SMARTY_DIR.'Smarty.class.php'); $smarty = new Smarty; $smarty->template_dir = SMARTY_TEST.'templates/'; $smarty->compile_dir = SMARTY_TEST.'templates_c/'; $smarty->config_dir = SMARTY_TEST.'configs/'; $smarty->cache_dir = SMARTY_TEST.'cache/'; $smarty->assign('msg','Hello World!'); $smarty->display('index.tpl'); ?> そして、このindex.phpにアクセスすると、下記のようなエラーメッセージが表示されます。 Warning: Smarty::require_once(/home/アカウント/www/libs/smarty/internals/core.write_compiled_resource.php) [smarty.require-once]: failed to open stream: No such file or directory in /home/アカウント/www/libs/smarty/Smarty.class.php on line 1429 Fatal error: Smarty::require_once() [function.require]: Failed opening required '/home/アカウント/www/libs/smarty/internals/core.write_compiled_resource.php' (include_path='.:/usr/local/php-5.2.6/lib/php') in /home/アカウント/www/libs/smarty/Smarty.class.php on line 1429 Smarty.class.phpの1429行目に問題があるのかとファイルを開いてみたものの、初心者のためよく理解できません。あと、Smarty.class.phpを呼び出すのに絶対パスを指定しているのは、さくらの管理画面からphp.iniでinclude_pathを指定しようとしても、うまくいかなかったためです(上記の場合、$smarty = new Smarty;を削除すれば、エラー画面は表示されません。変わりに何も表示されませんが・・)。 初心者のため、基本的なところで見落としがあるのかもしれませんが、教えて頂ければと思います。どうか、宜しくお願いします。

    • 締切済み
    • PHP
  • PHPとSmartyを使い、データベースからのデータを表示したい。

    PHP・Smartyを使い、DBからの表示を行おうとしています。 基本的な作業だとは思うのですが、なかなかできないので、教えてください。 ソースコードです。 //////PHPのコード <?php require_once('Connections/dbconn_mylocal.php'); ?>//DBへの接続を行っている。ここは正常。 <?php $sql = 'SELECT * FROM c_movie'; $result = mysql_query($sql, $conn); while($row = mysql_fetch_array ($result)){ $movie = $row["movie_title"]; } $smarty->assign('movie', $movie); $smarty->display("test1120.tpl"); ?> /////////////////tplファイル <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Smartyで表示する</title> </head> <body> ({foreach item='con' from=$movie}) ({$con}) ({/foreach}) </body> </html> よろしくお願いします!

    • ベストアンサー
    • PHP

専門家に質問してみよう