dragon456101
2018-03-08 06:38
浏览 60
已采纳

使用PDO根据另一个表的第一个值查询MYSQL数据库

Hi I am having trouble querying a mysql table based on the first value of another table here is my code.

<?php

$host    = 'localhost';
$db      = 'msdds';
$user    = 'root';
$pass    = '';
$charset = 'utf8';



$dsn = "mysql:host=$host;dbname=$db;charset=$charset";



$opt = [
        PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        PDO::ATTR_EMULATE_PREPARES   => false,
        ];


$dbh = new PDO($dsn, $user, $pass, $opt);



$sql  = $dbh->query("SELECT name FROM employee WHERE remarks != 'done' and branch ='EGYPT' and tenure=""");
$rows = $sql->fetchAll();
echo json_encode($rows);



?>

I want to add a query on tenure based on the first value of another table. I need to query from another table currently it is connected to employee table but I have another table that contains tenure and based on the first value there will be the content of tenure

图片转代码服务由CSDN问答提供 功能建议

您好我查询基于另一个表的第一个值的mysql表这里是我的代码。

 &lt;?php 
 
 $ host ='localhost'; 
 $ db ='msdds'; 
 $ user ='root'; 
 $ pass =  ''; 
 $ charset ='utf8'; 
 
 
 
 $ dsn =“mysql:host = $ host; dbname = $ db; charset = $ charset”; 
 
 
 
  $ opt = [
 PDO :: ATTR_ERRMODE =&gt;  PDO :: ERRMODE_EXCEPTION,
 PDO :: ATTR_DEFAULT_FETCH_MODE =&gt;  PDO :: FETCH_ASSOC,
 PDO :: ATTR_EMULATE_PREPARES =&gt;  false,
]; 
 
 
 $ dbh =新PDO($ dsn,$ user,$ pass,$ opt); 
 
 
 
 $ sql = $ dbh-&gt; query(“  SELECT name FROM employee WHERE remarks!='done'和branch ='EGYPT'和tenure =“”“); 
 $ rows = $ sql-&gt; fetchAll(); 
echo json_encode($ rows); 
 \  n 
 
?&gt; 
   
 
 

我想根据另一个表的第一个值添加终身查询。 我需要从另一个表中查询当前它连接到employee表但我有另一个包含任期的表,并且基于第一个值将有权属的内容

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doujuegai8830 2018-03-08 06:40
    已采纳

    Replace your Query with this

    $sql = $dbh->query("SELECT name FROM employee WHERE remarks != 'done' AND (branch ='EGYPT' and tenure='')");

    $rows = $sql->fetchAll();

    tenure is also equal to single quote.

    打赏 评论

相关推荐 更多相似问题