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 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!