Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如果MYSQL字段A等于MYSQL字段B,则打印MYSQL字段C_Php_Mysql - Fatal编程技术网

Php 如果MYSQL字段A等于MYSQL字段B,则打印MYSQL字段C

Php 如果MYSQL字段A等于MYSQL字段B,则打印MYSQL字段C,php,mysql,Php,Mysql,我想打印一个名为“res_properties”的表的内容,以获得关于我的公文包的概述,但不知道语法(PHP知道我,但我不知道PHP…): 我有一个名为'res_properties'的表和一个名为'location_id'的列 我有另一个名为'res_location'的表和两列名为'id'和'slug' 其中列“slug”包含该位置的可读内容 样品 “id”是“3”等于“California”(slug的内容) “id”等于“Virginia”(“slug”的内容) 等等 打印时,我需要查找

我想打印一个名为“res_properties”的表的内容,以获得关于我的公文包的概述,但不知道语法(PHP知道我,但我不知道PHP…):

我有一个名为'res_properties'的表和一个名为'location_id'的列 我有另一个名为'res_location'的表和两列名为'id'和'slug' 其中列“slug”包含该位置的可读内容

样品 “id”是“3”等于“California”(slug的内容) “id”等于“Virginia”(“slug”的内容) 等等

打印时,我需要查找表'res_location',并且'location_id'等于'id' 然后用“slug”的内容替换“id”(或将“slug”的内容移动到一个新字段中,例如“location_name”(25个字符),并打印该字段

到目前为止,我的代码是这样的,非常感谢您的帮助。非常感谢:

<html>
<head>
<title>Portfolio</title>
</head>
<link rel="stylesheet" type="text/css" href="../scripts/css/formate.css" />
<body>

<?php 
 // Connects to Database 
 mysql_connect("localhost","user","passw") or die(mysql_error()); 
 mysql_select_db("db_name") or die(mysql_error()); 
 $data = mysql_query("SELECT * FROM res_properties"); 
 $location = mysql_query("SELECT * FROM res_locations")

 or die(mysql_error()); 

 Print "<table border cellpadding=3>"; 
 Print "<br><h3>Portfolio</h3><p><br>"; 
   Print "<table border cellpadding=3>"; 
   Print "<tr align='left'><th width=130>Villa Name</th><th width=40>Beds</th><th width=40>Baths</th><th width=60>Sleeps</th><th width=40>Location</th><th width=40>Loc-ID</th><th width=300 >Site URL</th></tr>"; 

   while($info2 = mysql_fetch_array( $location )) 
   while($info = mysql_fetch_array( $data )) 

 { 

 Print "<tr>"; 
 Print "<td>".$info['ref_id'] . "</td> "; 
 Print "<td>".$info['bedrooms'] . " </td>"; 
 Print "<td>".$info['bathrooms'] . " </td>"; 
 Print "<td>".$info['max_occupants'] . " </td>"; 
 Print "<td>".$info2['slug'] . " </td>"; 
 Print "<td>".$info2['id'] . " </td>"; 

// here I want to print a clickable URL but some syntax error:
// Print "<td>" http://www.domain_name.com/'.$info['slug'] .'.html;

// when using the echo command it works fine like this, but doesn't output a clickable URL:
// echo 'http://www.domain_name.com/'.$info['slug'] .'.html' . "<br />"; 


 } 
 Print "</table>"; 
 ?> 

</body>
</html>

文件夹

你的问题很难理解,但是如果题目是准确的,那么这可能就是你想要的

SELECT IF(table.fieldA = table.fieldB, table.fieldC, NULL) FROM table;
格式为IF(条件,如果为真则输出,如果为假则输出)


现在,如果我的答案正确,但不完全正确,您告诉我,我将更新它。

在mysql中使用连接,那么您不必担心PHP脚本,使用下面的sql查询来获取数据

$data = mysql_query("select
rl.id,
rl.slug
from res_location as rl
join res_properties as rp on rp.location_id=rl.id");

我知道您想要打印表res_属性和res_位置的内容。表res_属性包含位于某些状态的属性。在输出中,您想要打印属性和属性所在的状态。最好的方法是打印表res_属性和位置使用res_属性的列location_id和res_locations的列id在单个sql语句中设置res_位置:

$data = mysql_query("SELECT * FROM res_properties t1 INNER JOIN res_locations t2 ON t1.location_id = t2.id");
通过join语句,mysql返回一个单独的联接表,其中包含与单个属性对应的全部信息。现在迭代$data并打印值:

print "<table>";
while($info = mysql_fetch_array( $data )) {
   print "<tr>"; 
   print "<td>" . $info['ref_id'] . "</td> "; 
   print "<td>" . $info['bedrooms'] . "</td>"; 
   print "<td>" . $info['bathrooms'] . "</td>"; 
   print "<td>" . $info['max_occupants'] . "</td>"; 
   print "<td>" . $info['slug'] . "</td>"; 
   print "<td>" . $info['id'] . "</td>";
   print "<td>http://www.domain_name.com/" . $info['slug'] . ".html</td>";
}
print "</table>";
还要注意在语句中使用正确的php语法,以避免语法错误!例如,要打印纯字符串值,必须使用parantesse:

print "hello world";

我希望我的代码没有语法错误,我回答了你的问题。

Joel-你就是那个人!完全理解并实现了你的代码,没有语法错误,工作起来很有魅力!!!我现在要退出了,因为我手头有所有数据来打印完整的详细列表!我唯一要找的是可点击的URL,它我必须将以下内容组合在一起:网址修复:www.domain-name/DB检索字段:xxxxxx(字段名为“slug”)Html结尾修复:.Html构建时可单击的URL应如下所示:www.domain-name/xxxxxx.Html我可以再次请求您的帮助吗?非常感谢!这很容易。要获得可单击的URL,您只需使用Html:print“”的a标记;有关a标记访问的更多信息:。谢谢@joel!也很好。我得到了一个可单击的URL!但我认为我需要保护该列的内容。$info['slug']。在加入表之前。它现在包含人类可读的位置名(并放在.html之前).BTW:我不想浪费您太多的时间,但请再问一个问题,然后我就完成了:为了打印属性的缩略图,我可以再将一个列为“id”和冒号为“filename”的表“resu images”连接到这个sql语句中:$data=mysql\u query吗(“选择*从t1上的资源属性t1内部连接资源位置t2。位置id=t2.id”);好的。不客气。别忘了标记一个回答作为回答您问题的答案。Srihari Goud也谢谢您!当我尝试joels建议它工作完美!Buttle Butkus-英语不是我的母语,是的,我同意,不容易理解我的意思,但找到了一个解决方案(如下)。也感谢您的支持;-)
print "hello world";