0

Why mysql display incorrect data in database

-------------------
|     Product     |
-------------------
| id              |
| name            |
| options_id      |
-------------------

Database row

id = 1,
name = Ring,
options_id = 2,5,6,1,

SQL Command

SELECT options_id FROM product WHERE name='Ring'

When am i test on phpMyAdmin => 2,5,6,1, Correct But PHP

$query = mysql_query("SELECT options_id FROM product WHERE name='Ring'");
$arrProduct = mysql_fetch_array($query);
echo $arrProduct['options_id'];

Result = 1,

  • That's because you're just fetching the first row. Loop the fetch, and you'll get them all. See the manual: http://php.net/manual/en/function.mysql-fetch-array.php – Qirel May 21 '17 at 17:43
  • 2
    [**Please, don't use `mysql_*` functions in new code**](http://stackoverflow.com/q/12859942). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://php.net/mysql-connect)? Learn about [*prepared statements*](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://php.net/manual/en/mysqlinfo.api.choosing.php) can help you decide which. – Qirel May 21 '17 at 17:44
  • my database have one row – Pirune Tirapibal May 21 '17 at 17:45
  • try `var_dump($arrProduct['options_id']);` – Agam Banga May 21 '17 at 17:47
  • try id "var_dump" result = string(2) – Pirune Tirapibal May 21 '17 at 17:49
  • Your php code and your phpMyAdmin are using different servers or a different database. – Paul Spiegel May 21 '17 at 17:51

0 Answers0