我想从两个表中选择记录。这两张table的前缀是“shop\”。
如何在sql语句中为两个商店选择记录?
我目前的声明:
// Select
$stmt = $mysqli->prepare("SELECT name, html_id, price FROM database_name WHERE TABLE_NAME LIKE 'shop%'");
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_assoc())
{
$arr[] = $row;
}
$name = [];
foreach($arr as $arrs)
{
$name[] = array(0 => $arrs['name'], 1 => $arrs['html_id'], 2 => $arrs['price']); //here
}
$stmt->close();
print_r($name);
当前错误为:
Fatal error: Uncaught Error: Call to a member function execute() on boolean in C:\wamp642\www\webcart\search.php on line 17 and line 17 is: $stmt->execute();
我可以使用以下命令使表格“显示”:
$stmt = $mysqli->prepare("show tables like '%shop%'");
但它没有记录,我想只是一个物体。
“show tables like“%shop%”的输出输出输出了2个数组,但这些数组是空的,没有数据/记录。
我认为sql语句需要改进。谢谢。
编辑:
我也试过:
$stmt = $mysqli->prepare("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='feeds' AND TABLE_NAME LIKE 'shop%'");
编辑:search.php的内容
<?php
include 'define.php';
$mysqli = new mysqli($host, $db_user, $db_password, $db_database);
if($mysqli->connect_error)
{
?><script>var profiles_delete_modal_1 = ' Error 3: Problem deleteing profile. Make sure database login credentials are correct.';</script>
<script>$(".modal").css({"opacity":"1", "z-index":"100"});$(".modal_mid").html("<pre>" + profiles_delete_modal_1 + "</pre>");setTimeout(function(){$(".modal").css({"opacity":"0", "z-index":"-100"});},5000);</script><?php
exit;
}
$shop = 'shop';
// Select
//$stmt = $mysqli->prepare("show tables like '%shop%'");
//$stmt = $mysqli->prepare("SELECT * FROM feeds WHERE TABLE_NAME LIKE 'shop%'");
$stmt = $mysqli->prepare("SELECT name, html_id, price FROM database_name WHERE TABLE_NAME LIKE 'shop%'");
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_assoc())
{
$arr[] = $row;
}
$n=0;
$name = [];
foreach($arr as $arrs)
{
$name[] = array(0 => $arrs['name'], 1 => $arrs['html_id'], 2 => $arrs['price']); //here
$n++;
}
$stmt->close();
print_r($name);
以及define.php的内容:
$www_dir = 'webcart';
$url_root = 'http://localhost/' . $www_dir . '';
$www_dir_slash = $_SERVER['DOCUMENT_ROOT'] . '' . $www_dir . '/';
$host = 'localhost';
$db_user = 'webcart_admin';
$db_password = 'asd123';
$db_database = 'shop';
$_SESSION['host'] = $host;
$_SESSION['db_user'] = $db_user;
$_SESSION['db_password'] = $db_password;
$_SESSION['db_database'] = $db_database;
编辑
从下面的答案开始,我可以创建一个如下的字符串:
SELECT name, html_id, price FROM shop_a UNION shop_b
但是它不会正确执行。
这是我的密码:
$stmt = $mysqli->prepare("SELECT name, html_id, price FROM shop_a UNION shop_b");
$result = $stmt->execute();
它给出以下错误:
Fatal error: Uncaught Error: Call to a member function execute() on boolean in C:\wamp642\www\webcart\search.php on line 43
我明白了。
我很快就把答案贴上去。声明是这样的:
"SELECT name, html_id, price FROM shop_a UNION SELECT name, html_id, price from shop_b"
2条答案
按热度按时间wfveoks01#
SELECT
查询用于从db表中获取数据,而不是db本身。所以需要在FROM
你的问题的一部分。考虑到您正试图从类似的表(相同的字段)中获取数据。。。
您可能需要添加条件来处理所有场景。
um6iljoc2#
我基本上是从musafar那里得到答案的,尽管我需要找到数组中的对象。所以我用了一些foreach循环来做这个,因为我不知道其他的方法。如果有其他方法获取mysqli\u对象数据,请告诉我。