How to Fetch Data in PHP with MySQLi

How to fetch data in PHP with MySQLi?

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;
}

Fetch data from database using MySQLi

After your comment yeah ...first error is gone..thanks:) still you have some issue with your code .

1st : $result[] = $record; you need to push the $row in array instead of $record

 $result[] = $row; 

2nd : Function will return the array you need to loop it to get the each row like this

3rd : Remove $ sign from array index

<?php
$myrow=$obj->select_data_from_db("home_slider");
?>

<?php if(count($myrow)>0){
foreach($myrow as $row){ ?>
<tr>
<td><?php echo $row['id']; ?> </td>
<td><?php echo $row['title']; ?> </td>
<td><?php echo $row['description']; ?> </td>
</tr>
<?php } }

?>

4th : use prepared statement to avoid sql injection

public function select_data_from_db($table_name,$conn)
{

$stmt=$conn->prepare("SELECT * FROM ".$table_name);
$stmt->execute();
$result = $stmt->get_result();
$total_count = $result->num_rows;
$result = array();
if($total_count>0)
{
while ($row = mysql_fetch_array($query)) {
$result[] = $row;
}
}

return $result;
}

Warning mysql_query, mysql_fetch_array,mysql_connect etc.. extensions
were deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0.
Instead, the MySQLi or PDO_MySQL extension should be used.

How to fetch a single row from a MySQL DB using MySQLi with PHP?

Add LIMIT 1 to the end of your query to produce a single row of data.

Your method is vulnerable to SQL injection. Use prepared statements to avoid this. Here are some links you can review:

  • What is SQL injection?

  • https://en.wikipedia.org/wiki/SQL_injection

  • https://phpdelusions.net/mysqli_examples/prepared_select



<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset("utf8mb4");

$txnid= $_GET['name_of_txnid_input_field'];

// prepare and bind
$stmt = $conn->prepare("SELECT * FROM `testtable1` WHERE `txnid` = ? LIMIT 1");
$stmt->bind_param("i", $txnid);

// set parameters and execute
$stmt->execute();

$result = $stmt->get_result();
$row = $result->fetch_assoc();
echo $row['date_field_you_want_to_display'];

Retrieve data into a variable with php and mysqli

You need to fetch the results:

$row = $result->fetch_assoc();
$id = $row['id'];
echo $id;

fetch_assoc() returns the next row of results as an associative array.

You can then use the $id variable in your INSERT query.

There's no need to use two queries to insert this into another table, you can do that with one query.

$stmt = $conec->prepare("
INSERT INTO entrada_produtos (fk_id, usuario, data_inclusao, qtd)
SELECT id, ?, now(), ?
FROM produtos
where tipo = 'Tubo'
and inteiro_pedaco = ?
and marca = ?
and comprimento = ?
and diaexterno = ?
and diainterno = ?");
$stmt->bind_param("sssssss", $usuario, $qtd, $tipo, $marca, $comprimento, $externo, $interno);
$stmt->execute();

I've rewritten this as a prepared statement to prevent SQL injection. The ? in the query are placeholders, which are filled in with the variable values given in the call to bind_param().

BTW, if you're selecting the row that you just inserted into produtos, you can use the MySQL function LAST_INSERT_ID() or the PHP variable $conec->insert_id to get the auto-increment ID that was assigned, you don't need a query for that.

How to fetch MySQLi data order by array values?

Easiest way to do what you want is to the order it within your SQL

$sql = "SELECT ID, number FROM table WHERE ID IN ({$sorted_ids}) ORDER BY FIELD(id, {$sorted_ids})";

Should do the trick

PHP Mysqli. Need to fetch data from table

Okay i got it!! My query is wrong.

SELECT a.id, a.telco, a.no_siri, a.no_topup, a.amount, a.requestingAgentID, a.requestDateTime, a.isUsed, b.name FROM card_telco a LEFT JOIN agents b ON a.requestingAgentID = b.id WHERE a.id = ?

This is my new query. Thanks for helping me

select count(*) from table of mysql in php

You need to alias the aggregate using the as keyword in order to call it from mysql_fetch_assoc

$result=mysql_query("SELECT count(*) as total from Students");
$data=mysql_fetch_assoc($result);
echo $data['total'];


Related Topics



Leave a reply



Submit