简体   繁体   English

从mysql DB中检索数据不起作用/ PHP

[英]Retrieve data from mysql DB doesn't work /PHP

I hvae the following PHP source: 我有以下PHP源代码:

$type_ID =$_GET["typeID"];
try{                               
$article_ID =$_GET["articleID"];
$select_query = mysql_query("SELECT articleContent, articleTitle From articles WHERE articleID=$article_ID && typeID=$type_ID");
}
catch(Exception $e)
{ $select_query = mysql_query("SELECT articleContent, articleTitle From articles WHERE typeID=$type_ID");
                                   }
$row = mysql_fetch_assoc($select_query); 
echo '<h1>'.$row['articleTitle'].'</h1>';
echo  $row['articleContent'];

I know that this code is no safe, and yo can easlily do sql injection. 我知道这段代码不安全,你可以轻松地做sql注入。

There problem here is that it's didn't go into the catch part (after the try )even when it should. 这里的问题是它没有进入catch部分(在try ),即使它应该。 The solution may be easy but I can't solve it. 解决方案可能很简单,但我无法解决。

Why it's didn't go into the catch section? 为什么它没有进入catch部分?

You'd have to change your queries to use the or to catch the fail in this case something like this may work though I'm not 100% (can anyone correct me?) You'd be far better off moving away from mysql_ functions though and moving to mysqli or pdo in an OO style then you can better trap and handle the errors. 你必须改变你的查询以使用或者捕获失败,在这种情况下这样的事情可能会起作用,虽然我不是100%(任何人都可以纠正我吗?)你离开mysql_功能会好得多虽然并以OO风格转移到mysqli或pdo,但您可以更好地捕获和处理错误。

$type_ID =$_GET["typeID"];
try{                               
$article_ID =$_GET["articleID"];
$select_query = mysql_query("SELECT articleContent, articleTitle From articles WHERE articleID=$article_ID && typeID=$type_ID") or throw new Exception("ERROR HERE");
}
catch(Exception $e)
{ 
 $select_query = mysql_query("SELECT articleContent, articleTitle From articles WHERE typeID=$type_ID"); // note we can't throw exception here because its already in the try catch. perhaps we should look at something like the finally statement.
//echo $e->getMessage(); //uncomment this line if you want to output the exception error text set above
}
$row = mysql_fetch_assoc($select_query); 
echo '<h1>'.$row['articleTitle'].'</h1>';
echo  $row['articleContent'];

Actually just had a thought you'd be much better doing something like this and validating your inputs before hand. 实际上只是想到你做得更好,并且事先验证你的输入。 (note i'm doing no string escaping here don't forget to do it) (注意我在这里没有字符串逃避不要忘记这样做)

$type_ID =$_GET["typeID"];
$article_ID =$_GET["articleID"];

if (strlen($type_ID)>0 && strlen($article_ID)>0 && is_numeric($type_ID) && is_numeric($article_ID)) { 
$sqlquery = "SELECT articleContent, articleTitle From articles WHERE articleID=$article_ID && typeID=$type_ID";
} else {
$sqlquery = "SELECT articleContent, articleTitle From articles WHERE typeID=$type_ID";
}

try {
    $queryresult = mysql_query($sqlquery) or throw new Exception("Query Failed");
} catch(Exception $e) { 
    echo $e->getMessage(); 
}

So basically you're validating and checking your inputs and switching your sql statements before then your try catch logic is purely for did the query succeed or fail which is far more sensible than what you were attempting. 所以基本上你是在验证和检查输入并切换你的sql语句之前你的try catch逻辑纯粹是为了查询成功或失败,这比你尝试的更加明智。

Mysql query will return FALSE on error Mysql查询将在出错时返回FALSE
So you can throw an exception for that 所以你可以抛出异常
$result = mysql_query("SELECT articleContent, articleTitle From articles WHERE articleID=$article_ID && typeID=$type_ID");

if(!$result) throw new Exception("Invalid query: ". mysql_error());

And catch them in your catch block 并抓住他们的捕获块
catch(Exception $e) { echo $e->getMessage()}

Its up to you what you will do with it echo or log. 它取决于你将用它做回声或日志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM