首先,我知道这个问题曾经被问过,但是我正在寻找一个可行的解决方案已有一个多星期了,但找不到。因此,请问有人可以帮助我,而不仅仅是将我的问题标记为重复吗?已经谢谢了!!
所以问题是这样的:我正在尝试检查数据库中是否存在mysql表(使用php)。有谁知道我该如何解决这个问题?据我所知,这是我的代码:
<form id="newChat" method="post">
Start a new chat:
<input type="text" name="name" />
<input type="submit" value="Search" />
</form>
<?php
function checkChat($me, $friend) {
global $conn;
$chatid = findChat($me, $friend);
$sql = 'SHOW TABLES FROM chatboxDB LIKE \'chat10\'';
$result = mysqli_query($conn, $sql) or trigger_error(mysql_error()." in ".$query);
if ($result > 0) {
echo '<script>alert("It works.");</script>';
}
}
if (isset($_POST)) {
$friend = checkInput($_POST["name"]);
unset($_POST["name"]);
checkChat($me, $friend);
}
?>
一些说明:
if ($result > 0)
->这似乎总是正确的...编辑:现在这是我的代码,并且可以正常工作:(我发现它可以更简单地完成)
<?php
function checkChat($me, $friend) {
global $conn, $dbname;
$chatid = findChat($me, $friend); # the findChat() is a query to find the name of a chat by giving the 2 persons from that chat, if one of those 2 persons does not exist, findChat will return nothing)
if (!$chatid) {
# the chat does not exist
}
}
if (isset($_POST)) {
$friend = checkInput($_POST["name"]);
$_POST = array();
checkChat($me, $friend);
}
?>
您可以尝试以下操作:
SELECT *
FROM information_schema.tables
WHERE table_schema = 'yourdb'
AND table_name = 'testtable'
LIMIT 1;
或者,您可以使用SHOW TABLES
SHOW TABLES LIKE 'yourtable';
如果结果集中有一行,则表不存在。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句