我必须搜索邮政编码是否在我的数据库中,我的表被称为“测试”,数据库中只有一个表具有一列和一行,该列名为“代码”,并且只有一行在INT 63000中,我在我的网站上有一个客户端输入代码的表格,它称为.php文件,该文件检查值是否丢失或是否存在于数据库中,我不知道PHP,所以这对我来说很难。 。:(我的代码不起作用:(
解决:这是工作代码:
<?php session_start(); ?>
<?php
if($_POST['code-postal'] === '') {
$hasError = true;
} else {
$variable = $_POST['code-postal'];
$code = intval($variable);
}
mysql_connect('xxxxxxxxx', 'xxxxxxxxxxxx', 'xxxxxxxxxxxx')
or die("I cannot connect to the database because: " . mysql_error());
mysql_select_db('xxxxxxxxxx');
$code = mysql_real_escape_string($code);
$sql = "SELECT COUNT(*) AS total_count FROM test WHERE codes='$code'";
$req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error());
$data = mysql_fetch_assoc($req);
if($data['total_count'] == 1) {
$verif = true;
}
else {
$verif = false;
}
// on ferme la connexion à mysql
mysql_close();
?>
$sql = "SELECT COUNT(*) AS total_count FROM test WHERE codes='$code'";
$req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error());
$data = mysql_fetch_assoc($req);
if($data['total_count'] == 1) {
$verif = true;
}
else {
$verif = false;
}
这是工作代码。
mysql_query
将返回结果集。您将需要使用mysql_fetch_assoc
函数从中检索数据。
我猜您将来在表中会有更多的行,因为正如您在表中提到的那样,您只有一个表只有一列和一行,那么就不需要数据库了,您可以直接比较值。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句