Print sql query on localhost - php

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

Related

php wont return same as sql query

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];
}

mysqli_fetch_object returns nothing

I try querying very simple sql statement with mysqli
"select * from area where area_pre_id=6035;"
it returns nothing.
After querying this in phpmyadmin , it returns 78 rows ....
PHP code is as below;
$sql = "select * from area where area_pre_id=6035;";
if ($result = mysqli_query($conn, $sql, MYSQLI_USE_RESULT)) {
while($obj = $result->fetch_object()){
if($obj->area_local_name_th){
$my_province = $obj->area_local_name_th . "(" . $obj->area_eng_name . ")";
}else{
$my_province = $obj->area_eng_name;
}
$line[] = array("ProvinceID"=>$obj->area_id,"ProvinceName"=>$my_province);
}
}
Please tell me what's wrong with my code or sql statement.
Your mysqli command is right.I think there is no value in your database for that particular id.
Is the datatype of that id fild integer?
If it is integer then the query is right.But if it is varchar then you have to put a single quote.
select * from area where area_pre_id='6035';
You are trying to use both procedural and OOPs concept. THat will be the issue.
Try this
Change $result->fetch_object() to mysqli_fetch_object($result)

PHP script to search mysql database doesn't return result

Following the code I wrote. This doesn't return any values, even though the table has the keywords.
<?php
$conn = mysql_connect("localhost", "root", "qwerty");
mysql_select_db("mis", $conn);
$coursename=$_POST['coursename'];
$sql = "SELECT *
FROM course
WHERE coursename='$coursename'".
"ORDER BY coursename";
$rs = mysql_query($sql);
while($row = mysql_fetch_array($rs))
{
echo $row['coursename'];
};
?>
The problem is case-sensitivity. MySQL identifiers are not case sensitive unless you enclose them in backticks. However, PHP array indexes are.
Therefore if you have a column named CourseName, the following query will work:
SELECT *
FROM course
WHERE cOuRSEnaME = 'foo'
ORDER BY courSEnAmE
But, referencing it in PHP as $row['coursename'], $row['cOURsENamE'] or any other differing combination will not work, as these all refer to different keys. You must use $row['CourseName'].
See also: PHP array, Are array indexes case sensitive?
Try to add error_reporting(E_ALL); immediately after your <?php and see if you get any error messages from your browser.
You should be able to track down the root cause of your problem.
Good luck with your coursework (i guess ;).
$sql = "SELECT *
FROM course
WHERE coursename='$coursename'".
"ORDER BY coursename";
Your code is good but it is much better to use concatenate the string and the variable so it is easily interpreted and also I wanna point out that there is no space before your ORDER BY statement that can cause an error, so make sure there are spaces between them coursename = '" . $coursename . "' ORDER BY. See the full query below
$sql = "SELECT *
FROM course
WHERE
coursename = '" . $coursename . "' ORDER BY coursename";
$sql = "SELECT *
FROM course
WHERE coursename='" . $coursename . "'
ORDER BY coursename";
$result = mysql_query($sql, $conn);
if(mysql_num_rows($result) > 0) {
while($row = mysql_fetch_array($result)) {
echo $row['coursename'];
}
} else {
echo "given coursename does not exist";
}
?>

mysql query works with plain text, but not with variable

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

PHP not displaying results from MySQL database

I am trying to display an entry from a MySql database which is selected by GET data.
if (isset($_GET["id"])){
$id=$_GET["id"];
$result = getSelectedBlog($id);
while($row = mysqli_fetch_array($result))
{
extract($row);
?>
<div class="headline"><?php echo $headline ?></div>
<div class="subtitle"><?php echo $subTitle ?></div>
<div class="content"><?php echo $content ?></div>
<?php
}
Here is the SQL statement:
function getSelectedBlog($id){
$con = mysqli_connect('localhost', 'root', '', 'michaelWebsite') or die('could not connect');
$sql = 'SELECT * FROM tblArticle WHERE tblArticle.articleID LIKE "$id"';
$result = mysqli_query($con, $sql) or die('entry does not exist.:' . mysqli_error($con));
return $result;
}
As you can see, I am passing the get data as $id to the method that returns the result. However nothing is being returned. There are three entries at the moment, if I change $id in the SQL statement to either 1, 2 or 3 it will show the corresponding data but it just will not work with the $id variable.
The URL does end with the correct info ?id=1.
Please excuse me if it is something stupid, I have just been stuck on this for hours now!!
All of these answers will solve your problem, but none have mentioned or prevented SQL Injection.
In your case I recommend (assuming articleID is an integer field).
$sql = 'SELECT * FROM tblArticle WHERE tblArticle.articleID LIKE "' . (int)$id . '"';
I'm also curious why you are using LIKE for an id field.
Note: Since you are using MySQLi, I'd encourage you to look at prepared statements.
$sql = 'SELECT * FROM tblArticle WHERE tblArticle.articleID LIKE "'.$id.'"';
escape your var in simple quote
Try with this:
$sql = "SELECT * FROM tblArticle WHERE tblArticle.articleID LIKE '$id'";
or with
$sql = 'SELECT * FROM tblArticle WHERE tblArticle.articleID LIKE "' . $id . '"';
You need to use double quotes in order for php to correctly expand your variables :) so change your query to
$sql = "SELECT * FROM tblArticle WHERE tblArticle.articleID LIKE '$id'";
Change
'SELECT * FROM tblArticle WHERE tblArticle.articleID LIKE "$id"'
to
"SELECT * FROM tblArticle WHERE tblArticle.articleID LIKE '$id'"
Variables will be evaluated only if they're between double quotes "

Categories