Question

Using phpmyadmin (MySQL)

I used the following code to generate a query table:

SELECT
 s.WO_NUM,
 s.WO_LINE,
 s.DESCRIPTION,
 s.CUS_LOC_NAME,
 s.MFG_LINE,
 u1.username,
 u1.lastname AS MECHANICAL,
 u2.lastname AS APPLICATIONS,
 u3.lastname AS DESIGN
FROM production_schedule s, users u1, users u2, users u3

WHERE s.PM_MECHANICAL = u1.USERID
AND s.PM_APPLICATIONS = u2.USERID
AND s.PM_DESIGN = u3.USERID

The query table came out exactly how I wanted it to. At the bottom where it says "Bookmark this SQL Query" I gave it a label 'eng_schedule' and checked the box that says "Let every user access this bookmark". Then I clicked "Bookmark this Query"

I was under the assumption that I can now treat this query like a table (correct me if I'm wrong)

So in my php file I did this:

<table class='tablesorter tablesorter-jobs'>
    <thead>
    <tr>
      <th>JOB NUMBER</th>
      <th>MODEL</th>
      <th>LINE</th>
      <th>CUSTOMER</th>
      <th>AE</th>
      <th>PE</th>
      <th>DE</th>
      <th>CE</th>
    </tr>
    </thead>
    <tbody>
    <?php
        require_once ('../config.php');
        $user_name = $_SESSION['user_name'];
        $db = null;
        $limit = 10;
        $counter = 0;
            while (true) {
            try {
                $db = new PDO($dsn, $uname, $pword);
                $db->exec( "SET CHARACTER SET utf8" );
                $db->setAttribute( PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC ); 
                $db->setAttribute( PDO::ATTR_PERSISTENT, true );
                break;
            }
                catch (Exception $e) {
                    $db = null;
                    $counter++;
                    if ($counter == $limit)
                        throw $e;
                }
            }

        //$current_date = new DateTime();           
        $result = $db->prepare("SELECT * FROM eng_schedule");
        $result->execute();
        while ($row = $result->fetch(PDO::FETCH_ASSOC)){
            $wonum=$row["WO_NUM"];
            $x=$row["WO_LINE"];
            $model=$row["DESCRIPTION"];
            $a=$row["MFG_LINE"];
            $customer=$row["CUS_LOC_NAME"];
            $proj_eng=$row["PM_MECHANICAL"];
            $app_eng= $row["PM_APPLICATIONS"];
            $design_eng=$row["PM_DESIGN"];
            $controls_eng=$row["PM_PROGRAM"];

            $olddate = DateTime::createFromFormat('m/d/y', $date_initiated);    // for example, see manual for formats
            $today = new DateTime();
            $diff = $today->diff($olddate);
            $age = $diff->format('%a');

            $x = ltrim($x, '0');

            if (strlen($x) > 1) {
                $woline = $x;
            } else {
                $woline = "-0".$x;
            }
            $jobnum = $wonum ."". $woline;
            if ($a == 1) {
                $prod = "A"; 
            } else if ($a == 2) {
                $prod = "B";
            } else if ($a == 3) {
                $prod = "C"; 
            } else {
                $prod = "N/A"; 
            }
            $jobnum = $wonum . "" . $woline;

            echo "<tr><td>".$jobnum."</td>";
            echo "<td>".$model."</td>";
            echo "<td>".$prod."</td>";
            echo "<td>".$customer."</td>";
            echo "<td>".$app_eng."</td>";
            echo "<td>".$proj_eng."</td>";
            echo "<td>".$design_eng."</td>";
            echo "<td>".$controls_eng."</td></tr>";
            }
            ?>
        </tbody>
  </table>

But I'm getting NOTHING in the tbody tags. I checked the code and tried to use the master table 'production_schedule' and it worked great. But I can't query 'eng_schedule'.

Am I missing something here? Why can't I fetch from the query?

Was it helpful?

Solution

Bookmarking a query helps you to reuse a query in the administrator tool, but it won't let you use that alias from other applications that use the database.

The think you are looking for, is a view. See: http://dev.mysql.com/doc/refman/5.0/en/create-view.html

The syntax would be:

CREATE VIEW eng_schedule AS
SELECT
 s.WO_NUM,
 s.WO_LINE,
 s.DESCRIPTION,
 s.CUS_LOC_NAME,
 s.MFG_LINE,
 u1.username,
 u1.lastname AS MECHANICAL,
 u2.lastname AS APPLICATIONS,
 u3.lastname AS DESIGN
FROM production_schedule s, users u1, users u2, users u3

WHERE s.PM_MECHANICAL = u1.USERID
AND s.PM_APPLICATIONS = u2.USERID
AND s.PM_DESIGN = u3.USERID

After that, you can use the name of the view as a table name in select queries.

B.t.w. if executing a query in PDO fails, you might want to inspect the error information that PDO returns. See: http://nl3.php.net/pdo.errorinfo In this case, you would probably see an error about a missing table, confirming that bookmarking the query indeed didn't work. The error information is useful for yourself and for people helping you, because you will get an actual error message, so you can quickly rule out silly typing errors in the query.

OTHER TIPS

I don't think that the link in phpmyadmin is what you were looking for, the link is probably now accessible by all user of phpMyAdmin on the web, but it did not create a view in MySQL.

syntax to create a view can be found here : http://dev.mysql.com/doc/refman/5.5/en/create-view.html

If you want to use it as you described, you probably want to use a view.

You can create a view using

CREATE VIEW eng_schedule as 
SELECT
 s.WO_NUM,
 s.WO_LINE,
 s.DESCRIPTION,
 s.CUS_LOC_NAME,
 s.MFG_LINE,
 u1.username,
 u1.lastname AS MECHANICAL,
 u2.lastname AS APPLICATIONS,
 u3.lastname AS DESIGN
FROM production_schedule s, users u1, users u2, users u3
WHERE s.PM_MECHANICAL = u1.USERID
AND s.PM_APPLICATIONS = u2.USERID
AND s.PM_DESIGN = u3.USERID
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top