简体   繁体   中英

Storing values of a mySQL query into vars with PHP

I am trying to store variables from mySQL prepared queries into PHP vars for insertion into text boxes..

function printform2($db, $ssn) {

$query="SELECT e.fname, e.minit, e.lname, e.ssn, e.bdate, 
      e.address, e.sex, e.super_ssn, e.salary,
      CONCAT(d.dnumber, ' (', d.dname, ')' ) as dept
FROM (employee e LEFT JOIN employee s ON e.super_ssn = s.ssn) 
LEFT OUTER JOIN department d ON e.dno = d.dnumber
WHERE e.ssn=$ssn";


$qstmt = $db->prepare($query, array(), MDB2_PREPARE_RESULT);
$qres = $qstmt->execute(NULL);

if (MDB2::isError($qres)) {
    die("query not successful: " . $qres->getMessage());
}

$myrow=mysql_fetch_array($qres, MYSQL_ASSOC);


print "<h3>Table of Employees</h3>";
table_format($qres);
print "<p>";



print <<<END
<b>This form is for updating the employee with ssn $ssn</b><p>
<form method="post" action="">
<input type="text" name="ssn" value="$ssn" readonly> SSN of employee<p>
<br><input type="text" name="fname" value="$myrow[fname]"> First Name
<br><input type="text" name="minit" value="$myrow[minit]"> Middile Initial
<br><input type="text" name="lname" value="$myrow[lname]"> Last Name
<br><input type="text" name="bdate" value="$bdate"> Birth Date
<br><input type="text" name="address" value="$address"> Address
<br><input type="text" name="sex" value="$sex"> Sex
<br><input type="text" name="salary" value="$salary"> Salary
<br><input type="text" name="dno" value="$dno"> Department Number
<br><input type="text" name="super_ssn" value="$super_ssn"> Supervisor SSN
<p><input type="submit" name="update" value="Update">
</form>
END;
    makePageButtons();
}

What I get are empty text boxes for those which I am trying to echo the myrow var into.. Is there something special I need to do when dealing with prepared statements and how they output? I'm confused because it works with a "vanilla" mysql_query(..) statement..

You are doing it wrong. Stop mixing mysql_* and PDO stuff;

It should be something like this

<?php

function printform2($db, $ssn) {

$query="
    SELECT 
        e.fname AS firstname, 
        e.minit AS minit, 
        e.lname AS lastname,
        e.ssn AS ssn, 
        e.bdate AS birthdate, 
        e.address AS agress, 
        e.sex AS gender, 
        e.super_ssn AS super_ssn, 
        e.salary AS salary,
        concat(d.dnumber, ' (', d.dname, ')' ) as dept
    FROM 
        employee AS e 
    LEFT JOIN 
        employee AS s ON  e.super_ssn = s.ssn 
    LEFT OUTTER JOIN 
        department AS d ON e.dno = d.dnumber
    WHERE
        e.ssn=:ssn";

$statement = $db->prepare( $query );
$statement->bindValue(':ssn', $ssn, PDO::PARAM_INT );

if ( !$statement->execute() ){
    die("query not successful: " . $statement->errorInfo());
}

$data = $statement->fetch( PDO::FETCH_ASSOC );
?>

<h3>Table of Employees</h3>
<p><strong>This form is for updating the employee with ssn <?php echo $ssn; ?></strong><p>
<form method="post" action="">
    <ul>
        <li>
            <input type="text" name="ssn" id="ssn" value="<?php echo $ssn; ?>" readonly>
            <label for="ssn">SSN of employee</label>
        </li>
        <li>
            <input type="text" name="fname" id="fname" value="<?php echo $data['firstname']; ?>">
            <label for="fname"> First Name</label>
        </li>
        <li>
            <input type="text" name="minit" id="minit" value="<?php echo $data['minit']; ?>">
            <label for="minit">Middile Initial</label>
        </li>
        <li>
            <input type="text" name="lname" value="<?php echo $data['lastname']; ?>">
            <label>Last Name</label>
        </li>
        <!-- SNIP -->
        <li>
            <input type="text" name="super_ssn" value="<?php echo $data['super_ssn']; ?>">
            <label>Supervisor SSN</label>
        </li>
        <li>
            <input type="submit" name="update" value="Update">
        </li>
    </ul>
</form>

<?php
    makePageButtons();
}

( code hasnt been tested )

mysql_fetch_array doesn't work in this situation.

You need to call after execute()

$myrow= $qstmt->fetch(PDO::FETCH_ASSOC);

You try to fetch rows using mysql_fetch_array(), which needs a resource and you give it an object of class MDB2_Result. Dont mix vanilla functions with mdb2 methods. You should use $myrow=$qres->fetchRow(MDB2_FETCHMODE_ASSOC); Also your table_format() function, whatever it does, needs to be checked for types too.

$myrow=mysql_fetch_array($qres, MYSQL_ASSOC);

This is invalid. $qres is a PDO result object, which is absolutely meaningless to the mysql_*() functions. If you had error checking, such as

$myrow=mysql_fetch_array($qres, MYSQL_ASSOC) or die(mysql_error());
                                            ^^^^^^^^^^^^^^^^^^^^^^

you'd have gotten the usual "expected result handle, got XXX instead" error message.

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