<?php
if ($username = $_GET['username'])
{
$sql = "SELECT * FROM posts WHERE username='$username'";
$q = $db->prepare($sql);
$q->execute();
while($q->fetch(PDO::FETCH_ASSOC))
{
echo "$username<br/>";
}
}
?>
Hello everybody. I wanna if username for ex Baran just fetch baran's posts. I have these code and I am getting usernames but when I try the get post date from posts table it does not work. What am I suppose to do.
i think its a typo
if ($username = $_GET['username']) {// assingment
change to:
if ($username == $_GET['username']) {//equal
use preparedStatement to avoid sql injection:
<?php
if ($username == $_GET['username']) {
$sql = "SELECT * FROM posts WHERE username = :user ";
$q = $db->prepare($sql);
$q->bindValue(':user', $username);
$q->execute();
while($row = $q->fetch(PDO::FETCH_ASSOC)) {
echo $row['username'] .'<br/>';
}
}
Are you looking something similar? change if ($username == $_GET['username']) {
while($row = $q->fetchAll(PDO::FETCH_ASSOC))
{
echo $row['username'];
}
You have to assign the result of $q->fetch to some variable. See fetch() documentation.
In your example you just echo the value used as input.
inside if you should always be using ==
eg:if($i ==1)
{
something here
}
else
{
something else
}
Related
guy's i want to ask about selecting with where clause.
the where clause i use $kode=$_POST['kode_mat'] and i call it in sql query into kode='$kode'.
nah the problem is.. when i have a string that need to pass into the sql query is same, like i use $kode but the $_POST['kode_mat'] has different id like kode_mat1,kode_mat2,kode_mat3,kode_mat4,kode_mat5. and i want to pass it into sql query $sql ="SELECT * FROM material WHERE kode='$kode' ";. how to do it?
i have try to separate the php function into 5 php file. and i think that's make my directory has many file that has been saved. i use this code for the 5 php file
<?php
include("../../Connections/koneksi.php");
$kode=$_POST['kode_mat']; // the variable for pass string i just change $_POST['']; ['kode_mat1'],['kode_mat2'],['kode_mat3']....
// Data for Titik1
$sql ="SELECT * FROM material WHERE kode='$kode' "; // and the query still same just change the id's of the textbox that i need to pass the string
$query = mysqli_query($db,$sql);
$rows = array();
while($tmp= mysqli_fetch_assoc($query)) {
$rows[] = $tmp;
}
echo json_encode($rows);
mysqli_close($db);
?>
this code above has no problem. it show the right value that i need. but i want to try it to make my separate php fuction can be called as one php.
i has to try two code like this but it not work.
1st code i have try
<?php
include("../../Connections/koneksi.php");
$kode=$_POST['kode_mat'];
$kode=$_POST['kode_mat1'];
$kode=$_POST['kode_mat2'];
$kode=$_POST['kode_mat3'];
$kode=$_POST['kode_mat4'];
$kode=$_POST['kode_mat5'];
// Data for Titik1
$sql ="SELECT * FROM material WHERE kode='$kode' ";
$query = mysqli_query($db,$sql);
$rows = array();
while($tmp= mysqli_fetch_assoc($query)) {
$rows[] = $tmp;
}
echo json_encode($rows);
mysqli_close($db);
?>
and the second code i have try
<?php
include("../../Connections/koneksi.php");
$kode=$_POST['kode_mat'];
$kode1=$_POST['kode_mat1'];
$kode2=$_POST['kode_mat2'];
$kode3=$_POST['kode_mat3'];
$kode4=$_POST['kode_mat4'];
$kode5=$_POST['kode_mat5'];
// Data for Titik1
$sql ="SELECT * FROM material WHERE kode='$kode' OR kode='$kode1' OR kode='$kode2' OR kode='$kode3' OR kode='$kode4' OR kode='$kode5'";
$query = mysqli_query($db,$sql);
$rows = array();
while($tmp= mysqli_fetch_assoc($query)) {
$rows[] = $tmp;
}
echo json_encode($rows);
mysqli_close($db);
?>
You can use if and else condition here for your solution like below.
<?php
include("../../Connections/koneksi.php");
if(isset($_POST['kode_mat']) && $_POST['kode_mat'] != "") {
$kode=$_POST['kode_mat'];
} else if(isset($_POST['kode_mat1']) && $_POST['kode_mat] != "") {
$kode=$_POST['kode_mat1'];
} else if(isset($_POST['kode_mat2']) && $_POST['kode_mat2'] != "") {
$kode=$_POST['kode_mat2'];
} else if(isset($_POST['kode_mat3']) && $_POST['kode_mat3'] != "") {
$kode=$_POST['kode_mat3'];
} else if(isset($_POST['kode_mat4']) && $_POST['kode_mat4'] != "") {
$kode4=$_POST['kode_mat4'];
} else {
$kode=$_POST['kode_mat5'];
}
// Data for Titik1
$sql ="SELECT * FROM material WHERE kode='$kode' ";
$query = mysqli_query($db,$sql);
$rows = array();
while($tmp= mysqli_fetch_assoc($query)) {
$rows[] = $tmp;
}
echo json_encode($rows);
mysqli_close($db);
?>
I have only just started writing the PHP script to power the backend of my android app. What i'm currently trying to do is run a PHP script that goes into my database called send_friendreq and the table called pending_req and gets the row toUser and adds it to an array. The only problem i'm currently having is the fact that I cannot get the PHP script to run correctly. Any help would be appreciated. Here is my code for the PHP script that I currently have. Thank you very much for the help!
if (isset($_POST['Username']) && isset($_POST['FriendReq']))
{
$username = $_POST['Username'];
$usernamebeingreq = $_POST['FriendReq'];
$i=0;
//$sqlCheck = "SELECT Username FROM Users WHERE Username = '" . $usernamebeingreq . "'";
//$resultCheck = mysqli_query($con, $sqlCheck);
//if(!$resultCheck)
//{
//echo "Invalid Username";
//}
//else
//{
$sql="SELECT fromUser FROM pending_req WHERE toUser='&username'";
$result = mysqli_query($con, $sql);
$array = array();
while ($row = pg_fetch_array($result))
{
$i++;
}
for($x=0;$x<$i;$x++)
{
echo $array[$x];
}
if(!$result)
{
echo 'Failed';
}
else
{
echo json_encode($array[$x]);
echo "<br>";
}
If you have suggestions on something that would work better / more efficiently / safer, please let me know!
If I am not wrong, you are trying to get json of results from your query. try this code.
if (isset($_POST['Username']) && isset($_POST['FriendReq']))
{
$username = $_POST['Username'];
$usernamebeingreq = $_POST['FriendReq'];
$i=0;
//$sqlCheck = "SELECT Username FROM Users WHERE Username = '" . $usernamebeingreq . "'";
//$resultCheck = mysqli_query($con, $sqlCheck);
//if(!$resultCheck)
//{
//echo "Invalid Username";
//}
//else
//{
$sql = 'SELECT fromUser FROM pending_req WHERE toUser='. $username ;
$result = mysqli_query($con, $sql);
if(!$result) {
echo 'Failed';
}elseif($result){
$myArray = array();
while($row = $result->fetch_array(MYSQL_ASSOC)) {
$myArray[] = $row;
}
echo json_encode($myArray);
}
you query syntax seems to be not correct plz modify as :
$sql="SELECT fromUser FROM pending_req WHERE toUser='$username'";
In fact I am working on a small PHP script but I am facing a problem right now.The problem is that i want to check if my query return records this is my mysqli query:
$sql = "select * from specs where btitleid=$id and phoneid=$aydi"
$check = $conn->query($sql)
while($row = $check->fetch_assoc()) {$tocheck = $row['content'];}
I don't want to check the number of rows of this query to see if it is null.I want to check if all $row['content'] are empty.
How about this:
$sql = "select * from specs where btitleid=$id and phoneid=$aydi";
$check = $conn->query($sql);
$contentAllEmpty = true;
while ($row = $check->fetch_assoc()) {
if (strlen($row['content']) > 0) {
$contentAllEmpty = false;
}
}
if ($contentAllEmpty) {
//do something
}
use ==
while ($row = $check->fetch_assoc()) {
if ($row['content'] == '') {
... code here
}
}
To get back only records where the content column is not empty -
$sql = "SELECT * FROM `specs` WHERE `btitleid` = $id AND `phoneid` = $aydi AND (`content` IS NOT NULL OR `content` != '') ";
I've got a php script with this statement:
$query = "SELECT type
FROM users
WHERE username = '$username'";
$result = $database->query($query);
if($result == 1) {
echo "whatever";
continue;
}
The problem is that the if never runs and when I created a print statement to print $result before the if runs, it prints a Reference ID#. So result is never == 1 because it is being assigned the reference ID #.
What the heck am I doing wrong? How do I assign the value of 'type' which is an INT, instead of it's contents Reference ID#?
you have to fetch that line first ...
$query = "SELECT type FROM users WHERE username = '$username'";
$result = $database->query($query);
$row = mysql_fetch_assoc($result);
if($row['type'] == 1)
{
echo "whatever";
}
You need to use mysql_fetch_assoc() or mysql_fetch_array() to get the result into an array:
$query = $database->query("SELECT type FROM users WHERE username = '$username'");
$result = mysql_fetch_assoc($query);
if($result['type'] == 1)
{
echo "whatever";
continue;
}
try
<?php
$result = mysql_fetch_assoc($database->query($query));
$id = $result['type'];
if($type == 1)
{
Yes. You have to check it like:
$query = "SELECT type FROM users WHERE username = '$username'";
$result = $database->query($query);
if($result)
{
echo "whatever";
continue;
}
This is a Boolean check, but... In php we have weak types - everything is true, except on false, 0, '' (empty string), null, etc.
If you want to make a good Boolean check including type, then you have:
if(bool_val === true)
{
..
}
// or
if(bool_val && is_bool(bool_val))
{
..
}
This query object returns a result resource in php. And you probably receive #Resource ID, which is some kind of pointer in php code.
If you receive something like this (#Resource ID) - this means your query has passed correctly and no error occured.
if(isset($_POST['uname']))
{
$query = "SELECT * FROM user_info";
$res = $mysqli->query($query);
$num = $res->num_rows;
$i = 0;
$cpwd = $_POST["pswd"];
$hpwd = SHA1($cpwd);
$tmp = $_POST["uname"];
while($i < $num)
{
$row = $res->fetch_object();
$name = $row->Username;
$pwd = $row->Password;
if($name == $tmp)
{
//check if user is blocked
}
//hashed pwd
if($pwd == $hpwd)
{
//success
exit();
}
//request for pwd change
else if($pwd == $cpwd)
{
//change
exit();
}
else if($pwd != $hpwd)
{
//incorrect pwd
}
}
$i++;
}
if($i == $num)
{
//new user
}
}
I'd guess that you're somehow looping past the end of the array and $row is actually NULL.
So $res->fetch_object() did not return an object. Take a look at the documentation of this function. What does it return when it finds nothing?
some times num_rows return 1, even if no rows effected. Try to use
while($row = $res->fetch_object())
or
you forget to increment $i :)
get rid of that junk and make it like this
$query = "SELECT * FROM user_info WHERE Username = ? AND Password = ?";
$stmt = $mysqli->prepare($query);
$stmt->bind_param('ss', $_POST["uname"], SHA1($_POST["pswd"]));
$stmt->execute() or trigger_error($mysqli->error());
if (!$mysqli->affected_rows) {
//no such user
}
I've never used mysqli myself, so, there may be typos.
But I hope you'll be able to get the idea.