returning multiple rows from mysql in php -
I am trying to write a PHP-script that will obtain multiple rows from MySQL and give them JSONObject Will return in the form if I try to get only 1 row, but if I try to get more than one at a time, the return string is empty.
$ i = mysql_query ("Select * from database where id = '$ V1'", $ Conn.); $ Temp = 2; While ($ line = mysql_fetch_assoc ($ i)) {$ r [$ temp] = $ line; // $ temp = $ temp +1; } If I write code this way so he'd know what I hope makes it, but if I remove the loop // second line would then no Will someone return to explain how to solve it and what should I do?
-
You are using an obsolete mysql_ * library.
-
You are prone to SQL injection
-
The fool your code and no if you really want to stick it in, please do not:
while ($ Row = mysql_fetch_assoc ($ i) )) {$ R [] = $ line; Echo json_encode ($ r); And finally, using an example PDO:
$ database = 'your_database'; $ User = 'your_db_user'; $ Pass = 'your_db_pass'; $ Pdo = new \ PDO ('mysql: host = localhost; dbname ='. $ Database, $ user, $ pass); $ Pdo- & gt; Set attribute (\ PDO :: ATTR_ERRMODE, \ PDO :: ERRMODE_EXCEPTION); {$ Stmt = $ pdo- & gt; Try ("Select * FROM your_table WHERE id =: id"); $ Stmt- & gt; Bind value (': id', $ id); $ Stmt- & gt; Executed (); $ Result = $ stmt-> Fetch ALL (\ PDO :: FETCH_ASSOC); } Hold (\ PDOException $ e) {$ result = ['error' = & gt; $ E- & gt; GetMessage (), 'file' = & gt; $ E- & gt; GetFile (), 'line' = & gt; $ E & gt; GetLine ()); } Echo json_encode ($ result);
Comments
Post a Comment