-1

Please see my php-codes below,first one works as it should be. It will echo all data (based on values from text fields) from database. If there is multiple values it will print all data separated at rows. This will be printed straightly to web-page without table.

Another code should print all data at multiple cases (based on values from text fields) at the table, but in some reason it will only print one result.Even that there´s more data at the database. Any suggestions to fix this?

Codes below, first one(which works):

<?php

mysql_connect("","","");
mysql_select_db("");


$search_value = $_GET['value'];
$search_value2 = $_GET['value2'];
$query = "select * from data where data1 like '$search_value' and data2 like '$search_value2'";

$run = mysql_query($query);

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

$data1 = $row['data1'];
$data2 = $row['data2'];

echo "<h1>$data1</h1>";
echo "<h1>$data2</h1>";

}

?>

Another one(which print only one row to the table)

<?php

mysql_connect("","","");
mysql_select_db("");

$search_value = $_GET['value'];
$search_value2 = $_GET['value2'];
$query = "select * from data where data1 like '$search_value' and data2 like '$search_value2'";

$run = mysql_query($query);


while($row=mysql_fetch_array($run)){
{
  print('<tr>');
  print('  <td>');
  print $row["data1"];
  print('</td>');
  print('  <td>');
  print $row["data2"];
  print('</td>');
  print('</tr>');
  print ("\n");            
}
mysql_free_result($run);
}

?>

Any help is appreciated.

mkl13
  • 23
  • 4
  • 2
    Your code is vulnerable to SQL injections. You should read on [how to prevent them in PHP](http://stackoverflow.com/q/60174/53114). – Gumbo Mar 23 '14 at 13:51
  • That depends on what your search terms are from value and value 2 as well as your database data, looking at your query it will only give you that results that have data1 like this and data 2 like that if they both don't match it the result will not be included. You also have 1 loose `{` and 1 loose `}` on your second code which would make it not work and produce an error. Besides that both codes look the same except for how you're printing the result and the HTML. – Prix Mar 23 '14 at 14:02
  • You're assigning `$tyonumero = $row['data1'];` yet you're echoing `echo "

    $data1

    ";` Replace that with `echo "

    $tyonumero

    ";` etc.
    – Funk Forty Niner Mar 23 '14 at 14:05
  • 1
    @Fred-ii- he said the first code works, even though it's wrong. – Prix Mar 23 '14 at 14:09
  • @Prix Theoretically, OP's first code should work with my recommendation. – Funk Forty Niner Mar 23 '14 at 14:13
  • @Fred-ii- `Codes below, first one(which works)` so this assume his problem is with the second code despite the wrong variables on his first code which he perhaps typo when copying it here and replacing whatever he didn't want to show. Regardless the question is very unclear. – Prix Mar 23 '14 at 14:15
  • True, which is why I voted to close as unclear. @Prix – Funk Forty Niner Mar 23 '14 at 14:16
  • Hi, lot´s of comments so far, thanks. @Fred-ii-, you are absolutely right with the typo because i changed few things before i copied code at here. So , first one is correct (concerning printing) – mkl13 Mar 23 '14 at 14:21
  • If the second one only prints one row, then try changing `mysql_fetch_array` to `mysql_fetch_assoc` @user3451834 – Funk Forty Niner Mar 23 '14 at 14:22
  • Too bad that I cannot post pictures to show how the printing will go in these cases.. – mkl13 Mar 23 '14 at 14:23
  • Changing the mysql_fetch_array to mysql_fetch_assoc wont make any change to the printing. – mkl13 Mar 23 '14 at 14:31
  • What do you mean by "printing"? You're looking to print in a certain fashion? – Funk Forty Niner Mar 23 '14 at 14:43
  • My question seems to be unclear:( I try to print data to the website from mysql-database. In first code printing will proceed as it should be, example: I add value 12345 and value 1 to the text fields at the html-page, happends that first code above search these values from the database and print these 5 results to the separated rows at web-page. In second code I would like to print these results to the table but only one table row will be printed (even there´s actually 5 results at "12345" and "1". – mkl13 Mar 23 '14 at 14:45

1 Answers1

0

You are using mysql_free_result() which purges whole resultset right after first cycle of your while.
Just remove this line: mysql_free_result($run);

Also, I suggest you use 1 line to print, instead of 9.

while($row=mysql_fetch_array($run)){
      echo '<tr><td>'.($row['data1']).'</td><td>'.($row['data2']).'</tr>';            
    }
Alexander
  • 3,129
  • 2
  • 19
  • 33
  • 1
    Thank you. This helped me. Printed table shows all results after I removed mysql_free_result- line. Now I will check this another suggestion and also comment concerning SQL injection. – mkl13 Mar 23 '14 at 17:24