ok, so I have a search script with is sort of working but I dont get the results I want.
when i run the query in php it doesnt result anything.
$searchStr = htmlspecialchars($searchStr);
$sql = "SELECT * FROM steamitems WHERE steamid='" . $id . "' AND name LIKE '%".$searchStr."%'";
$r_query = mysqli_query($link, $sql);
but if I run the exact same output as $sql (SELECT * FROM steamitems WHERE steamid='76561198196240283' AND name LIKE '%sawed%') in phpmyadmin it returns the correct result..
EDIT: I forgot to mention that I obviously print the results here
while ($row = mysqli_fetch_array($r_query)){
echo $row["assetid"];
echo $row["name];
}
Related
I'm making a query to my database, but the query always return empty. If I copy the query and paste it directly to mysql, it show one record, as it should be.
This is my query in php:
$sql = "SELECT * FROM disponibilities WHERE st_week = '" .
$st_week . "' AND en_week = '" . $en_week . "' AND boat = '" .
$_POST['boat'] . "'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result)) {
$disponibility = $row;
}
} else {
echo "0";
echo $_POST['boat'];
}
And this is the query (echo of the previous query) that show one record if used in phpmyadmin:
SELECT * FROM disponibilities WHERE st_week = '2021-11-13' AND en_week = '2021-11-20' AND boat = 'sapphire'
I've tried converting the date in this way, but still don't work
$sql= "SELECT * FROM disponibilities WHERE st_week =
STR_TO_DATE('$st_week','%Y-%m-%d') AND en_week = STR_TO_DATE('$en_week','%Y-%m-%d')";
EDIT:
this is my phpmyadmin query response:
Thanks in advance
try to run
SELECT * FROM disponibilities WHERE st_week = '2021-11-13' AND en_week = '2021-11-20' AND boat = 'sapphire';
this query from your code what i mean is that run the query with static values instead of variables and see if it returns anything if it does then you are getting wrong value in your variables...
let me if this is the problem if not also so that i can help you further...
I made the following query:
mysqli_query($conn, "SELECT image_first, REPLACE(image_first,'/home/erik/','')
FROM reviews_media WHERE review_id = $id");
I've tested it in PhpMyAdmin and it's working. But when I echo it, it's still showing the /home/erik/ part.
What am I doing wrong?
Obvioulsy you echo image_first value, but you need to echo result of REPLACE. You can modify query as:
mysqli_query($conn, "SELECT image_first, REPLACE(image_first,'/home/erik/','') as new_img FROM reviews_media WHERE review_id = $id");
See, I added an alias to result of REPLACE function. Now you can echo something like:
echo $row['new_img'];
As you don't do anything to result of REPLACE in a query, you can also simplify it and do a replace with php:
mysqli_query($conn, "SELECT image_first FROM reviews_media WHERE review_id = $id");
// fetching results
echo str_replace('/home/erik/', '', $row['image_first']);
I have a SQL query in my PHP file that makes use of some variables in it. I want to print the query itself on the localhost to check as to whether the entire query is been executed or not.
My query is like this:
$result = mysql_query("SELECT * FROM sample WHERE col01 LIKE '%$abc%',$db);
I am trying to print the query using echo $result but get Resource id #25 on localhost. I want to print Select * FROM ... as the output. Is there any way?
First of all: You are missing a double quote: $result = mysql_query("SELECT * FROM sample WHERE col01 LIKE '%$abc%'",$db).
That said, what stops you from
$sql="SELECT * FROM sample WHERE col01 LIKE '%$abc%'";
$result = mysql_query($sql,$db);
echo $sql;
If you were using PDO (and you should, the old mysql_ functions are deprecated and insecure) you could just use PDOStatement->queryString property to view the query at a later time.
Store as a variable $sql
Its normal, first you need to fetch that resource obj
And anyway you missing a double quote,
example.
$sql = "SELECT * FROM sample WHERE col01 LIKE '%$abc%'";
$result = mysql_query($sql);
while ($line = mysql_fetch_object($result)) {
echo $line->colname ."\n";
}
echo "\n" . ' query: ' . $sql
And from PHP 5.5.0 and beyond use mysqli
$sql = "SELECT * FROM sample WHERE col01 LIKE '%$abc%'";
if ($result = $mysqliobj->query($sql)) {
while($line= $result->fetch_object()){
echo = $line->colname ."\n";
}
}
echo "\n" . ' query: ' . $sql
or print_r($mysqliobj->info); # store las query
I am trying to print out some topic information, but it is not going so well. This is my query:
SELECT * FROM topics WHERE id='$read'
This doesn't work. I've echo'ed the $read variable, it says 1. So then if I do like this:
SELECT * FROM topics WHERE id='1'
It works perfectly. I don't get what is the problem. There's no hidden characters in $read or anything else like that.
Try like this:
$query = "SELECT * FROM topics WHERE id='" . $read . "'"
ID is normally a numeric field, it should be
$id = 1;
$query = "SELECT * FROM topics1 WHERE id = {id}"
If you are using strings for some reason, fire a query like
$id = '1';
$query = "SELECT * FROM topics1 WHERE id = '{$id}'"
SELECT * FROM topics WHERE id=$read
it consider it as string if you put i single quotes
I wonder why all the participants didn't read the question that clearly says that query with quotes
SELECT * FROM topics WHERE id='1'
works all right.
As for the question itself, it's likely some typo. Probably in some other code, not directly connected to $read variable
try
$query = sprintf("SELECT * FROM topics WHERE id='%s';",$read);
Also remember to escape the variable if needed.
Looks like you might have an issue with the query generation as everyone else is pointing to as well. As Akash pointed out it's always good to build your query in to a string first and then feed that string to the MySQL API. This gives you easy access to handy debugging techniques. If you are still having problems try this.
$id = 1;
$query = "SELECT * FROM `topics1` WHERE `id`={$id}";
echo ": Attempting Query -> {$query}<br />";
$res = mysql_query($query, $dblink);
if($res <= 0)
die("The query failed!<br />" . mysql_error($dblink) . "<br />");
$cnt = mysql_num_rows($res);
if($cnt <= 0)
{
$query = "SELECT `id` FROM `topics1`";
echo "No records where found? Make sure this id exists...<br />{$query}<br /><br />";
$res = mysql_query($query, $dblink);
if($res <= 0)
die("The id listing query failed!<br />" . mysql_error($dblink) . "<br />");
while($row = mysql_fetch_assoc($res))
echo "ID: " . $row['id'] . "<br />";
}
This will at least let you monitor between calls, see what your query actually looks like, what mysql says about it and if all else fails make sure that the ID you are looking for actually exists.
try with this : SELECT * FROM topics WHERE id=$read
I want to query a MySQL database. I have the following. $name keeps changing in a loop. ($query is a sample query here)
$query = "SELECT id FROM table1 WHERE name='$name';";
$result = mysql_query($query) or die(mysql_error());
echo "$result<br/>";
While ($row = mysql_fetch_array($result)) {
echo $row["id"] . " - " . $row["name"];
}
with the echo "$result<br/>" it just prints something like Resource id #. Nothing from $row is printed. The MySQL connection is fine. If I run the query without PHP, it works fine. What might be wrong?
Everything is correct, you'll get nothing from $result until you use some function like mysql_fetch_array() or mysql_fetch_assoc() like you do in your loop.