MySQL with php variable trouble - php

I'm having trouble with putting a php variable into a mysql query.
For example:
mysql_query("SELECT * FROM listings WHERE title LIKE '%ipod%'");
That works, but
$key = "ipod";
mysql_query("SELECT * FROM listings WHERE title LIKE '%$key%'");
That doesn't work.
I might be doing it wrong though. If the above is the correct way to do it, then maybe another part of my script has a typo or something like that.
Any help would be great.

Your not selecting anything:
"SELECT * FROM listings WHERE title LIKE '%$key%'"
notice the *

Try this:
$key = "ipod";
mysql_query("SELECT * FROM listings WHERE title LIKE '%".$key."%'");

Try this:
$key = "ipod";
$results = mysql_query("SELECT * FROM listings WHERE title LIKE '$key'");
$num = mysql_num_rows($results);
echo "Received " . $num . "rows of results";
While ($row = mysql_fetch_assoc($results)) {
echo '<pre>';
print_r($row);
echo '</pre>';
}

$key = "ipod";
mysql_query("SELECT * FROM listings WHERE title LIKE '".$key."'");

Related

How to echo data from id

I assume this must be simple, but I'm just stuck on a solution here. How would I echo data from the id in the database? I cannot edit the css div where I display this data so need to find out a way to cut down the PHP, for example:
<? $query = "SELECT * FROM example WHERE id='1'";
$result_1 = mysql_query("SELECT * FROM example WHERE id=1");
$result_2 = mysql_query("SELECT * FROM example WHERE id=2");
$result_3 = mysql_query("SELECT * FROM example WHERE id=3");
while($row_1 = mysql_fetch_array($result_1))
while($row_2 = mysql_fetch_array($result_2))
while($row_3 = mysql_fetch_array($result_3))
{ ?>
From here I echo the data in a div:
<? echo $row_1['name'] ?>
What I am trying to do is something like this: echo $row_1['name']['1']. I want to somewhat use the WHERE id=1 inside my echo. Sorry if this is not clear.
Thanks
You can easily do:
$result = mysql_query("SELECT * FROM example WHERE id IN (1, 2, 3)");
while ($row = mysql_fetch_assoc($result)) {
echo ($row['id'] == 1) ? $row['id'] : ''; // print id only when id == 1
}
Notice: This extension (mysql_*) is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.
Try something like this:
<?
$result = mysql_query("SELECT * FROM `example`"); // Select all columns and rows from the example table
while($row = mysql_fetch_assoc($result)) { // For each row returned
print($row['id'] . " - " . $row['name'] . "<br />\n"); // Print the row's ID and name
}
?>
You can try to do an if() just before the echo, like below:
<? if($row_1['id'] == 1) echo $row_1['name']; ?>
Or from what we know, your results in $row_1 will always have id == 1, since you specified it in the query.
Hope this helps, good luck :)
Try this:
$result = mysql_query("SELECT * FROM example WHERE id IN (1, 2, 3)");
while ($row = mysql_fetch_assoc($result)) {
echo ($row['id'] == 1) ? $row['id'] : ''; // print id only when id == 1
}

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

Issue with mysql query when changing value to a variable

$results = mysql_query("select * from doctorlist where assignednumber = '1231231234' ");
I need to change the number 1231231234 to a variable. If I change it to the code below it does not work. I have displayed the variable on the page so I know it is set.
$results = mysql_query("select * from doctorlist where assignednumber = '$phoneNumber' ");
Could someone please help. I know it is a small issue, but have been unable to fix it.
Perhaps split it like this
$sql_query = "select * from doctorlist where assignednumber='$phoneNumber'";
$results = mysql_query($sql_query);
or
$sql_query = "select * from doctorlist where assignednumber='".$phoneNumber."' ";
$results = mysql_query($sql_query);
First check your variable type with var_dump($phoneNumber) than do the following:
$results = mysql_query("select * from doctorlist where assignednumber = '".$phoneNumber."' ");
to improve readability and last if you expect an Integer cast your variable like:
(int)$phoneNumber
or if string do
mysql_real_escape_string($phoneNumber)
Try using the variable inside the query like this:
'{$phoneNumber}'

PHP MYSQL $row[$variable]

