2

i have here a code for the selection of all the data from the database, here's my code:

<?php

$name = $_POST['cname'];

if ($_POST["Search"] == "Search") {
    $query = mysql_query("SELECT * FROM parts WHERE cname LIKE '%$name%'");
    if (!empty($query)) {
        $vsi = 'No Data';
        $date = 'No Data';
        $cname = 'No Data';
    }

    while ($row = @mysql_fetch_array($query)) {
        $vsi = $row["vsi"];
        $date = $row["date"];
        $cname = $row["cname"];
    }

and here is how i echo all the variables,

<tr class="gradeC">
<?php echo "<td width='10%'><font size='-2'> $vsi </font></td>" ?>
<?php echo "<td width='20%'><font size='-2'>$date</font></td>" ?>
<?php echo "<td width='20%'><font size='-2'> $cname</font></td>" ?>
</tr>

My problem is that,

i only get one result from each row but my data in the database are 5.

here is my result

vsi      date             name
123      12/12/2012       test1

but the correct result should be:

vsi      date             name
123      12/12/2012       test1
123      12/12/2012       test1
123      12/12/2012       test1
123      12/12/2012       test1
123      12/12/2012       test1

note,

i make all the data in the database all the same

Prahalad Gaggar
  • 11,389
  • 16
  • 53
  • 71
Jerome
  • 139
  • 10
  • 5
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – Joseph Silber Jan 18 '13 at 05:32
  • 1
    There's only one `$vsi` (and `$date` and `$cname`), so there's only one line. – Passerby Jan 18 '13 at 05:35
  • Are you echoing the results *inside* or *after* the loop? – lc. Jan 18 '13 at 05:35
  • but their id is not the same, its auto increment. – Jerome Jan 18 '13 at 05:36
  • once you fetch result form fetch array the pointer set to the end – NullPoiиteя Jan 18 '13 at 05:37
  • even if i change the data in the database the result is the same, one data only. – Jerome Jan 18 '13 at 05:47
  • @Jerome : Did you tried the answers given below ? – Dhruv Patel Jan 18 '13 at 06:26
  • not yet, im sorry, my internet was down last day. – Jerome Jan 19 '13 at 03:04

2 Answers2

8

You have to use TR in while loop.

<?php

$name = $_POST['cname'];

if ($_POST["Search"] == "Search") {
    $query = mysql_query("SELECT * FROM parts WHERE cname LIKE '%$name%'");
    if (!empty($query)) {
        $vsi = 'No Data';
        $date = 'No Data';
        $cname = 'No Data';
    }

    while ($row = @mysql_fetch_array($query)) {
        $vsi = $row["vsi"];
        $date = $row["date"];
        $cname = $row["cname"];

?>
<tr class="gradeC">
<?php echo "<td width='10%'><font size='-2'> $vsi </font></td>" ?>
<?php echo "<td width='20%'><font size='-2'>$date</font></td>" ?>
<?php echo "<td width='20%'><font size='-2'> $cname</font></td>" ?>
</tr>
<?
    }

?>

Warning

your code is vulnerable to sql injection you need to escape all get and post and the better approach will be using Prepared statement

Good Read

  1. How to prevent SQL injection in PHP?
  2. Are PDO prepared statements sufficient to prevent SQL injection?

Note

  1. The entire ext/mysql PHP extension, which provides all functions named with the prefix mysql_, is officially deprecated as of PHP v5.5.0 and will be removed in the future. So use either PDO or MySQLi

Good read

  1. The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead
  2. PDO Tutorial for MySQL Developers
  3. Pdo Tutorial For Beginners

I Hope this helps.

Community
  • 1
  • 1
Dhruv Patel
  • 404
  • 1
  • 5
  • 16
0

The result of fetching will be an array. It will contain all of your matching database entries. But your echo is just outside of the while loop.Thats why it return only one result.Close the 2nd while loop after your echo.

while ($row = @mysql_fetch_array($query)) {
$vsi = $row["vsi"];
$date = $row["date"];
$cname = $row["cname"];

?>
<tr class="gradeC">
<?php echo "<td width='10%'><font size='-2'> $vsi </font></td>" ?>
<?php echo "<td width='20%'><font size='-2'>$date</font></td>" ?>
<?php echo "<td width='20%'><font size='-2'> $cname</font></td>" ?>
</tr>
<?
}
DjangoDev
  • 889
  • 6
  • 16
  • 25