SELECT CONCAT查询的echo结果?

时间:2013-02-04 12:45:29

标签: php mysql sql join phpmyadmin

所以,我正在尝试运行SELECT CONCAT查询,返回查询结果并基本上将它们显示在.php网页上。例如,如果我手动将以下SQL代码放入PHPMyAdmin:

SELECT CONCAT(m.Name, ' ', p.Model, ' - ', p.StorageSize, ' - ', o.Name, ' - ', p.Price)
FROM phones p
JOIN manufacturer m
ON p.ManufacturerID = m.ManufacturerID
JOIN operatingsystem o
ON p.OSID = o.OSID

PHPMyAdmin运行它并显示“Apple iPhone 5 - 16GB - iOS - 499.99。但是,我试图将其显示在.php页面上,而不仅仅是通过PHPMyAdmin,并且无法使其工作。我'猜测我需要使用某种echo命令但是当我使用例如echo $ result时,它在网页上或者有时“资源ID#3”都没有显示任何内容。我的整个PHP代码如下:

<html>
<head>
<LINK rel="stylesheet" href="style.css">
<title>Phones | Mobiles Four You</title>
</head>

<body>
<div id="container"> 
<div id"header"><img src="images/logo.png" alt="Logo"/></div>
<div id="navbar">
<ul>
<li><a id=”nav-home” class=”first” href="index.php">Home</a></li>
<li><a id=”nav-phones” href="phones.php?sort=Make">Phones</a></li>
<li><a id=”nav-search” href="search.php">Search</a></li>
<li><a id=”nav-login” href="login.php">Login</a></li>
<li><a id=”nav-contact” class=”last” href="contact.php">Contact</a></li>
</ul>
</div>

<div id="content">
<h1>Mobile Phones</h1>
</div>


<?php
$con = mysql_connect("b*********", "*****", "*****");
if (!$con)
{
die('Could not connect: '  . mysql_error());
}

mysql_select_db("bg29li", $con);
$query = ("SELECT CONCAT(m.Name, ' ', p.Model, ' - ', p.StorageSize, ' - ', o.Name, ' -             ', p.Price)
FROM phones p
JOIN manufacturer m
ON p.ManufacturerID = m.ManufacturerID
JOIN operatingsystem o
ON p.OSID = o.OSID"); 

mysql_close($con);
?>
</body>
</html>

任何帮助都会受到赞赏

4 个答案:

答案 0 :(得分:1)

为连接的名称添加别名,然后遍历行并回显它们:

mysql_select_db("bg29li", $con);
$query = ("SELECT CONCAT(m.Name, ' ', p.Model, ' - ', p.StorageSize, ' - ', o.Name, ' -             ', '£' , p.Price) AS Names
FROM phones p
JOIN manufacturer m
ON p.ManufacturerID = m.ManufacturerID
JOIN operatingsystem o
ON p.OSID = o.OSID"); 

$result = mysql_query($query);

while($row=mysql_fetch_array($result))
{
    // here you can get the values using $row['names']
    echo $row['names'];

} 

mysql_close($con);

但是,请停止使用mysql_query扩展并改为使用PDO或预备语句。

答案 1 :(得分:0)

$query = "SELECT CONCAT(m.Name, ' ', p.Model, ' - ', p.StorageSize, ' - ', o.Name, ' -             ', p.Price)  AS val
FROM phones p
JOIN manufacturer m
ON p.ManufacturerID = m.ManufacturerID
JOIN operatingsystem o
ON p.OSID = o.OSID"; 

$res  = mysql_query($query);

while($row = mysql_fetch_assoc($res)){
    print_r($row);
}

答案 2 :(得分:0)

mysql_select_db(“bg29li”,$ con);

首先创建以字符串格式运行的SQL查询

$ query =“SELECT CONCAT(m.Name,'',p.Model,' - ',p.StorageSize,' - ',o.Name,' - ',p.Price) 来自手机p 加入制造商m ON p.ManufacturerID = m.ManufacturerID JOIN operatingsystem o ON p.OSID = o.OSID“;

之后运行此查询,您将获得结果$ result变量

$ result = mysql_query($ query);

while($ row = mysql_fetch_array($ result,MYSQL_NUM)){

printf("%s  ", $row[0]);  

}

答案 3 :(得分:0)

    $query = ("SELECT CONCAT(m.Name, ' ', p.Model, ' - ', p.StorageSize, ' - ', o.Name, ' -             ', p.Price)
FROM phones p
JOIN manufacturer m
ON p.ManufacturerID = m.ManufacturerID
JOIN operatingsystem o
ON p.OSID = o.OSID");

这不足以使mysql_query($query)需要执行,否则只需要一个字符串变量,然后你将不得不使用mysql_fetch_array或mysql_fetch_object来获得结果