I am trying to work around with dynamic table creation and data fetching. I am trying to get the data using following code :
$myQuery = "SELECT ".$col_name." FROM ".$tabname." WHERE sampleid='".$sid."'";
$result = mysql_query($myQuery);
$row = mysql_fetch_array($result);
echo "<br>".$row['$col_name'];
But, I am unable to get any data back. I checked printing the query and running it in php my admin and its working as I want. But I guess variable in array might not be working I guess.
Please help me regarding the same. Thanks.
The Whole loop looks something like this :
$myQuery = "SELECT * FROM information_schema.columns WHERE table_name = '$tabname'";
$re = mysql_query($myQuery);
while($row = mysql_fetch_array ($re)){
if(!empty ($row)){
$col_name = $row['COLUMN_NAME'];
$myQuery = "SELECT ".$col_name." FROM ".$tabname." WHERE sampleid='".$sid."'";
echo "<br>".$myQuery;
$reqq = mysql_query($myQuery);
$roww = mysql_fetch_array($reqq);
echo "<br>".$roww[$col_name];
}
}
You are fetching an array, not an assoc array. Use this:
echo "<br>".$row[0];
Edit: Having looked a little more, this may not be correct. You can set fetch_array to return assoc arrays.
You cannot parse variables through single quotes '
echo $row['col_name']; // manually typed string.
or
echo $row[$col_name];
or
echo $row["$col_name"];
You tried that->
echo "<br>".$row[$col_name];
OR
$myQuery = "SELECT ".$col_name." FROM ".$tabname." WHERE sampleid='".$sid."'";
$result = mysql_query($myQuery);
$row = mysql_fetch_assoc($result);
echo "<br>".$row[$col_name];
Cause like said #Fluffeh it's not a associative array
Have you tried looping through the results array as such:
while($row = mysql_fetch_array($result)) {
echo $row['col_name'];
echo "<br />";
}
I would suggest the following solution. Give the parameter $col_name and AS Col_Name
Then the $row['Col_Name'] can always be set to the parameter no matter what the value.
$myQuery = "SELECT ".$col_name." AS Col_Name FROM ".$tabname." WHERE sampleid='".$sid."'";
$result = mysql_query($myQuery);
$row = mysql_fetch_array($result);
echo "<br>".$row['Col_Name'];

php Mysql query in a while loop

I'm new to php and i don't have the formal study, since i study it myself
i have a problem and i hope someone can help me.. :(
I have and array
$array = array("where","are","you" and so on.....);
and i want to search the database with all those values
like
$sql_res1 =mysql_query("select * from lady_vega where react like '%$array[0]%'");
$sql_res2 =mysql_query("select * from lady_vega where react like '%$array[1]%'");
$sql_res3 =mysql_query("select * from lady_vega where react like '%$array[2]%'");
.... and so on
there are times that the array don't have the exact number of values.
and someone said to me that loop could be a help...
but i don't know how...
and i also want the results of each mysql query will be stored like this so that i can i dentify which results are from...
$row1 = mysql_fetch_array($sql_res1);
$row2 = mysql_fetch_array($sql_res2);
$row3 = mysql_fetch_array($sql_res3);
... so on
i hope there would a possible solution/technique to this..
Try using the following:
$sql = "select * from lady_vega where react like '%".implode("%' OR react LIKE '%", $array)."%'"
$sql_res1 =mysql_query($sql);
You can chain multiple where clauses using OR and AND in your query:
WHERE field = 'value' AND field2 = 'value'
You could try using a foreach loop for example.
$terms = explode(",",$array);
$query = "select * from lady_vega where ";
foreach($terms as $each){
$i++;
if($i ==1){
$query.="react LIKE '%$each%'";
}
else
$query .= "OR react LIKE '%$each%' ";
}
$sql = "";
for($i=0;$i<count($array);$i++){
$sql.="select *,'".$array[$i]."' as keyword from lady_vega where react like '%".$array[$i]."%'";
if($i!=count($array)-1) $sql .= " union ";
}
$result = mysql_fetch_array(mysql_query($sql));
$last_keyword="";
$index = 1;
foreach($result as $row){
if($last_keyword!=$row["keyword"]){
$index++;
${"row".$index} = array();
}
array_push(${"row".$index},$row);
}
then you'll get the same result with an extra "keyword" column.

Categories