检查数据是否在表格行
问题描述:
这是我的第一篇文章,所以请将代码输入到这里。我试图输出一些图像到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;
}
答
很难说你正在寻找什么,因为它不是很清楚,但我认为你想要做的是检查看看$image
公顷是一个价值,如果是的话,显示它。如果不是,则改为显示$text
。
如果是这种情况,请使用empty()
。它会告诉你一个变量是否为空。
if (!empty($image))
{
print $image;
}
else
{
print $text;
}
以下的事情被认为是空的:
- “”(空字符串)
- 0(0作为整数)
- 0.0(0为float)
- “0”(0作为字符串)
- NULL
- FALSE
- array()(一个空数组)
- $ var; (变量声明,但没有一个值)
答
看起来像你只需要在$图像测试数据
if(!empty($image))
{
echo $image;
}
else
{
echo $text;
}
答
虽然您使用的是贬值的老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);
我用数据填充一个简单的数组之前,我清理它,它进一步转化成一个类中的代码。
答
if(!empty($row[1])) {
...
贵表包含哪些数据? – Vamsi 2012-08-08 11:59:33
只是从表单输入的文本和图像路径 – Josh 2012-08-08 12:26:55