检查表信息时是否存在PHP / SQL问题

时间:2016-05-04 03:02:11

标签: php mysql sql

如果信息存在,我的一个页面将显示表格中的信息,但是它输出的错误表明它不存在。

$get_provider = str_clean($_GET['provider']);

$provider_sql = mysql_query("SELECT * FROM `payment_providers` WHERE `provider_seo_name`='".$get_provider."'");
$row = mysql_fetch_array($provider_sql);

$check_provider = mysql_query("SELECT * FROM `payment_providers` WHERE `provider_seo_name`='".$get_provider."'");
$provider_exists = mysql_num_rows($check_provider);

\\\\\\\\\\\\\\\\\\\\\\\\\\\\

<?php if($provider_exists == 0) { ?>
<h2 class="page-header">Error!</h2>
<div class="alert alert-danger">You never specified a payment provider to redeem with.</div>
<?php } else { ?>
<h2 class="page-header"><?php echo $row['provider_name']; ?></h2>
<p><?php echo $row['provider_description']; ?></p> 

此表和特定的provider_seo_name中存在信息,但由于某种原因,它不允许显示或认为它不存在。之前我遇到了页面扩展的问题,我编辑的是将.php添加到位置

我已经搜索了可能的原因,但我无法弄明白。如果您需要更多信息,我很乐意提供

2 个答案:

答案 0 :(得分:0)

我可以看到两者都提供了正确的理由,说明它无法正常运作

http://www.php.net/manual/en/types.comparisons.php

我在检查provier_exists时使用了错误的类型!空工作和== 0不是因为它不正确地检查单元格值

我还将研究MYSQLi而不是mysql_函数,以避免再次发生这种情况。谢谢你们

答案 1 :(得分:0)

// Database info
$host = "localhost";
$database = "crm";
$user = "root";
$password = "bijoux22";

// Create a database connection w/ PDO
$pdo = new PDO('mysql:host='.$host.';dbname='.$database.';charset=utf8', $user, $password);

// Make a prepared statemnt
$q = $pdo->prepare("SELECT * FROM `payment_providers` WHERE `provider_seo_name`= :provider");

// Execute it with your variables
$q->execute(array(":provider"=>$_GET['provider']));

// Get the results
$row = $q->fetch();

// check if there were matches
if(count($row)) // provider exists
else // provider doesn't exist

请参阅:http://php.net/manual/en/pdo.prepared-statements.php