2

私は2つの引数を持つ関数を持っています。ここにあります

function listBoats($con,$table){
    //get record set for all boats sort them by their "sort" number
    $queryBoat = "SELECT * FROM " .$table. " WHERE `id` <> 'mainPage' ORDER BY `sort` LIMIT 0, 1000";
    $result = mysqli_query($con,$queryBoat);
    return $result;
}

これが私がそれを呼んでいる方法です

$result = listBoats($con,"CSINSTOCK"); //run query to list all the boats in the CSINSTOCK table

私はそれを働かせることができません。しかし、関数内に変数を追加すると$table = "CSINSTOCK"、機能します。"CSINSTOCK"関数が変数を渡さないのはなぜですか?

4

1 に答える 1

-1

PDO を使用することをお勧めします。ここに例があります

例。 これはあなたの dbc クラスです (dbc.php)

<?php

class dbc {

    public $dbserver = 'server';
    public $dbusername = 'user';
    public $dbpassword = 'pass';
    public $dbname = 'db';

    function openDb() {    
        try {
            $db = new PDO('mysql:host=' . $this->dbserver . ';dbname=' . $this->dbname . ';charset=utf8', '' . $this->dbusername . '', '' . $this->dbpassword . '');
        } catch (PDOException $e) {
            die("error, please try again");
        }        
        return $db;
    }

    function getAllData($qty) {
        //prepared query to prevent SQL injections
        $query = "select * from TABLE where qty = ?";
        $stmt = $this->openDb()->prepare($query);
        $stmt->bindValue(1, $qty, PDO::PARAM_INT);
        $stmt->execute();
        $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
        return $rows;
    }    
?>

あなたのPHPページ:

<?php 
require "dbc.php";

$getList = $db->getAllData(25);

foreach ($getList as $key=> $row) {
         echo $row['columnName'] .' key: '. $key;
    }

データベースにアクセスできる場合は、必要な操作を実行できるはずです。

于 2013-06-10T17:45:39.563 に答える