• 締切済み

queryの使い方

お世話になります。 queryの使い方で質問です。 DB内に下記の様なデータが在ります。 -------------------------------- | id | header | footer | -------------------------------- | 1 | HTML文 | HTML文 | -------------------------------- テーブルとフィールドを指定してデータ(HTML文)を抜き出したいのですが、いまいち方法が理解できません。 queryの使い方を理解していないのだと思います。 以下、PHPに記述しているコードです。PHP自体は問題なく動いてます。 PHP---------------------------------------------------- $footer =& $mdb2 -> query('select footer from where id=1'); if (PEAR::isError($footer)) {die($footer->getMessage());} $smarty->assign("footer",mb_convert_encoding($footer,"SJIS","UTF-8")); 表示---------------------------------------------------- MDB2 Error: syntax error 宜しくお願いします。

noname#227352
noname#227352
  • MySQL
  • 回答数1
  • ありがとう数3

みんなの回答

  • g_liar
  • ベストアンサー率52% (382/728)
回答No.1

SELECT文にテーブルの指定がありません。 例えばテーブル名をxyzとするなら select footer from xyz where id=1 と記述します。

noname#227352
質問者

補足

早速の回答有り難うございます。 記述漏れでした・・・。(恥) で、表示されている内容が『Object』になりました。 宜しくお願いします

