1. 程式人生 > >PHP傳給前端的值有大量html程式碼

PHP傳給前端的值有大量html程式碼

話不多說, 直接上程式碼

<br />
<font size='1'><table class='xdebug-error xe-deprecated' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span
>
Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:\wamp\www\unit1\json1.php on line <i>5</i></th></tr> <tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr
>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0000</td><td bgcolor='#eeeeec' align='right'>242216</td><td bgcolor='#eeeeec'>{main}( )</td><td title='C:\wamp\www\unit1\json1.php' bgcolor='#eeeeec'>..\json1.php<b>:</b>0</td></tr> <tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0000</td><td bgcolor='#eeeeec' align='right'>242688</td><td bgcolor='#eeeeec'><a href='http://www.php.net/function.mysql-connect' target='_new'>mysql_connect</a> ( )</td><td title='C:\wamp\www\unit1\json1.php' bgcolor='#eeeeec'>..\json1.php<b>:</b>5</td></tr> </table></font> [{"0":"Test1","product":"Test1","1":"Hello","questions":"Hello"},{"0":"","product":"","1":"","questions":""},{"0":"Test1","product":"Test1","1":"Venky","questions":"Venky"},{"0":"","product":"","1":"","questions":""}]

其實我們本意只想傳下面的json資料

[{"0":"Test1","product":"Test1","1":"Hello","questions":"Hello"},{"0":"","product":"","1":"","questions":""},{"0":"Test1","product":"Test1","1":"Venky","questions":"Venky"},{"0":"","product":"","1":"","questions":""}]

那上面多出來的html程式碼都是什麼鬼?
那就再來看下PHP程式碼

<?php

header('Content-type:application/json');

mysql_connect('localhost','root','') or die(mysql_error()); 

mysql_select_db('testdb');

$select = mysql_query('select * from questions');

$rows = array();

while($row=mysql_fetch_array($select))
{

    $rows[] = $row;
}

echo json_encode($rows);

?>

返回的json字串中, 有個關鍵詞, “Deprecated”, 代表 “不建議使用”.PHP給出的解釋是: mysql_connect() 這個方法在將來的某個時間會被徹底移除掉.
而且, 在宣告一點, 返回的json資料走的是error方法,至於為什麼會返回正確的資料,這就得問大神了.

解決方案如下
使用PDO或者mysqli_connect();
PDO程式碼:

<?php
//  header('Content-type:application/json');
    // 連線資料庫
    $dsn = 'mysql:dbname=dbtest1;host=127.0.0.1';
    $user = 'root';
    $password = '';

    try {
$dbh = new PDO('mysql:dbname=dbtest1;host=127.0.0.1', $user, $pass);
$dbh = new PDO($dsn, $user, $password);
        foreach($dbh->query('SELECT * from tb_test1') as $row) {
            print_r($row[0]);
            print_r("\n");
        }
        $dbh = null;
    } catch (PDOException $e) {
        print "Error!: " . $e->getMessage() . "<br/>";
        die();
    }
?>

我自己用的是PDO,本以為能好了, 結果還是報類似的錯: 一大堆html程式碼+返回資料
後來把PDO物件的第一個引數由常量改為變數後, 就好了, 如下:

$dbh = new PDO($dsn, $user, $pass);

公司同事給出了一個猜測性解釋——PDO例項在後續操作中可能會影響到引數改變, 常量不可變, 變數可變, 所以常量填進去後會報錯.
謹以此文,拋磚引玉,希望大神來探討.