php select query help

willy ze great

JF-Expert Member
Apr 30, 2013
1,066
1,293
database hairetrieve data za mtu anayetaka kuona vitu alivyonunua kutoka table ya manunuzi
lakini nikiedit hapo "where username='john'" inaretrieve data zote za john napata but now napata Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result
<?php
$host="localhost";
$user="root";
$password="";
$database="online";
$username=$_SESSION['username'];
$link= mysqli_connect($host, $user, $password,$database);
$query="select * from manunuzi where username=$username order by date desc";
$result= mysqli_query($link, $query);
echo "<table border='1' bgcolor='WHITE'>";
echo "<caption>YOUR BIDDING TABLE</caption>";
echo "<tr><th>PRODUCT</th><th>DATE</th>
<th>QUANTITY</th><th>PRICE</th><th>METHOD</th></tr>";
while ($row =mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>".$row['product']."</td>";
echo "<td>".$row['date']."</td>";
echo "<td>".$row['qty']."</td>";
echo "<td>".$row['price']."</td>";
echo "<td>".$row['method']."</td>";
echo "</tr>";
}
?>
 
Hapo kwenye $query weka username = '$username'. i.e add quotes kwenye hiyo variable username
 
database hairetrieve data za mtu anayetaka kuona vitu alivyonunua kutoka table ya manunuzi
lakini nikiedit hapo "where username='john'" inaretrieve data zote za john napata but now napata Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result
<?php
$host="localhost";
$user="root";
$password="";
$database="online";
$username=$_SESSION['username'];
$link= mysqli_connect($host, $user, $password,$database);

/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}


$query="select * from manunuzi where username=$username order by date desc";
$result= mysqli_query($link, $query);


if($result===FALSE){
printf("Query: " . $query);
printf(" Failed");
exit();
}


echo "<table border='1' bgcolor='WHITE'>";
echo "<caption>YOUR BIDDING TABLE</caption>";
echo "<tr><th>PRODUCT</th><th>DATE</th>
<th>QUANTITY</th><th>PRICE</th><th>METHOD</th></tr>";
while ($row =mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>".$row['product']."</td>";
echo "<td>".$row['date']."</td>";
echo "<td>".$row['qty']."</td>";
echo "<td>".$row['price']."</td>";
echo "<td>".$row['method']."</td>";
echo "</tr>";
}
?>

Validate results zako kabla haujazitumia, utajua tatizo liko wapi. (Nimeedit code hapo cheki maandishi mekundu.)

Pia jaribu kusetup debugger ambayo itakuwezesha kupitia code yako step by step wakati inaexecute na kuangalia values zako cheki Xdebug youtube.

Mwisho unavyounga username kwenye SQL query hizo kuna hatari ya kuwa na security issue ya SQL Injection.
How can I prevent SQL-injection in PHP?
 
database hairetrieve data za mtu anayetaka kuona vitu alivyonunua kutoka table ya manunuzi
lakini nikiedit hapo "where username='john'" inaretrieve data zote za john napata but now napata Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result
<?php
$host="localhost";
$user="root";
$password="";
$database="online";
$username=$_SESSION['username'];
$link= mysqli_connect($host, $user, $password,$database);
$query="select * from manunuzi where username=$username order by date desc";
$result= mysqli_query($link, $query);
echo "<table border='1' bgcolor='WHITE'>";
echo "<caption>YOUR BIDDING TABLE</caption>";
echo "<tr><th>PRODUCT</th><th>DATE</th>
<th>QUANTITY</th><th>PRICE</th><th>METHOD</th></tr>";
while ($row =mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>".$row['product']."</td>";
echo "<td>".$row['date']."</td>";
echo "<td>".$row['qty']."</td>";
echo "<td>".$row['price']."</td>";
echo "<td>".$row['method']."</td>";
echo "</tr>";
}
?>

jaribu kutumia OOP way

$host="localhost";
$user="root";
$password="";
$database="online";
$username=$_SESSION['username'];
$link= new mysqli($host, $user, $password,$database);


$query= $link->query("select * from manunuzi where username=$username order by date desc");
if($query->num_rows > 0){
while ($row = $query->fetch_array($result)) {
echo "<tr>";
echo "<td>".$row['product']."</td>";
echo "<td>".$row['date']."</td>";
echo "<td>".$row['qty']."</td>";
echo "<td>".$row['price']."</td>";
echo "<td>".$row['method']."</td>";
echo "</tr>";
}
}
 
database hairetrieve data za mtu anayetaka kuona vitu alivyonunua kutoka table ya manunuzi
lakini nikiedit hapo "where username='john'" inaretrieve data zote za john napata but now napata Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result
<?php
$host="localhost";
$user="root";
$password="";
$database="online";
$username=$_SESSION['username'];
$link= mysqli_connect($host, $user, $password,$database);
$query="select * from manunuzi where username=$username order by date desc";
$result= mysqli_query($link, $query);
echo "<table border='1' bgcolor='WHITE'>";
echo "<caption>YOUR BIDDING TABLE</caption>";
echo "<tr><th>PRODUCT</th><th>DATE</th>
<th>QUANTITY</th><th>PRICE</th><th>METHOD</th></tr>";
while ($row =mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>".$row['product']."</td>";
echo "<td>".$row['date']."</td>";
echo "<td>".$row['qty']."</td>";
echo "<td>".$row['price']."</td>";
echo "<td>".$row['method']."</td>";
echo "</tr>";
}
?>
pia jaribu kuweka quotation mark katika variable ya $username it will work
 

Similar Discussions

Back
Top Bottom