将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

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.

You have an extra closing brace on line 14 (as the error says nicely):

$row = mysqli_fetch_array($result));

Should be:

$row = mysqli_fetch_array($result);

one thing that pops out is

$row = mysqli_fetch_array($result));

should be:

$row = mysqli_fetch_array($result);

note bracket at end of line.