0

这是我的第一篇文章,所以请耐心等待我在这里输入代码。我试图将一些图像输出到 PDF,并且需要创建一个 if 语句来连续查找数据。

$connection = mysql_connect("localhost", "testdb", "********")
    or die ("Unable to connect!");    

// select database  
mysql_select_db("testdb") or die ("Unable to select database!");      

// Select all the rows in the test table
$query = "SELECT * FROM test2 WHERE testid=89";
$result = mysql_query($query);
if (!$result) {
    die('Invalid query: ' . mysql_error());
}

while ($row= mysql_fetch_array($result)) {
    $image = $row[1]; 
    $text = $row[2];
}

这就是我到目前为止所拥有的,基本上我需要一些类似的东西:

If (data in row 1) {
    print $image;
} else {
    print $text;
}
4

5 回答 5

2

很难确切地说出您要查找的内容,因为它不是很清楚,但是我认为您想要做的是检查是否$image有值,如果有,请显示它。如果没有,$text则改为显示。

如果是这种情况,请使用empty(). 它会告诉您变量是否为空。

if (!empty($image))
 {
   print $image;
 }
else
 {
  print $text;
 }

以下内容被认为是空的:

  • ""(一个空字符串)
  • 0(0 为整数)
  • 0.0(0 作为浮点数)
  • "0" (0 作为字符串)
  • 无效的
  • 错误的
  • array() (一个空数组)
  • $var; (声明的变量,但没有值)
于 2012-08-08T12:00:20.200 回答
1

看起来你只需要测试 $image 中的数据

if(!empty($image))
{
    echo $image;
}
else
{
    echo $text;
}
于 2012-08-08T11:59:53.613 回答
0
if( !empty($row[1]) ) { 
   ...
于 2012-08-08T12:00:39.893 回答
0

使用 isset 检查变量。

喜欢

if(isset($images) !='')
{
echo $images;
}
于 2012-08-08T12:00:45.967 回答
0

尽管您正在使用mysql_*已折旧的旧功能,但您几乎就在那里

$connection = mysql_connect("localhost", "testdb", "********") or die ("Unable to connect!");    
// select database  
 mysql_select_db("testdb") or die ("Unable to select database!");      
  // Select all the rows in the test table
    $query = "SELECT * FROM test2 WHERE testid=89";
    $result = mysql_query($query);
    if (!$result) {
     die('Invalid query: ' . mysql_error());
    }

while ($row= mysql_fetch_array($result))
// This will only be called if there is a matching result.
{
 echo $row[1]; 
 echo $row[2];
}

编辑:这是一个在 Eclipse 中打开的查询部分的剪切和粘贴:

$arrKPIData = Array();
try{
    $dbh = new PDO($this->mySQLAccessData->hostname, $this->mySQLAccessData->username, $this->mySQLAccessData->password);
    $stmt = $dbh->query($sql);
    $obj = $stmt->setFetchMode(PDO::FETCH_INTO, new kpiData);
    $dataCount=0;
    foreach($stmt as $kpiData)
    {
        $arrKPIData[$dataCount]['year']=$kpiData->year;
        $arrKPIData[$dataCount]['month']=$kpiData->month;
        $arrKPIData[$dataCount]['measure']=$kpiData->kpiMeasure;
        $arrKPIData[$dataCount]['var1']=$kpiData->var1;
        $arrKPIData[$dataCount]['var2']=$kpiData->var2;
        $dataCount++;
        unset($stmt);
    }
    unset($dbh);
}
catch(PDOException $e){
    echo 'Error : '.$e->getMessage();
    exit();
}
unset($arrKPIData);

在清理它并将其进一步转换为代码中的类之前,我正在用数据填充一个简单的数组。

于 2012-08-08T12:00:23.213 回答