简体   繁体   中英

How to fetch data in PHP with MySQLi?

I tried several times but cannot succeed in getting the right syntax—according to PHP 5.5.12 —to fetch single or multiple rows from my database.

session_start();
$con=mysqli_connect("localhost","root","","doortolearn");
if (!$con) {
    echo "Could not connect to DBMS";       
}
    $query="select * from teacher where tremail='$_POST[email]' and trpasssword='$_POST[password]'";
    $result=mysqli_query($con,$query);
    $flag=FALSE;
    while ($row=mysqli_fetch_array($result,MYSQLI_BOTH)) {
        $_SESSION['email']=$row['email'];
        $flag=TRUE;
    }

First, you have no single quotes ' around $_POST[password] :

$query = "SELECT * FROM teacher WHERE tremail='". $_POST['email'] ."' and trpasssword='" . $_POST['password'] . "'";
$result = mysqli_query($con, $query) or die(mysqli_error($con));
$flag = FALSE;
while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {
    $_SESSION['email'] = $row['email'];
    $flag = TRUE;
}

But past that, do you even have a MySQL database connection set here? I see $con but is that really working?

Also, check if there are errors by adding or die(mysql_error($con)) to your mysqli_query($con, $query) line.

Also, you have a $_SESSION value, but do you even set session_start at the beginning of your script?

But I also recommend you use mysqli_stmt_bind_param for your values to at least escape them if you are not going to do basic validation:

$query = "SELECT * FROM teacher WHERE tremail=? and trpasssword=?";
mysqli_stmt_bind_param($query, 'ss', $_POST['email'], $_POST['password']);
$result = mysqli_query($con, $query) or die(mysqli_error($con));
$flag = FALSE;
while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {
    $_SESSION['email'] = $row['email'];
    $flag = TRUE;
}

To successfully fetch data from MySQL using mysqli extension in PHP you need to perform more or less three actions: connect, execute prepared statement, fetch data.

Connection:

The connection is really simple. There should always be only 3 lines of code for opening a connection. You enable error reporting, create new instance of mysqli, and set the correct charset.

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli('localhost', 'user', 'pass', 'db_name');
$mysqli->set_charset('utf8mb4');

Prepared statement

This is the tricky part. You need to prepare SQL statement to be executed. Careful, never concatenate PHP variables into SQL directly . Bind the variables using placeholders. Once the statement is ready you can execute it on the server.

$stmt = $mysqli->prepare('SELECT * FROM teacher WHERE tremail=?');
$stmt->bind_param('s', $_POST['email']);
$stmt->execute();

Fetch the data

If your prepared statement should return some results, you need to fetch them and do something with the records. To fetch the result use get_result() . This will give you an object that you can iterate on to fetch each row one by one.

$result = $stmt->get_result();
foreach ($result as $row) {
    echo $row['user_id'];
}

If you are only starting learning PHP, please consider learning PDO instead. It is easier to use and offers more functionality. Use mysqli only for legacy projects.

can You try this code


<?php $query=mysqli_query($connection, "SELECT * FROM user");
while($rows=mysqli_fetch_array($query)){ ?>
<tr>
<td><?php echo $rows['name']; ?></td>
<td><?php echo $rows['age']; ?></td>
<td><?php echo $rows['mobile']; ?></td>
<td><?php echo $rows['email']; ?></td>
</tr>
<?php } ?>
$r =$mysqli->query("select * from users");

while ( $row =  $r->fetch_assoc() )
{
?>
  <tr>
  <td><?php echo $i++; ?></td>
  <td><?php echo $row['name']; ?></td>
  <td><?php echo $row['pwd']; ?></td>
  </tr>
  <?php
  }
  ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM