1

My apologies for being a cut and paste user.

What I am attempting to do is create a basic admin lookup for DB users.

I get no error message fro DB but no results display.

What I am attempting to do is get DB info of a User_ID using form post method from a form where Admin looksup user info based on user ID.

Here is my php

<?php
 $con=mysqli_connect("dbhost","username","password","dbase");
 // Check connection
 if (mysqli_connect_errno()) {
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
 }
 $result = mysqli_query($con,"SELECT 'User_ID', 'Username', 'Email', 'Group_ID' * FROM table
 WHERE User_ID= $User_ID ");
 echo $row['User_ID'] . " " . $row['Username'] . " " . $row['Email']. " " . $row['Group_ID'];
 echo "<br>";
 ?> 

I greatly appreciate your assistance

Jeff

  • Thanks all for your time and suggestions. I have attempted various experiments using the posted suggestions. Still no joy. I receive no Error from the DB server but no echo results either, – Jeffrey Murphy Sep 26 '14 at 10:01

2 Answers2

1

This query have lot of errors:

 $result = mysqli_query($con,"SELECT 'User_ID', 'Username', 'Email', 'Group_ID' * FROM table
 WHERE User_ID= $User_ID ");

1) Undefined variable $User_ID

2) You are using both * and column names in query

3) Use of quotes for columns. Instead you can use backtits.

Change your query to:

 $result = mysqli_query($con,"SELECT `User_ID`, `Username`, `Email`, `Group_ID`  FROM table
     WHERE User_ID= $User_ID ");
Jenz
  • 8,280
  • 7
  • 44
  • 77
0

REMARKS

  • Your using apostrophe (') on your column name, which should not and should only be used on variable/s inside the query. You can use back ticks (`) instead of apostrophe (') on your column name.
  • If you're gonna select the specific column names on your query, you should not include the asterisk (*) along with them.
  • Next is you don't have a fetch loop to get the results.
  • You need to use apostrophe (') when having a variable/s in your condition in your query.
  • Did you assign the submitted input into $User_ID variable?

Right Code:

<?php

$con=mysqli_connect("dbhost","username","password","dbase");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT User_ID, Username, Email, Group_ID FROM table
WHERE User_ID= '$User_ID'");

while($row=mysqli_fetch_array($result)){
echo $row['User_ID'] . " " . $row['Username'] . " " . $row['Email']. " " . $row['Group_ID'];
echo "<br>";

} /* END OF WHILE LOOP */

?> 

You can also use prepared statement instead to prevent further SQL injections. Here's what it would look like if you had it in prepared statement:

<?php

$con=mysqli_connect("dbhost","username","password","dbase");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

if ($stmt = $con->prepare("SELECT User_ID, Username, Email, Group_ID FROM table WHERE User_ID=?")) {

  $stmt->bind_param("s", $User_ID); /* LETS ASSUME THAT THE SUBMITTED DATA IS STORED INTO $User_ID VARIABLE */

  $stmt->execute();

  $stmt->bind_result($userid,$username,$email,$groupid);

  while ($stmt->fetch()) {
    printf ("%i %s %s %i<br>", $userid,$username,$email,$groupid); /* YOU CAN REPLACE THE NECESSARY STRING FORMAT IF YOU NEED TO */
  } /* END OF WHILE LOOP */

  $stmt->close();
}

$con->close();

?>
Community
  • 1
  • 1
Logan Wayne
  • 6,001
  • 16
  • 31
  • 49
  • Thanks Logan, I appreciate your assistance. No Error displayed but no results displayed either. I am going back to look at my input form...maybe the variable is not being stored. However I thought that the input form was the easy but. I have also changed the name form input variable so it was not conflicting with the nae of one of the table rows. Still testing. The fun part is when you finally get it to work. – Jeffrey Murphy Sep 27 '14 at 05:33
  • Hoorah!! Many thanks Logan. I persisted with it and finally got it to work. I used the first corrected code as I understand it better[still very much a beginner] I figured out that the 'where' clause needed changing, took a stab at it and presto. working like a charm – Jeffrey Murphy Sep 29 '14 at 23:28
  • @JeffreyMurphy - no problem. If my answer did help you solve your problem, don't hesitate to accept my answer as the correct one or up-vote my answer. – Logan Wayne Sep 30 '14 at 00:22