0

I have this PHP (testing):

<?php
if (isset($_POST['submit'])) {

  $link = mysqli_connect("localhost","php","","A");

  $query = "SELECT passport FROM users WHERE mail = '$_POST[mail]';";

  $result = $link->query($query);

}
?>

A manual SELECT using the same $query shows:

mysql> SELECT passport FROM users WHERE mail = '[email protected]';
+----------------------+
| passport             |
+----------------------+
| 8KQSWCZPJAUV1M9D4TFG |
+----------------------+
1 row in set (0.00 sec)

I am trying to echo the $result in PHP which should show 8KQSWCZPJAUV1M9D4TFG but it is null. What am I doing wrong here?

Update:

<?php
if (isset($_POST['submit'])) {

  $link = mysqli_connect("localhost","php","","A");

  $query = "SELECT passport FROM users WHERE mail = '$_POST[mail]';";

  $result = mysqli_query($query);

  $row    = mysqli_fetch_assoc($result);

  echo $row["passport"];

}
?>

This also is still null.

2
  • php.net/manual/en/mysqli-stmt.fetch.php Commented Jan 14, 2014 at 10:09
  • you can remove ; from the end of query string if you want, and on the next line echo $query, to make sure what query exactly is generated. Commented Jan 14, 2014 at 10:17

4 Answers 4

1

Just try with:

$result = mysqli_query($query);
$row    = mysqli_fetch_assoc($result);
Sign up to request clarification or add additional context in comments.

2 Comments

And output it echo $row["passport"];
Thanks! I gave that a go and updated this question with what I'm using, but it's still not giving me the result that the manual SELECT does?
1
$link = new mysqli("localhost", "php", "", "A");

$query = "SELECT passport FROM users WHERE mail = '$_POST[mail]';";
$result = $link->query($query);

while($row = $result->fetch_array())
{
  echo $row['passport'];
}

You need to use object based MySQLi in order to use $link->query()

Comments

0

Check this one.

It should work for you.

$mysqli = new mysqli('localhost', 'root', 'pass123', 'test');
$query = "SELECT passport FROM users WHERE mail = '$_POST[mail]'";

$result = $mysqli->query($query);

// PRINT
if($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo stripslashes($row['passport']);    
    }
}

Comments

0

You need to fecth in loop and fix query(remove ;) like this:

<?php
if (isset($_POST['submit'])) {

  $link = mysqli_connect("localhost","php","","A");

  $query = "SELECT passport FROM users WHERE mail = '$_POST[mail]'";

  $result = mysqli_query($query);

  while( $row = mysqli_fetch_assoc($result)){

     echo $row["passport"];
    }

}
?>

1 Comment

@user2656114 : did you try this?

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.