I got the following Mysql command:
// Call on a random style ID to display in rating window which user hasn't seen yet.
$resultSet = $conn->query("SELECT pictureID,userID FROM styles WHERE NOT
viewedByUser = (NOT LIKE '%$userID%') ORDER BY RAND() LIMIT 1");
while($rows = $resultSet->fetch_assoc() ){
$rateableUserID = $rows['userID'];
$rateablePictureID = $rows['pictureID'];
}
I want to use the WHERE NOT function to search through the field "viewedByUser" after a string that does not contain the same string as the variable $userID.
What options have I got here?
Try This :-
$resultSet = $conn->query("SELECT pictureID,userID FROM styles WHERE
viewedByUser NOT LIKE '%$userID%' ORDER BY RAND() LIMIT 1");
while($rows = $resultSet->fetch_assoc() ){
$rateableUserID = $rows['userID'];
$rateablePictureID = $rows['pictureID'];
}
You can directly use != instead of NOT LIKE and there is nothing called WHERE NOT
$resultSet = $conn->query("SELECT pictureID, userID FROM styles WHERE viewedByUser != $userID ORDER BY RAND() LIMIT 1");
Since they are email addresses, you can do:
$userId = 'abc#abc.com xyz#xyz.com';
$userId = explode(' ', $userId);
echo $userStr = implode("', '", $userId);
$resultSet = $conn->query("SELECT pictureID, userID FROM styles WHERE viewedByUser NOT IN ('" . $userStr . "') ORDER BY RAND() LIMIT 1");
If you need a (not) like on the viewedByUser you can simply use
"SELECT pictureID,userID
FROM styles W
WHERE viewedByUser = NOT LIKE '%$userID%'
ORDER BY RAND()
LIMIT 1";
Related
I want to order data by Id, how can i do this ?
if($_GET["grupid"]>0){
$DUZEN = array();
$sql = "SELECT * FROM siparis_ana WHERE grupid =".$_GET["grupid"];
$rsDuzen = mysql_query($sql, $conn) or die(mysql_error());
while ($r = mysql_fetch_assoc($rsDuzen)) {
$DUZEN[] = $r;
}
}
i can read all data with this code which have same group id. But data aline random.
You have to use mysql order clause in your query like order by id asc. Which you can use at the end of your query.
$sql = "SELECT * FROM siparis_ana WHERE grupid =".$_GET["grupid"]." order by id asc";
Your sql query should be like given below...
$sql = "SELECT * FROM siparis_ana where grupid = " . $_GET['grupid'] . " ORDER BY id asc ";
I am trying to display my first 10 users and their information by uid. When I use the code below, it only returns a single uid:
include "db_conx.php";
$sql = ('SELECT uid,username,country FROM users ORDER BY uid DESC LIMIT 10');
$result = mysqli_query($db_conx, $sql);
echo $result->fetch_object()->uid;
What function am I supposed to use to display rows correctly?
include "db_conx.php";
$sql = ('SELECT uid,username,country FROM users ORDER BY uid DESC LIMIT 10');
$result = mysqli_query($db_conx, $sql);
while($var = $result->fetch_object()->uid){
echo $var;
}
Should do the trick
If you get a list of object you should use a loop for show them
include "db_conx.php";
$sql = ('SELECT uid,username,country FROM users ORDER BY uid DESC LIMIT 10');
$result = mysqli_query($db_conx, $sql);
while($row = mysqli_fetch_object($result)) {
$row->uid;
}
I am NOT experienced with php/mysql at all, this is what I have so far.
I want to make a section on my webpage that shows 5 random users, so I have made 5 querys output 1 user each, its working fine, but I have some things I need help adding, I want the username I am currently logged in as excluded, and I want there to be no repeat usernames. My code I have so far is below, help is extremely appreciated.
include "connect.php";
$query = mysql_query("SELECT * FROM users ORDER BY rand() LIMIT 5");
$row = mysql_fetch_assoc($query);
$random = $row['username'];
$query = mysql_query("SELECT * FROM users ORDER BY rand() LIMIT 5");
$row = mysql_fetch_assoc($query);
$random2 = $row['username'];
$query = mysql_query("SELECT * FROM users ORDER BY rand() LIMIT 5");
$row = mysql_fetch_assoc($query);
$random3 = $row['username'];
$query = mysql_query("SELECT * FROM users ORDER BY rand() LIMIT 5");
$row = mysql_fetch_assoc($query);
$random4 = $row['username'];
$query = mysql_query("SELECT * FROM users ORDER BY rand() LIMIT 5");
$row = mysql_fetch_assoc($query);
$random5 = $row['username'];
echo "
<div class='well well-sm' style='padding-top:4px;padding-bottom:8px; margin-bottom:8px; overflow:hidden; background:white;'>
<a href='./$random'>$random</a><br>
<a href='./$random2'>$random2</a><br>
<a href='./$random3'>$random3</a><br>
<a href='./$random4'>$random4</a><br>
<a href='./$random5'>$random5</a>
</div>
</div>
$query = mysqli_query('SELECT * FROM users WHERE username <> "' . mysqli_real_escape_string($connection, $username) . '" ORDER BY rand() LIMIT 5');
while($row = mysqli_fetch_array($query)) {
$random[] = $row['username'];
}
foreach ($random as $random_user) { //echo name of each random user
echo $random_user . '<br />';
}
If your username is stored in $username you can do
$list= array();
$list[]=$username;
$query = mysql_query("SELECT * FROM users WHERE username NOT IN $list ORDER BY rand() LIMIT 5");
Then you can simply add the usernames from each iteration to the list.
I'd also think a loop would be more effective.
I'm a little confused though, Limit 5 indicates you expect 5 results from your query but you pull one from the assoc array.
If your query returns 5 rows(which you can check by running it in phpMyAdmin or whatever SQL control panel you have), then you don't need to keep running it and it shouldn't return duplicates anyway. You just run :
while($row = mysql_fetch_assoc($query)){
$list[]=$row;
}
Then you can use the list to limit further iterations AND to store all of the users you've acquired.
Also, you should note that the mysql extension is deprecated, you want mySQLi or PDO_MySQL for security.
I have 2 database tables. loppe containing username and loppeID, and billeder containing loppeID and billedeNavn.
I would like to have a div containing 2 random billedeNavn from the same username. I have tried some things but I can only figure out how to get 2 billedeNavn from the same loppeID (username). I'm pretty new to all this. So would me grateful for any help.
<?php
$frontimage = ("SELECT * FROM loppe RIGHT JOIN billeder ON loppe.loppeID=billeder.loppeID WHERE loppe.username = '$username' ORDER BY RAND()");
$st = $db->prepare($frontimage);
$st->execute();
$row = $st->fetch();
$loppeid = $row['loppeID'];
$loppe = $loppeid;
$sql = "SELECT * FROM billeder WHERE loppeID = :loppeID order by rand() limit 2";
$stmt = $db->prepare($sql);
$stmt->execute(array("loppeID" => $loppe));
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$fname = $row['billedeNavn'];
echo '<div class="col-xs-6 loppe-pic-outer"><img class="img-responsive loppe-pic" src="loppebilleder/'.$fname.'"></div>';
}
?>
Have you tried this change your
$sql = "SELECT * FROM billeder WHERE loppeID = :loppeID order by rand() limit 2";?
to this
$sql = "SELECT * FROM billeder WHERE loppeID = '.$loppeid.' order by rand() limit 2";
A good way to figure out what is going on when using php and database is after you create your query do something like
$sql = "SELECT * FROM billeder WHERE loppeID = '.$loppeid.' order by rand() limit 2";
die($sql);
So with this you will see what the database is really querying and you can check for in your sql string or query.
I have a DB table. I want to make a text input where the user can input the "uid" and the query will return the row associated with that uid.
So let's say I have something like this:
$query = "SELECT name,age FROM people WHERE uid = '2' LIMIT 0,1";
$result = mysql_query($query);
$res = mysql_fetch_assoc($result);
echo $res["age"];
how would I modify that query to something like..
SELECT name, age
FROM people
WHERE uid = $_POST['blahblah'] LIMIT 0,1
Thanks in advance for your help!
In reality...
// Read input from $_POST
$uid = (isset($_POST['uid']) ? $_POST['uid'] : '');
// Build query. Properly escape input data.
$query =
"SELECT name,age " .
"FROM people " .
"WHERE uid = '" . mysql_real_escape_string($uid) . "' " .
"LIMIT 0,1";
Its advisable to escape characters in the variable for security reasons. Take a look at this document for some of the reasons:
http://en.wikipedia.org/wiki/SQL_injection
To save from SQL injection attack, use:
$search_query = mysql_real_escape_string($_POST['blahblah']);
$query = "SELECT name, age FROM people WHERE uid = '".$search_query."' LIMIT 0 , 1";
There are so many ways to do the same
But first escape it and store it in one variable
$blahblah = mysql_real_escape_string($_POST['blahblah']);
And then There are
First:
As #Mett Lo mentioned:
$query = "SELECT name,age FROM people WHERE uid = '" . $blahblah . "' LIMIT 0,1";
Second:
$query = "SELECT name,age FROM people WHERE uid = '{$blahblah}' LIMIT 0,1";
Third:
$query = "SELECT name,age FROM people WHERE uid = '$blahblah' LIMIT 0,1";
and if blahblah is an int value in db table then Fourth:
$query = "SELECT name,age FROM people WHERE uid = $blahblah LIMIT 0,1";
You may use the sprintf function to create the query.
$query = sprintf("SELECT name,age FROM people WHERE uid = '%s' LIMIT 0,1",
$_POST['blahblah'] );
The rest will be the same. It is highly recommended that you escape the $_POST data before running the query to prevent SQL attacks. You may re phrase the query as follows.
$query = sprintf("SELECT name,age FROM people WHERE uid = '%s' LIMIT 0,1",
mysql_escape_string($_POST['blahblah']) );