0

Trying to get all the users from my database, yet when I do so, the query fails.

Attempting to do so with

 $userNames = mysqli_query($con, "SELECT * FROM Login");

Where in PHPMyAdmin the database has a few records in the table login. I checked if the connection is connected, its connected.

Is there any reason this wouldn't work?

Here is a examplepage, you can try logging in with Username and Password.

EDIT: From the example page, here is the code used to test:

if (mysqli_connect_errno($con)) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error() . "<br />";
} else {
    echo "Connected to MySQL!<br />";
}
if(mysqli_query($con, "SELECT * FROM Login")){
    echo "Query good!<br />";
} else {
    echo "Query bad!<br />";
}

EDIT 2: Here is a screenshot of the table existing, and the data existing: Something

17
  • And $conn is a valid resource? Commented Jul 16, 2013 at 21:32
  • What does mysqli_error() return when it fails? Commented Jul 16, 2013 at 21:33
  • Added example code, (and its $con). If you go to the demo page and try it, it will say that it could connect to $con. Commented Jul 16, 2013 at 21:33
  • Barmar, it returns nothing. Commented Jul 16, 2013 at 21:34
  • Dave, it is a table. I suppose I could send you a censored screenshot.. Commented Jul 16, 2013 at 21:34

3 Answers 3

1

I use this

$con = StartDatabase("localhost", "username", "password", "mydatabase");

function StartDatabase($dblocation, $dbuser, $dbpasswd, $dbname){
 $link = mysqli_connect($dblocation, $dbuser, $dbpasswd, $dbname);
  if (!$link) {
    die('Connect Error (' . mysqli_connect_errno() . ') '
        . mysqli_connect_error());
  }
 return $link;
}

if(mysqli_query($con, "SELECT * FROM Login")){
    echo "Query good!<br />";
} else {
    echo "Query bad!<br />";
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try adding this to the else of the query to get an error output:

echo "Error code ({$con->errno}): {$con->error}";

2 Comments

Can you post your $con string. Just edit out the info you don't want shown.
$con = mysqli_connect("pdb1.awardspace.com", "**********", "*******", "*******");
0

Try like this:

$con = new mysqli("pdb1.awardspace.com", "*******", "****", "*******");

$query = "SELECT * FROM Login";

if ( !$con->query($query) ) {
    echo "Query bad!<br />";
    echo mysqli_connect_error() . "<br />";
    echo "Error code ({$sql->errno}): {$sql->error}";
} else {
    echo "Query good!<br />";
}
$con->close();

5 Comments

there was an extra backtick(`) in there after login. I have removed it so have an eye for that.
What? I also tried this, and I get a 500 from if (!$con -> query($query))
What do you mean a 500?
Ah never mind. I just decided to switch to something else.. And by 500 I mean PHP server error.
Did you edit the $con string, it is different than your orig.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.