dtf54486 2016-03-23 20:59
浏览 49
已采纳

将PHP变量值与MySQL列值进行比较

I receive a post and want to do some actions only if the value of variable received in post exists in a specific column of the table. So i did this:

$pedidoID = $_POST["pedidoID"];
$con = mysql_connect("127.0.0.1", "root", "password") or die("Could not connect: " . mysql_error());
$result = mysql_query('SELECT id FROM listapagamento WHERE numeroPedido = "pedidoID"');
if(mysql_num_rows($result) == 0) {
//Some actions
}

So if the value from pedidoID doesn't exist in the column numeroPedido it will do the actions, because result will be 0 (because no rows are found).

What is happenning is that the $result is returning as bool(false) in both cases (if the value exists or not). I guess that my problem is how I'm using the variable inside the SELECT to compare to the column. I've tried to insert $_POST["pedidoID"] inside the SELECT also but my syntax was also wrong.

Does anyone know the correct syntax to use?

  • 写回答

3条回答 默认 最新

  • dsfjnxjlbqv9812 2016-03-23 21:08
    关注

    Try:

    $pedidoID = mysql_real_escape_string($_POST["pedidoID"]);
    $con = mysql_connect("127.0.0.1", "root", "password") or die("Could not connect: " . mysql_error());
    mysql_select_db('<your_database_name>', $con);
    $result = mysql_query("SELECT id FROM listapagamento WHERE numeroPedido = 'pedidoID'");
    if(mysql_num_rows($result) == 0) {
    //Some actions
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器