dongsi2317 2014-10-16 16:33
浏览 51
已采纳

将MySQL查询的结果分配给字符串[关闭]

I'm working on a web app using PHP with MySQL. I have on another page successfully queried the database, and returned results to a dynamically created table. In this instance, I just need to get a name from the database.

I'm using the same basic principle and i'm attempting to assign the results to a variable, but it's bombing out on me. I know my connection is good. I run the query directly in PhpMyAdmin and it works.

Here's the code:

<?php
$con=mysqli_connect("....");

// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$id_num = mysqli_real_escape_string($con, $_POST['id_num']);
$username = mysqli_real_escape_string($con, $_POST['username']);

$result = mysqli_query($con,"SELECT NAME FROM users where ID ='".$id_num."' AND username='".$username."'");
$row = mysqli_fetch_array($result));
$name = $row['NAME'];
echo $name;
mysqli_close($con);
?>

The error it gives is:

Parse error: syntax error, unexpected ')' in D:\Hosting\8715276\html\delta\login.php on line 14

I'm sure this is due to my unfamiliarity with php. I've looked for specifics on how to do this but I'm not really turning much much. I investigated mysqli_fetch_array, but the examples on php.net print and do not assign to a variable. I sort of thought if (in their example) this works:

$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
printf ("%s (%s)
", $row["Name"], $row["CountryCode"]);

then

$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
    $name = $row['NAME'];
    echo $name;

likewise should work. It does not. Any suggestions or links would be helpful. I've been looking at http://php.net/manual/en/mysqli-result.fetch-array.php

  • 写回答

3条回答 默认 最新

  • dongyan4157 2014-10-16 17:37
    关注

    After some testing, I've come to some form of conclusion that you are not using the right letter-case for your columns, including the (username) column in the WHERE clause.

    You have and notice NAME in your select and username instead of name, and in another piece of code $row["Name"].

    Make sure that your column's name is indeed name and not NAME or username, this will depend on how/what you select, more information about this can be found further down below.

    Here is your code, which by the way had a bracket too many in mysqli_fetch_array($result));

    $result = mysqli_query($con,"SELECT NAME FROM users where ID ='".$id_num."'  
    AND username='".$username."'");
    
    $row = mysqli_fetch_array($result));
    $name = $row['NAME'];
    echo $name;
    

    This is what I concluded to work (as a diagnosis):

    $result = mysqli_query($con,"SELECT name FROM users where ID ='".$id_num."'  
    AND name='".$username."'");
    
    $row = mysqli_fetch_array($result);
    $name = $row['name'];
    echo $name;
    

    However, when you SELECT column, the $row['column'] must match what you select.

    So in doing:

    SELECT NAME and $row['NAME'] would work, but not SELECT NAME and $row['name'].

    In conjunction with your WHERE clause.

    Add error reporting to the top of your file(s) which will help find errors.

    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    

    Sidenote: Error reporting should only be done in staging, and never production.

    Including or die(mysqli_error($con)) to mysqli_query().


    Plus, as you stated in a comment under Shomz's answer:

    "I've narrowed it down to something in my sql query. If I replace ID ='".$id_num."' AND username='".$username."'"); with static inputs, works fine."

    This tells me that you may not have assigned the variables properly. Maybe an unnamed form element, or no variable at all. It's hard to say for certain because your question doesn't show enough information to know for sure (the HTML form itself), but those are possible scenarios.

    Using error reporting would have signaled an undefined index and/or undefined variable.

    If your form elements do not contain name="id_num" and name="username" respectively, then that would be another possible reason as to why your code was failing.

    • I'm just trying to get to the bottom of it, as to knowing/finding out exactly why your query failed.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler