I want to get an id from browser and display some pictures from the database.
If there is no "display2.php?productid=" found, then I want to display default image.
How can I do that?
Here is my code;
$sql = "SELECT * FROM productlist where productid=".$_GET['productid'];
$result = $mysqli->query($sql);
while($myRow = $result->fetch_array())
{
if(null !==($_GET['productid']==$myRow["productid"])){
echo "<img src=".$myRow["productid"].">";
}
else {
echo "<img src="SELECT productimage FROM productlist where productid = 1;">";
}
}
Now I will make it easier to explain for you...
Check this out;
//This part works without any problem
$sql = "SELECT * FROM productlista where productid=".$_GET['productid'];
$result = $mysqli->query($restwo);
while($myRow = $resulttwo->fetch_array())
{
if(null !==($_GET['productid']==$myRow["productid"])){
echo "<img src=".$myRow["productimage"].">";
}
//This part below (that should be default) does not work...
if (!$_GET){
echo "hello world"; }
Asaph pointed out SQL injection. You should bind the parameter (google it), or at the minimum do this:
$defaultImage = "SELECT * FROM productlist WHERE imageSrc != '' OR IS NOT NULL ORDER BY productid DESC LIMIT 1";
// run the query, get the result, create a variable with default image...
$defaultImageSrc = ''; // what you get from the query result
$_GET['productid'] = preg_replace('#[^0-9]#', '', $_GET['productid']);
$sql = "SELECT * FROM productlist where productid=".$_GET['productid'];
$result = $mysqli->query($sql);
while($myRow = $result->fetch_array()) {
if(!$myRow['imageSrc']) $myRow['imageSrc'] = $defaultImageSrc;
echo '<img src="'.$path.'">';
}
If you want either $_GET['productid'] or the max(productid) when $_GET['productid'] is not set, you can use a ternary to change your sql query
$productid = ! empty($_GET['productid']) ? " WHERE productid = ".(int)$_GET['productid'] : " ORDER BY productid DESC LIMIT 1";
$sql = "SELECT * FROM productlist".$productid
$result = $mysqli->query($sql);
while($myRow = $result->fetch_array())
{
echo "<img src=".$myRow["productimage"].">";
}
so if isset($_GET['productid']) your query would be
SELECT * FROM productlist WHERE productid = (int)$_GET['productid']
but if not the default would be
SELECT * FROM productlist ORDER BY productid DESC LIMIT 1
Related
I have the following code
$sql = "SET #uid := (SELECT ID FROM channels WHERE Used = 0 ORDER BY RAND() LIMIT 1);";
$sql = "UPDATE channels SET Used = 1 WHERE ID = #uid;";
$sql = "SELECT * FROM channels WHERE ID IN = #uid;";
$result = mysqli_multi_query($conn, $sql)
or die( mysqli_error($sql) );
if (mysqli_num_rows($result) > 0) {
$text = '';
while($row = mysqli_fetch_assoc($result)) {
$Channel_Location = $row['Channel_Location'];
$text = $text . $Channel_Location;
}
}
Now the issue i'm having is the php isnt displaying the result returned by the MYSQL query which is stored in a session later on in the code to be displayed on a dummy page it comes up with the following error
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result
The my SQL query does exactly what I need it to I just need to, so I don't really want to change it. I just need some advice on how i'd get the PHP to echo the #uid
is there anyone willing to help me solve the issue? if so thankyou.
You have 3 queries in your $sql so you should use multi_query function
http://php.net/manual/en/mysqli.multi-query.php
And you can change your first query to:
SET #uid = 0;
SELECT #uid := ID FROM channels WHERE Used = 0 ORDER BY RAND() LIMIT 1);
Update You can try this fragment of your code modified with all commented improvements.
$sql = 'SET #uid = 0;';
$sql .= 'SELECT #uid:= ID FROM channels WHERE Used = 0 ORDER BY RAND() LIMIT 1);';
$sql .= 'UPDATE channels SET Used = 1 WHERE ID = #uid;';
$sql .= 'SELECT * FROM channels WHERE ID IN = #uid;';
if (mysqli_multi_query($conn, $sql)) {
do {
$result = mysqli_store_result($conn);
} while(mysqli_next_result($conn));
if (mysqli_num_rows($result) > 0) {
$text = '';
while($row = mysqli_fetch_assoc($result)) {
$Channel_Location = $row['Channel_Location'];
$text = $text . $Channel_Location;
}
}
} else {
die( mysqli_error($conn) );
}
I have a table and i want to echo the 2 last rows of my tabel, I used the below code but just the last one showed, what is the problem.
$result1 =(mysql_fetch_array(mysql_query("SELECT * FROM $table ORDER BY id DESC LIMIT 2")));
Print $result1['time'];
mysql_fetch_array = 1 fetch.
do it again for fetching 2nd result.
Also, use mysqli eh.
You're doing mysql_fetch_array only one time, so it gets the first element. If you want to get all the elements, then do it again, or use a loop.
Something like:
$query = "SELECT * FROM $table ORDER BY id DESC LIMIT 2";
while($row = mysql_fetch_array(mysql_query($query) )
{
echo $row['time'].'<br>';
}
For 2 Or more rows you need to loop it
$sql = mysql_query("SELECT * FROM $table ORDER BY id DESC LIMIT 2")
while($row=mysql_fetch_array($sql))
{
echo $row['time']."<br>";
}
$query = mysqli_query("SELECT * FROM $table ORDER BY id DESC LIMIT 2");
while ($result = mysqli_fetch_array($query)) {
echo $result['time'];
}
Gives out every return of your database (2 in this case). You should use mysqli_-functions.
Maybe you should try like this, since mysql_fetch_array returns only one row
while ($row = mysql_fetch_array($yourQuery)) {
echo $row["yourAlias"];
}
Further details here : http://fr2.php.net/manual/en/function.mysql-fetch-array.php
My solution:
$limit = 2;
$sql = "SELECT * FROM $table ORDER BY id DESC LIMIT $limit";
$result = mysql_query($sql);
$array = array(); $i = 0;
while($row = mysqli_fetch_array($result))
{
$array[$i] = $row;
$i++;
}
var_dump($array[0]);
var_dump($array[1]);
I have a query that gets 5 lines of data like this example below
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
}
I want to run a query inside each results like this below
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
$query = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) )
{
$title = $row['title'];
} else {
$title = "No Title";
}
echo "$ref - $tile";
}
but for some reason it's only display the first line when I add the query inside it. I can seem to make it run all 5 queries.
SELECT
t1.ref,
t1.user,
t1.id,
t2.domain,
t2.title
FROM
table AS t1
LEFT JOIN anothertable AS t2 ON
t2.domain = t1.ref
LIMIT
0, 5
The problem is that inside the while-cycle you use the same variable $result, which then gets overridden. Use another variable name for the $result in the while cycle.
You change the value of your $query in your while loop.
Change the variable name to something different.
Ex:
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
$qry = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
$rslt = mysql_query($qry) or die(mysql_error());
if (mysql_num_rows($rslt) )
{
$title = $row['title'];
} else {
$title = "No Title";
}
echo "$ref - $tile";
}
Use the following :
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
$query_domain = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
$result_domain = mysql_query($query_domain) or die(mysql_error());
if (mysql_num_rows($result_domain) )
{
$row_domain = mysql_fetch_row($result_domain);
$title = $row_domain['title'];
} else {
$title = "No Title";
}
echo "$ref - $title";
}
This is a logical problem. It happens that way, because you are same variable names outside and inside the loop.
Explanation:
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
// Now $results hold the result of the first query
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
//Using same $query does not affect that much
$query = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
//But here you are overriding the previous result set of first query with a new result set
$result = mysql_query($query) or die(mysql_error());
//^ Due to this, next time the loop continues, the $result on whose basis it would loop will already be modified
//..............
Solution 1:
Avoid using same variable names for inner result set
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
$query = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
$sub_result = mysql_query($query) or die(mysql_error());
// ^ Change this variable so that it does not overrides previous result set
Solution 2:
Avoid the double query situation. Use joins to get the data in one query call. (Note: You should always try to optimize your query so that you will minimize the number of your queries on the server.)
SELECT
ref,user,id
FROM
table t
INNER JOIN
anothertable t2 on t.ref t2.domain
LIMIT 0, 5
Learn about SQL joins:
SELECT table.ref, table.user, table.id, anothertable.title
FROM table LEFT JOIN anothertable ON anothertable.domain = table.ref
LIMIT 5
You're changing the value of $result in your loop. Change your second query to use a different variable.
it is not give proper result because you have used same name twice, use different name like this edit.
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
$query1 = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
$result1 = mysql_query($query1) or die(mysql_error());
if (mysql_num_rows($result1) )
{
$title = $row['title'];
} else {
$title = "No Title";
}
echo "$ref - $tile";
}
Im trying to get this next/previous image thing to work. But i don't know how to get this right. I only get the id 88 and 87, not the ones before that.
www.wallpapers.puffys.net
In my index.php page i got this code to show the image and get the next/previous image.
include 'db_connect.php';
#Last
$sql = "SELECT id FROM images WHERE validated=1 ORDER BY id DESC LIMIT 0,1";
$result = mysql_query($sql) or trigger_error(mysql_error()."".$sql);
$row = mysql_fetch_array($result);
$last = $row['id'];
#Next
$sql = "SELECT id FROM images WHERE validated=1 AND id>'".$last."' ORDER BY id DESC LIMIT 0,1";
$result = mysql_query($sql) or trigger_error(mysql_error()."".$sql);
$row = mysql_fetch_array($result);
$next = $row['id'];
#Previous
$sql = "SELECT id FROM images WHERE validated=1 AND id<'".$last."' ORDER BY id DESC LIMIT 0,1";
$result = mysql_query($sql) or trigger_error(mysql_error()."".$sql);
$row = mysql_fetch_array($result);
$previous = $row['id'];
?>
<!--Scripts-->
<script type="text/javascript" src="resources/jquery-1.7.1.js"></script>
<script>
$(document).ready(function() {
function showLoader() {
$('.loader').fadeIn(200);
}
function hideLoader() {
$('.loader').fadeOut(200);
}
$(".next").click(function() {
showLoader();
$("#wallpaper").load("get.php?id=<? echo $next; ?>", hideLoader);
return false;
});
$(".previous").click(function() {
showLoader();
$("#wallpaper").load("get.php?id=<? echo $previous; ?>", hideLoader);
return false;
});
showLoader();
$("#wallpaper").load("get.php?id=<? echo $last; ?>", hideLoader);
});
</script>
The get.php page:
include 'db_connect.php';
$id = mysql_real_escape_string($_GET['id']);
if ($id) {
$sql = "SELECT COUNT(*) FROM images WHERE id='".$id."' AND validated=1";
$result = mysql_query($sql) or trigger_error(mysql_error()."".$sql);
$num_rows = mysql_fetch_row($result);
if ($num_rows[0] == 1) {
$sql = "SELECT image FROM images WHERE id='".$id."' AND validated=1";
$result = mysql_query($sql) or trigger_error(mysql_error()."".$sql);
$row = mysql_fetch_array($result);
echo '
<img src="'.$row['image'].'" width="1024" height="576" alt="wallpaper" border="0">
';
}
else {
}
}
else {
}
?>
I know i am doing this wrong, but i don't know how to get this working. So i would hope someone could help me out.
Your problem is most likely this;
#Next
$sql = "SELECT id FROM images WHERE validated=1 AND id>'".$last.
"' ORDER BY id DESC LIMIT 0,1";
You get all IDs greater than the last, and order from highest to lowest which will give you the highest id available. You'll want to change it around to;
#Next
$sql = "SELECT id FROM images WHERE validated=1 AND id>'".$last.
"' ORDER BY id ASC LIMIT 0,1";
which will sort in the correct order and give you the lowest ID that is higher than the last.
This is my solution:
$NEW_ID = 0;
$SQL_FUNCTION = "SELECT ID FROM TableName";
$SQL_DATA = MYSQL_QUERY($SQL_FUNCTION);
WHILE($SQLDAT = MYSQL_FETCH_OBJECT($SQL_DATA)){
$ID = "$SQLDAT->ID";
if($NEW_ID < $ID){$NEW_ID = $ID;}
}
//NOW INSERT WITH THIS ID...
But I'm not sure if it works when more people simultaneously do this!?
(I would ask this question but i'm closed)
I have the following code:
$query = mysql_query("SELECT * FROM activity ORDER BY activity_time DESC LIMIT 50");
while($result = mysql_fetch_array($query)) {
extract($result);
if ($activity_type == "discussion") {
$query = mysql_query("SELECT * FROM discussions WHERE discussion_uuid = '$activity_ref'");
while($result = mysql_fetch_array($query)) {
extract($result);
echo $discussion_user . " said:<br>" . $discussion_text . "<br>";
}
} elseif ($activity_type == "file") {
}
}
But it just returns the last row. My goal is to have a chronological list of "activities" each displayed slightly differently depending on their type.
Your using $query and $result twice so the second loop is overwriting the result of the first and stopping...
$query = mysql_query("SELECT * FROM activity ORDER BY activity_time DESC LIMIT 50");
and
$query = mysql_query("SELECT * FROM discussions WHERE discussion_uuid = '$activity_ref'");
same with $results var...
I would suggest you change to $query and $query2 but best to use something like
$activies = mysql_query("SELECT * FROM activity ORDER BY activity_time DESC LIMIT 50");
while($activity = mysql_fetch_array($activies)) {
and
$discussions = mysql_query("SELECT * FROM discussions WHERE discussion_uuid = '$activity_ref'");
while($discussion = mysql_fetch_array($discussions)) {
I would also avoid using extract - as you might be overwriting vars your not expecting to...
You have to create another connection to the database so that you can run them at the same time.
OR
You can load the results of the first one into an array, and then just loop through the array.