I've got the following query in an existing script - but it's not always returning a value even though it should based off what's in the database. There are plenty of things in the database it SHOULD be grabbing - they are there.
Don't see anything wrong with it - but I barely do this anymore :) See anything?
$query = "SELECT id FROM xtags WHERE tag_id = '$tagid' ORDER BY RAND() Limit 2";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$query = "SELECT * FROM xtable WHERE id = '$row[id]'";
$result = mysql_query($query) or die(mysql_error());
$row2 = mysql_fetch_assoc($result);
echo $row2[title];
}
$result is being used inside the loop and outside, try making a new variable inside and not reusing the outside one.
You're reusing the $result variable inside the loop which overwrites the value for use in the while condition. Use a different name for $query and $result inside the loop.
I don't know if is ok, but you are using $result twice, one before the "while" and another inside the "while".
I would personally split the string and the variable $row.
Why not use var_dump() to see $row and the other variables ???
I don't understand what you are trying to do actually, but try this:
$query = "SELECT id FROM xtags WHERE tag_id = '".$tagid."' ORDER BY RAND() Limit 2";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$query2 = "SELECT * FROM xtable WHERE id = '".intval($row[id])."'";
$result2 = mysql_query($query2) or die(mysql_error());
$row2 = mysql_fetch_assoc($result2);
echo $row2[title];
}
Problem solved - did a join statement.
Related
$sql = "SELECT temail FROM teacherusers WHERE tfullname='$teachername' limit 1";
$result = mysql_query($sql);
$value = mysql_fetch_object($result);
$teacheremail2 = $value->temail;
echo $teacheremail2;
echo $teacheremail2 returns nothing.
$teachername is valid and i have checked multiple times.
It should be a two-dimensional array , you need
$value[0]->temail
The result of mysql_fetch_object($result) is an object(stdClass).
The explanation of object(stdClass) ican be found at this link
$sql = "SELECT temail FROM teacherusers WHERE tfullname='$teachername' limit 1";
$result = mysql_query($sql);
while ($value = mysql_fetch_object($result))
{
$teacheremail2 = $value->temail;
echo $teacheremail2;
}
First off, you'll want to run the query directly against your database to ensure that the query returns some kind of result.
Secondly, if that works, you'll want to echo $value directly to check that you are getting results back on the webpage.
Then you can check if temail is a field of $value
$sql = "SELECT temail FROM teacherusers WHERE tfullname='$teachername' limit 1";
$result = mysql_query($sql);
while ($value = mysql_fetch_object($result))
{
$teacheremail2 = $value->temail;
echo $teacheremail2;
}
hope this help
$sql23 = "SELECT * FROM map1pokemon ORDER BY RAND() LIMIT 1;";
$result23 = mysql_query($sql23) or die(mysql_error());
$battle_get23 = mysql_fetch_array($result23);
$sql2 = "SELECT * FROM pokemon WHERE name='".$battle_get23['pokemon']."'";
$result2 = mysql_query($sql2) or die(mysql_error());
$battle_get2 = mysql_fetch_array($result2);
$filler = "SELECT * FROM pokemon WHERE name='Charmander'";
$filler2 = mysql_query($filler) or die(mysql_error());
$filler3 = mysql_fetch_array($filler2);
$pokemon1 = array_fill(1,10,"".$filler3."");
$pokemon2 = array_fill(1,2,"".$battle_get2."");
$pokemon3 = array_merge($pokemon1, $pokemon2);
shuffle($pokemon3);
$pokemon4 = array_shift($pokemon3);
$pic2= mysql_real_escape_string($pokemon4['pic']);
$pic = strip_tags($pic2);
echo "<center>";
echo '<img src="pokemon/'.$pic.'" border=0><p></p>' ;
The queries at the top grab the information about each pokemon, I then want to use the array_fill and array_merge to make it harder for one of the results to appear the, but right now it doesn't show the result of the pokemon after the mixing of the pokemon, I believe that I'm doing the mixing of the pokemon wrong, but have no idea why?
Does anyone see what I'm doing wrong here?
this line seems to be useless :
$pic2= mysql_real_escape_string($pokemon4['pic']);
Then, it is logical you do not display what you want as you do not store what you want. Let's review your code :
$filler = "SELECT * FROM pokemon WHERE name='Charmander'";
$filler2 = mysql_query($filler) or die(mysql_error());
$filler3 = mysql_fetch_array($filler2);
$filter3 is clearly an array which contains all of your table columns.
$pokemon1 = array_fill(1,10,"".$filler3."");
What? you concatenate an array with a string? Don't use this concate trick it will not bring you good results. $pokemon1 = array_fill(1,10,$filler3); is ok. A little greedy but ok.
One thing you can do is using random generator, it will be easier to use, less greedy and quicker.
$rand = rand(12);
if($rand <=10){
$result = $filler3;
}else{
$result = $battle;
}
echo $result['name'];
I am trying to pass a variable to a very basic mysql query. but php doesnt return a true value. nothing.
i have checked everything
the problem is here.
the syntax of $a varible typing into mysql query
$result = mysql_query("SELECT id,floatingnumber FROM posts WHERE id='$a' LIMIT 1");
when i change $a to 22 it returns a value otherwise nothing.
exact query is here...
$a=$this->post_id;
$result = mysql_query('SELECT floatingnumber FROM posts WHERE id="'.$a.'" LIMIT 1')or die(mysql_error());
$row = mysql_fetch_row($result);
$sdfa=$a.'-'.$row[0];
$sdfa returns "86 - " without quotes 86 - space
so the problem is on the mysql fetch row please help
Have you tried echoing the query to see what the real value of $a is?
echo "SELECT id,floatingnumber FROM posts WHERE id='$a' LIMIT 1";
Have you tried checking for errors?
$result = mysql_query("SELECT id,floatingnumber FROM posts WHERE id='$a' LIMIT 1") or die(mysql_error());
Also, you shouldn't even be using mysql_* as it's deprecated.
This is how you'd do it in PDO:
$stmnt = $db->prepare("SELECT id,floatingnumber FROM posts WHERE id=:id LIMIT 1");
$stmnt->bindValue( ':id' , $a , PDO::PARAM_INT );
$stmnt->execute();
$result = $stmnt->fetchAll(PDO::FETCH_ASSOC);
typically when I'm writing in double quotes, simply putting in the variable works:
"... $1 ..."
but also, I originally learned it with brackets
"... {$1} ..."
you can try that. also, a handy way to write queries is store the query string in its own variable so you can easily print out the query and see what you wrote before submitting.
$query = "SELECT id,floatingnumber FROM posts WHERE id=$a LIMIT 1";
$result = mysql_query( $query );
This helps identify things like this.
try this
$result = mysql_query("SELECT id,floatingnumber FROM posts WHERE id='".$a."' LIMIT 1");
if your $a is a number then do like that
$result = mysql_query("SELECT id,floatingnumber FROM posts WHERE id= $a LIMIT 1");
EDIT :
your code is right
$row = mysql_fetch_row($result);
$sdfa=$a.'-'.$row[0];
the problem is in your sql or table because there is no floatingnumber where id is 86 .
I have the following code that fetches a single row:
$query = "SELECT *
FROM translations
WHERE iddoc = '$id'
AND submitted = 1;";
$result = mysqli_query($query);
$row = mysqli_fetch_array($result);
I know this is useful in a while loop, where you can just loop through the results.
But I need to be able to grab specific rows that meet this condition. Something following this pseudo code:
$query = "SELECT *
FROM translations
WHERE iddoc = '$id'
AND submitted = 1;";
$result = mysqli_query($query);
$arrayofrows = mysqli_fetch_arrayofrows($result);
$arrayofrows[0] //(to get the first row)
$arrayofrows[1] //(2nd row)
and so on...
How can I do this?
You can try something like this
$arrayofrows = array();
while($row = mysqli_fetch_array($result))
{
$arrayofrows = $row;
}
You can now have
$arrayofrows[0] //(to get the first row)
$arrayofrows[1] //(2nd row)
I think the function you are looking for is mysqli_fetch_all as shown below. This avoids the need to create an extra looping structure or a function to do something already provided.
$query = "SELECT *
FROM translations
WHERE iddoc = '$id'
AND submitted = 1;";
$result = mysqli_query($query);
$arrayofrows = mysqli_fetch_all($result);
$arrayofrows[0] //(to get the first row)
$arrayofrows[1] //(2nd row)
It depends on whether you require the entire result set back or not but I think the LIMIT could be used like:
$query = "SELECT *
FROM translations
WHERE iddoc = '$id'
AND submitted = 1 LIMIT 200,200;";
Otherwise as others say you will need to convert to an array (which is what fetch_all does) and then get the element from that array.
I want to echo out one field from my database so I do not want to use a while loop.
The database table is called index and the field that I want to echo is called title.
What is wrong with this code as the output is just blank.
$result = mysql_query("SELECT * FROM index");
$row = mysql_fetch_array($sql);
echo $row['title'];
You're passing a wrong argument to mysql_fetch_array(). Modify it as follows.
$result = mysql_query("SELECT * FROM index");
$row = mysql_fetch_array($result);
echo $row['title'];
You need to pass $result and not $sql with the mysql_fetch_array()function.
Try:
$row = mysql_fetch_array($result);
print_r($row); ///see what you get
The fastest solution would be mysql_result
$result = mysql_query('SELECT title FROM index LIMIT 1');
$field = mysql_result($result, 'title');
You may want to add LIMIT or check your database against something
$result = mysql_query("SELECT * FROM index WHERE id='$someid' LIMIT 1");