-2

シンプルな CMS でページの追加を自動化しようとしていますが、PHP の大規模なユーザーではないため、MySQL データベースから列を取得し、そこから一連の文字列を作成する方法がわかりません。説明します。

$home=mysql_query("SELECT * FROM data WHERE callsign='home'"); //**

$num=mysql_num_rows($home);
$i=0;
while ($i < $num) {
 $f1=mysql_result($home,$i,"code"); //**
 $i++;
}
switch($_GET['page'])  {
case '#home' : $page = $f1; break; //**
}
echo $page;

MySQL データベースの列の各エントリのアスタリスクでマークされた行に文字列と変数を作成するにはどうすればよいですか?

4

1 に答える 1

1

データベースの「変数」と「文字列」が何を意味するのか完全にはわかりませんが、これを正しくする方法は次のとおりです。関数が非推奨になっているため、PDOを使用していることに注意してください。mysql_*

// this is how you connect with PDO
$dbh = new PDO('mysql:dbname=yourdbname;host=yourhost','youruser','yourpassword');

// you can name the column from the database itself. This is much faster
// also the ? is a placeholder. We'll pass the value on execute()
// this prevents SQL Injection attacks.
$sth = $dbh->prepare("SELECT code FROM data WHERE callsign=?");

// use try { } catch { } to detect errors
try {
    $sth->execute( array($_GET['page']) );
}
catch(PDOException $e) {
    die( $e->getMessage() );
}

// now you can fetch(). This returns the first row as an array. list() names the only variable in it
list($code) = $sth->fetch(PDO::FETCH_NUM); 

$sth->closeCursor(); // close the cursor or you'll have problems reusing the db handle

print $code; // output is a string
于 2012-07-04T05:24:50.740 に答える