Dan
Dan

Reputation: 883

PHP Foreach statement issue. Multiple rows are returned

I'm a PHP beginner and lately I've been having a problem with my source code.

Here it is:

   <html>
    <head>
        <title>
            Bot
        </title>
        <link type="text/css" rel="stylesheet" href="main.css" />
    </head>
    <body>
        <form action="bot.php "method="post">
            <lable>You:<input type="text" name="intrebare"></lable>
            <input type="submit" name="introdu" value="Send">
        </form>
    </body>
</html>
<?php
//error_reporting(E_ALL & ~E_NOTICE);
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("robo") or die(mysql_error());

$intrebare=$_POST['intrebare'];
$query = "SELECT * FROM dialog where intrebare like '%$intrebare%'"; 
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
?>
<div id="history">
<?php       
        foreach($row as $rows){ 
            echo "<b>The robot says: </b><br />";
            echo $row['raspuns'];
            }
?>
</div>

It returns the result 6x times.

This problem appeared when I've made that foreach because I wanted the results to stuck on the page one by one after every SQL query.

Can you please tell me what seems to be the problem? Thanks!

Upvotes: 3

Views: 1333

Answers (5)

Lenin
Lenin

Reputation: 610

You are trying it reversed way:

    <?php     

    while($row = mysql_fetch_array($result,MYSQL_ASSOC)){ 
        echo '<strong>The robot says: </strong><br />', $row['raspuns'];
        }
    ?>

Try now :)

Upvotes: 1

cHao
cHao

Reputation: 86506

mysql_fetch_array fetches one row per call. You'll want to do like this:

while ($row = mysql_fetch_array($result)) {
    echo "<b>The robot says:</b><br>";
    echo htmlentities($row['raspuns']);
}

and get rid of that first mysql_fetch_array.

(Notice that i am HTML-escaping the variable output. Unless you know what you're doing, you should not output raw data into a page.)

By the way, mysql_query is effectively deprecated. It is not at all recommended for new code. Take a look at mysqli (the replacement) or PDO (the new hotness). With the new mysqli (objecty) interface, the PHP part would look a bit like this:

<?php

//error_reporting(E_ALL & ~E_NOTICE);

$db = new mysqli('localhost', 'root', '', 'robo');

# turn into a wildcard
$intrebare='%' . $_POST['intrebare'] . '%';

$stmt = $db->prepare('SELECT * FROM dialog WHERE intrebare LIKE ?');
$stmt->bind_param('s', $intrebare);
$result = $stmt->execute();

echo '<div id="history">';
# 5.4 lets you do it like this; 
# older versions, try `while ($row = $result->fetch_assoc())`
foreach ($result as $row) {
    echo "<b>The robot says: </b><br />";
    echo htmlentities($row['raspuns']);
}

?>

Upvotes: 2

MTahir
MTahir

Reputation: 1171

Use while to fetch all the data and check variable names

        while($row = mysql_fetch_array($result)){ 
        echo "<b>The robot says: </b><br />";
        echo $row['raspuns']; // Here
        }

Upvotes: 1

Chris Forrence
Chris Forrence

Reputation: 10084

You're only getting one result (only one call to mysql_fetch_array()). There are six columns, I bet, in dialog.

...
$result = mysql_query($query) or die(mysql_error());
?>
<div id="history">
<?php
while($row = mysql_fetch_array($result))
{
    echo "<b>The robot says: </b><br />";
    echo htmlentities($row['raspuns']);
}
?>
</div>

Also, mysql_* functions are being deprecated. Switch to mysqli_* or PDO.

Upvotes: 1

Benjamin Paap
Benjamin Paap

Reputation: 2759

You are doing it wrong. ;-)

First of all you have to fetch your result with mysql_fetch_array in a loop like this:

while (true == ($row = mysql_fetch_array($result))) {
    echo "<b>The robot says: </b><br />";
    echo $row['raspuns'];
}

Second I want to tell you that all mysql_* functions are marked as deprecated. If you want to learn PHP you should try to learn how to connect to mysql using PDO.

Upvotes: 2

Related Questions