javascript - PHP MySQL Query returning data but unable to display

252

I'm trying to display data from my database based on a select form. I'm using js and php to achieve this.

In my header I have the js:

<script>
function showUser(str) {
  if (str=="") {
    document.getElementById("txtHint").innerHTML="";
    return;
  }
  if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  } else { // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
      document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
  xmlhttp.open("GET","getuser.php?q="+str,true);
  xmlhttp.send();
}
</script>

The code for the select page:

 <?php 
 include 'header.php'; 
 mysql_connect("#####", "#####", "########") or die(mysql_error());
 mysql_select_db("######") or die(mysql_error());
 $result = mysql_query("SELECT * FROM medlemmer");

 ?>

<div >
  <div >
    <form>
      <select name="users" onchange="showUser(this.value)">
      <?php
      $i = 0;
      while($row = mysql_fetch_array($result))
       { if (!$i++) echo "<option selected medlem</option>" ?>

        <option value="<?php echo $row[medlemmer_id] ?>"><?php echo $row[medlemmer_navn] ?></option>

        <?php } $i++; ?>
      </select>
    </form>
  </div>
  <div >
    <div id="txtHint">
      <b>Person info will be listed here.</b>
    </div>
  </div>
</div> 

And finally the php code which should return the query results from the selection:

<?php
$q = intval($_GET['q']);
mysql_connect("######", "#######", "######") or die(mysql_error());
mysql_select_db("######") or die(mysql_error());
$result4 = mysql_query("SELECT boder_boder_id, medlemmer_medlemmer_id, medlemmer_navn, medlemmer_id, boder_navn, boder_id, boder_pris FROM boder_has_medlemmer, medlemmer, boder WHERE boder_boder_id = boder_id AND medlemmer_medlemmer_id = medlemmer_id AND medlemmer_id ='$q' ");


if (mysql_num_rows($result4) == 0)
echo "Query returned 0 rows";

while($row = mysqli_fetch_array($result4)) {
    echo $row[medlemmer_navn].'<br>';
    echo $row[boder_pris].' kr.';
}
?> 

I run a check to make sure that the query is returning something. I get no errors.

I'm relatively new to this. Any suggestions to what is going wrong? I can see that something happens if i click on the select form. But nothing is displayed.

331

Answer

Solution:

You are combiningmysql_* withmysqli_*.`

So working solution will be

while($row = mysql_fetch_array($result4)) {
    echo $row[medlemmer_navn].'<br>';
    echo $row[boder_pris].' kr.';
}

Butmysql_* is deprecated and will be removed in the future, you should usemysqli_* or instead PDO with prepared statements.

700

Answer

Solution:

On this line

<option value="<?php echo $row[medlemmer_id] ?>"><?php echo $row[medlemmer_navn] ?></option>

you are missing colons it should be

<option value="<?php echo $row[medlemmer_id]; ?>"><?php echo $row[medlemmer_navn]; ?></option>

People are also looking for solutions to the problem: datetime - php check month exist between two dates

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.