関連するQ&A

  • PHPでmySQLのデータを取り出す

    お世話になります。 php、smarty、mySQLを利用してサイトを構築しています。 下記のPHPプログラムを記述してデータベースからデータを取り出そうとしているのですが、queryの利用方法が良く判りません。 下記の場合、ブラウザ上には、『 Object 』と表示されています。 アドバイスをお願いします。 $sql = 'select footer from common where id=1'; $footer =& $mdb2 -> query($sql); if (PEAR::isError($footer)) {die($footer->getMessage());} $smarty->assign("footer",$footer);

  • mySQLからデータを取り出す

    お世話になります。 PHPてmySQLからデータを取り出したいです。 下記の様に記述をしたのですが、データベース上の[tanka]フィールドの合計[SUM(tanka)]が返ってきません。 宜しくお願いします。 //IDをキーにデータを検索 $id = $_GET['id']; $sql = 'SELECT SUM(tanka) FROM jisseki WHERE id2 = ' . $id; $res = $mdb2->query($sql); ※接続は出来ています。 if(PEAR::isError($res)){ exit('selectに失敗しました'); } //ココから先の処理がイマイチ理解できていないのかもしれません。 while ($row[] = $res->fetchRow() ){ $smarty->assign("sum",$row['SUM(tanka)']); }

    • ベストアンサー
    • PHP
  • smartyについて

    smartyについて 環境は、windows /php-5 / smarty-2.6.17です。 以下の2つエラーが出てどう対処すればいいか困っています。 【1つめ】 Fatal error: Smarty error: [in **.tpl line 10]: syntax error: unrecognized tag: 文--> 【2つめ】 {$errmsg (Smarty_Compiler.class.php, line 446) in C:\xampp\php\PEAR\Smarty\libs\Smarty.class.php on line 1095 【ソース】 呼び先PGは、 $this->smarty->assign("errmsg","ニックネーム"); $this->smarty->display("**.tpl"); テンプレートでは、 <tr id="" class=""> <td id="" class=""><font color="#ff0000">{$errmsg}を入力してください。</font></td> </tr> よろしくお願いいたします。

    • 締切済み
    • PHP
  • smartyでページングするには

    このようなsmartyのphpの場合はどのようにページングをしたらいいのかわかりません。どなたかお知恵をお貸し下さい。 $cache_id = 'rakuten:' . serialize($_GET); if (!$smarty->is_cached($tmpl_name, $cache_id)) { $query = "楽天の情報を取得"; if ($affiliate_id) { $query .= "&affiliateId=${affiliate_id}"; } $query .= "&keyword=" . urlencode($_GET['keyword']); $max = intval($_GET['max']); if ($max > 0 && $max <= 30) { $query .= "&hits=${max}"; } if (isset($_GET['sort'])) { $query .= "&sort=" . urlencode($_GET['sort']); } if (intval($_GET['genreId'])) { $query .= "&genreId=" . intval($_GET['genreId']); } if ($_GET['field'] == 'use') { $query .= "&field=0"; } if (intval($_GET['imageFlag']) == 1) { $query .= "&imageFlag=1"; } if (intval($_GET['minPrice']) > 0) { $query .= "&minPrice=" . intval($_GET['minPrice']); } if (intval($_GET['maxPrice']) > 0) { $query .= "&maxPrice=" . intval($_GET['maxPrice']); } if ($_GET['orFlag'] == 'use') { $query .= "&orFlag=1"; } $client =& new HTTP_Client(); $client->get($query); $resp =& $client->currentResponse(); if ($resp['code'] != 200) { $smarty->assign('tid', intval($_GET["tid"])); $smarty->assign('is_error', 1); $smarty->assign('is_connection_error', 1); $smarty->assign('errmsg', '接続に失敗しました。'); $smarty->caching = 0; $smarty->display('error.tpl'); exit(); } //print_r($resp['body']); //exit(); $xml = new XML_Unserializer(); $xml->setOption('complexType', 'array'); $xml->setOption('forceEnum', array('Item')); $result = $xml->unserialize($resp['body'], FALSE); $data = $xml->getUnserializedData(); $status = $data['header:Header']['Status']; if ($status == 'ClientError' || $status == 'ServerError' || $status == 'Maintenance') { $smarty->assign('tid', intval($_GET["tid"])); $smarty->assign('is_error', 1); $smarty->assign('is_api_error', 1); $smarty->assign('api_errmsg', $data['header:Header']['StatusMsg']); $smarty->caching = 0; $smarty->display('error.tpl'); exit(); } // print("query = $query<br />\n"); // print_r($data); // exit(); $smarty->assign('total_count', $data['Body']['itemSearch:ItemSearch']['count']); $smarty->assign('items', $data['Body']['itemSearch:ItemSearch']['Items']['Item']); } // テンプレートの表示 $smarty->display($tmpl_name, $cache_id);

    • ベストアンサー
    • PHP
  • MDB2 [Call to undefined method MDB2

    MDB2 [Call to undefined method MDB2_Error::execute()] 以下のようなMDB2を利用したSELECT文を書きましたが、 最終行で、 Call to undefined method MDB2_Error::execute() が出ます。 どのように修正すれば良いでしょうか? ===================================================================================== require_once('/usr/share/pear/PEAR/MDB2.php'); $db=MDB2::connect('mysqli://【user】:【password】@localhost/【dbname】?charset=utf8'); if(PEAR::isError($db)){ die($db->getMessage()); } $sql = 'select * from user where name=? and password=?'; $types = array('text','text'); $stmt=$db->prepare($sql, $type, TRUE); $data = array('guest' , 'guest'); $res = $stmt->execute($data); ===================================================================================== 下から3行目の$stmtがMDB2_Errorオブジェクトなのまでは分かるのですが、 どう対処すればよいかが不明です。 宜しくお願いします。

    • ベストアンサー
    • PHP
  • PEAR::MB2によるPHP+MySQLについて

    PEAR::MB2でPHP+MySQLを勉強中なのですが、特定カラムの値を取得することができません。 下記のソースコードで、変数$aaaをechoで参照すると空白となってしまいます。 大変恐縮ですがご教授のほどよろしくお願いいたします。 [環境]----------------------------------------------------------- OS: windows7(64bit) PHP: ver5.3.8 MySQL: ver5.5 Pear::MDB2  pear/MDB2 2.5.0b3 2.4.1 database abstraction layer  pear/MDB2_Driver_fbsql 0.2.0 fbsql MDB2 driver  pear/MDB2_Driver_ibase 1.5.0b3 ibase MDB2 driver  pear/MDB2_Driver_mssql 1.5.0b3 mssql MDB2 driver  pear/MDB2_Driver_mysql 1.5.0b3 1.4.1 mysql MDB2 driver  pear/MDB2_Driver_mysqli 1.5.0b3 mysqli MDB2 driver  pear/MDB2_Driver_oci8 1.5.0b3 oci8 MDB2 driver  pear/MDB2_Driver_odbc 0.1.0 ODBC Driver for MDB2  pear/MDB2_Driver_pgsql 1.5.0b3 pgsql MDB2 driver  pear/MDB2_Driver_querysim 0.6.0 querysim MDB2 driver  pear/MDB2_Driver_sqlite 1.5.0b3 sqlite MDB2 driver  pear/MDB2_Driver_sqlsrv 1.5.0b3 sqlsrv MDB2 driver  pear/MDB2_Schema 0.8.5 XML based database schema manager  pear/MDB2_TableBrowser 0.1.2 Database table abstraction library (pear list -allから一部抜粋) [ソースコード]----------------------------------------------------------- require_once("MDB2.php"); $dsn = "mysql://user:pass@localhost/db_name"; $conn = MDB2::connect($dsn); // 接続に失敗したらメッセージを表示して終了 if (MDB2::isError($conn)){ die($conn->getMessage()); } //フェッチモード設定 $conn->setFetchMode(MDB2_FETCHMODE_ASSOC); //SELECT文 $sql = <<<EOS SELECT * FROM db_table WHERE name = 'taro' EOS; //実行 $res = $conn->query($sql); //エラーチェック if (PEAR::isError($res)) { die($res->getMessage()); } //値の取得 $row = $res->fetchRow(); //変数$aaaに、別カラム値(例:miyoji)を格納 $aaa = $row['miyoji']; echo "$aaa"; //←(例)mysqlから田中といった苗字を出力したい。 //クローズ $conn->disconnect(); ----------------------------------------------------------------------- エラー出力はありません。

    • ベストアンサー
    • PHP
  • MDB2によるデータベースアクセス

    ができません。 次のものは動きました。 <?php require_once("DB.php"); $db=DB::connect("pgsql://gomiuser@localhost/gomidatabase"); $db->disconnect(); ?> が以下が動きません。 <?php require_once 'MDB2.php'; $mdb2=&MDB2::connect(pgsql://gomiuser@localhost/gomidatabase); if(PEAR::isError($mdb2)) { die($mdb2->getMessage()); } $mdb2->disconnect(); ?> Don't Find(出力) 何が悪いのでしょうか?

    • ベストアンサー
    • PHP
  • いつもお世話になっております。

    いつもお世話になっております。 小生、只今WindowsXPSP3上でXAMPP1.7.1を使用し、PHPを勉強しているPHP初心者です。 今回皆様にご質問させて頂きたい内容はXAMPPにSmatyをインストールするべきかしないべきかです。 私の環境でXAMPPのフォルダ内で「smarty」というキーワードを入れ、検索してみたところ、以下のファイルが検出されました。 smarty ディレクトリ     C:\xampp\htdocs\xoops\class Smarty.class.php       C:\xampp\htdocs\xoops\class\smarty Smarty_Compiler.class.php   C:\xampp\htdocs\xoops\class\smarty Smarty-2.6.0 ディレクトリ  C:\xampp\php\PEAR\PhpDocumentor\phpDocumentor core.assign_smarty_interface.php C:\xampp\htdocs\xoops\class\smarty\core core.smarty_include_php.php  C:\xampp\htdocs\xoops\class\smarty\core SmartyTask.php C:\xampp\php\PEAR\phing\tasks\ext smarty ディレクトリ C:\xampp\htdocs\extras\extra_languages\pt_utf8\html\class Smarty ディレクトリ C:\xampp\php\PEAR\PhpDocumentor\phpDocumentor\Converters\HTML Smarty.class.php C:\xampp\php\PEAR\PhpDocumentor\phpDocumentor\Smarty-2.6.0\libs Smarty_Compiler.class.php C:\xampp\php\PEAR\PhpDocumentor\phpDocumentor\Smarty-2.6.0\libs HTMLSmartyConverter.inc C:\xampp\php\PEAR\PhpDocumentor\phpDocumentor\Converters\HTML\Smarty core.assign_smarty_interface.php C:\xampp\php\PEAR\PhpDocumentor\phpDocumentor\Smarty-2.6.0\libs\core core.smarty_include_php.php C:\xampp\php\PEAR\PhpDocumentor\phpDocumentor\Smarty-2.6.0\libs\core 以上のファイルがあっても、XAMPPにSmartyをインストールしなければ、Smartyの機能は使えないのでしょうか?? また、上記の設定(Smartyをインストールしていない状態)でC:\xampp\sample\templatesフォルダを作成し、以下の.tplファイルを作成しました。 -----.tplファイル------ <html> <head> <title>smatyのテスト</title> </head> <body> ようこそ、($name)さん<br/> Hello World! </body> </html> そしてその上位フォルダにindex.phpを作成しました。 -----index.php----- <?php require_once "Smarty.class.php"; $smarty = new Smarty; $smarty->assign('name', '桃太郎'); $smarty->;display('index.tpl'); ?> 上記の2ファイルを作成し、http://localhost/sampleにアクセスしても、 「Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `'{'' or `'$'' in C:\xampp\htdocs\sample\index.php on line 6」のエラーが出現し、正確に表示されません。 お忙しい中、大変申し訳ございませんが、先輩方アドバイス宜しくお願い致します。

    • ベストアンサー
    • PHP
  • PHPのPEAR:DBでPostgreSQLを使用時、カラム名でデータが取得できない

    PHPのPEAR::DBのサンプルを公開しているサイトから以下のソースを引っ張ってきて、試してみました。すると、少しおかしな現象が発生したので、ご質問させていただきます。 ------------------------------ $dsn = "pgsql://postgres:pass@localhost/postgres"; $db = DB::connect($dsn); if (PEAR::isError($db)) { die($db->getMessage()); } print('接続に成功しました<br>'); $sql = 'select * from "USER_TBL"'; $res =& $db->query($sql); if (PEAR::isError($res)) { die($res->getMessage()); } while ($row =& $res->fetchRow()) { print($row['ID']); print($row['NAME'].'<br>'); } ------------------------------ 接続は問題なかったのですが、データベースからデータを取得する「print($row['ID']);」の部分が動かず、<br>だけ出力されてしまいます。IDのカラム名をダブルクォーテーションで囲っても、値が出てきません。ところが、print($row[0]);などのように、数値を指定してやると、その部分が出力されます。 また、カラム名を小文字に変えてみたりもしましたが、やはりそれでもデータは取得できませんでした。 理由がよく判らないのですが、カラム名でデータを引っ張ってきたいと思います。解決方法はありませんでしょうか?

    • ベストアンサー
    • PHP
  • PEARのライブラリはどこにインストールされるのでしょう

    お疲れ様です。 お世話になっております。 HTML_QuickFormに関して質問です。 CentOSでPEARをインストールし、 pear install HTML_QuickFormでインストールしました。 pear list すると HTML_Common 1.2.4 stable HTML_QuickForm 3.2.9 stable PEAR 1.4.9 stable と、インストールは確認できます。 php.iniのinclude_pathは include_path =".:/usr/lib/php/smarty:/usr/lib/php/pear" となっています。 smartyはrequire_once"Smarty.class.php";で呼び出しできるのですが、 HTML_QuickFormが"HTML/quick_form.php"でfile not foundになってしまいます。 pearのディレクトリをのぞいてみたら空の状態なんですが、いったいどこにHTML_QuickFormはインストールされているのでしょうか。 pearのライブラリはpearの配下にくると思っていたのですが。 呼び出すにはどこのパスを指定すればよいのでしょう・・・。

    • 締切済み
    • PHP