简体   繁体   中英

PHP MySQL Run Two Queries in one Connection?

Is it possible to run two queries inside of one connection. What I am doing is I am populating a form with profile data. But then I need to populate two drop downs from a database that contains the values. I have included how I have it setup but my first drop down never is populated what am I doing wrong?

<?php
session_start(); 
include("includes.php");
$uid = $_SESSION[username];

    try
        {
            $con = mysql_connect("XXX.XXX.XXX.XX","ita","iiiii");
            if (!$con)
                {
                    die('Could not connect: ' . mysql_error());
                }

            mysql_select_db("bia", $con);

            $options = mysql_query("SELECT * FROM `Schools`");

            $options = array();

            while($row = mysql_fetch_assoc($options))
                {
                    $options[] = $row;
                }

            $result = mysql_query("SELECT * FROM `users` WHERE uid = '$uid'");
            while($row = mysql_fetch_assoc($result)){


?>
<form id="myform" name="myform" action="profiledo.php" method="post">
<p>First Name
  <input type="text" name="firstname" id="textfield" value="<?php echo( htmlspecialchars( $row['FirstName'] ) ); ?>" />
  <br />
<label for="collegedropdown"></label>
<select name="collegedropdown" id="collegedropdown">
<?php
  foreach($options as $option) {
      print '<option value='.$option.'>'.$option.'</option>'."\n";
    }
  }
?>
</select>

You can have any number of queries in a single connection.

Here are a couple of things I can see right away:

  • mysql_fetch_assoc returns an array, but I'm tempted to say you're treating it like a string?
  • There are no quotes around your value ?
  • You're overwriting $options (it's your MySQL result and then turns into your results array)?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM