1

所以,我试图让一个 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

4 回答 4

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 或准备好的语句。

于 2013-02-04T12:49:32.257 回答
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);
}
于 2013-02-04T12:50:15.260 回答
0

mysql_select_db("bg29li", $con);

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

$query = "SELECT CONCAT(m.Name, ' ', p.Model, ' - ', p.StorageSize, ' - ', o.Name, ' - ', p.Price) FROM 电话 p 加入制造商 m ON p .ManufacturerID = m.ManufacturerID 加入操作系统 o ON p.OSID = o.OSID";

之后运行这个查询,你会得到 $result 变量的结果

$result = mysql_query($query);

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

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

}

于 2013-02-04T12:51:45.627 回答
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 来获得结果

于 2013-02-04T12:52:53.460 回答