I have the code below to search for the text 'english' anywhere in the field language. I've tried formatting the code from SQL into a web PHP page but it doesn't display any information. When I run the sql version select language from media where language like '%english%' it works.
$result = mysql_query("SELECT `language` FROM `media` WHERE `language` LIKE \'%english%\'");
while ($row = mysql_fetch_array($result)) {
echo $row[''];
echo ' ' , $row['language'];
echo '<br />';
}
Don't escape the single quotes around your search term.
$result = mysql_query("SELECT `language` FROM `media` WHERE `language` LIKE '%english%'");
You don't need to escape the ''s. It should just work. So try this:
$result = mysql_query("SELECT `language` FROM `media` WHERE `language` LIKE '%english%'");
Related
I'm trying to search a value in a specific mysql database column via PHP. However, it's not working. I tried many different ways and I had no success so far.
I'm trying the following code:
<?php
require('connect.php');
$search= 'search';
$query = "SELECT * FROM `user1` WHERE `description` LIKE '%'a'%'";
if($result){
$smsg = $query;
}else{
$fmsg ="Sorry, nothing was found. Try using our menu on top left.";
}
?>
Change it:
"SELECT * FROM `user1` WHERE `description` LIKE '%'a'%'";
to
"SELECT * FROM `user1` WHERE `description` LIKE '%a%'";
and try again. It will return you all the rows in which description column contains character a.
I'm trying to select all the rows from my database, where 'street' is LIKE the contents from an array ($streets).
Here's what I have...
#TEXT AREA INPUT = $streets
$sql = "SELECT * FROM `data` WHERE `street` LIKE '%".implode("%' OR `street` LIKE '%",$streets)."%'";
echo $sql;
$result = mysqli_query($con,$sql) or die(mysql_error());
$totalitems1 = mysqli_num_rows($result);
echo $totalitems1 . "<br>";
while($row = mysqli_fetch_array($result))
{
echo $row['street']. "<br>";
}
the varible $streets is exploded from a text area input, each value on a new line.
When I process this using PHP, only the rows that are LIKE the last 'OR' are returned .. (The last line in the text area). But when I copy and paste the generated SQL into PHPMYADMIN, it returns ALL the data, as expected.
What am I missing here? Thanks.
My guess would be that you aren't stripping the newline from the end of each $street entry, therefore only the last entry looks valid as it probably doesn't have a trailing newline. No doubt your query probably looks like...
SELECT * FROM `data` WHERE `street` LIKE '%foo
%' OR `street` LIKE '%bar
%' OR `street` LIKE '%baz%'
The quick fix would be to use...
implode("%' OR `street` LIKE '%", array_map(function($s) use ($con) {
return $con->escape_string(trim($s));
}, $streets))
Ideally, you should be using a prepared statement with parameter binding.
I am reading values from excel sheet in $number_column variable
$number_column = $data->sheets[0]['cells'][$row1][1];
Then make a select query
$query_patent = "SELECT `id` FROM `ipoverview` WHERE number ='$number_column'";
It is not working. If I echo my $number_column I am getting the data at that location.
And if hard code any number to $number_column, that query is executing.
$number_column="12345";
echo $query_patent = "SELECT `id` FROM `ipoverview` WHERE number ='$number_column'"; //This works
Help me figure out this issue??
Make sure that you have UTF-8 as a character encode for your excel file,
and also make sure that you should have utf8_general_ci as collation for your database column
for more info please refer http://docs.php.net/manual/en/mysqli.set-charset.php.
thanks
You've got this line:
echo $query_patent = "SELECT `id` FROM `ipoverview` WHERE number ='$number_column'";
shouldn't that be something like this:
$query_patent = "SELECT `id` FROM `ipoverview` WHERE number ='$number_column'";
$rResult = mysqli_query($dblink,$query_patent)or die("something went wrong when executing this query:<br /><br />" + $query_patent + "<br /><br />"+ mysqli_error($dbLink));
// Dot Stuff with $rResult
while($row = mysqli_fetch_row($rResult)){
print_r($row);
}
Try this.And also echo $query_patent whether you are getting a variable on that variable before executing the query..
echo $query_patent = "SELECT `id` FROM `ipoverview` WHERE number ='".$number_column."'";
The response of the above echo
SELECT `id` FROM `ipoverview` WHERE number = 'E1234A'
SELECT `id` FROM `ipoverview` WHERE number = 'E1234B'
SELECT `id` FROM `ipoverview` WHERE number = 'E1234C'
Im trying to display the amount of a certain pokemon that is in the game by the name of the pokemon and by the type, ( there is two types normal and shiny ) I want it to only grab one type hard to explain... right now its grabing both types and I don't know why here's my code :/
$sql23 = "
SELECT * FROM user_pokemon
WHERE belongsto='". $_SESSION['username']."'AND (slot='1')
";
$result = mysql_query("
SELECT * FROM user_pokemon
WHERE belongsto='". $_SESSION{'username'}."'AND (slot='1')
");
while($row = mysql_fetch_array($result))
{
$sql = "SELECT * FROM pokemon WHERE name='".$row['pokemon']."'";
$result = mysql_query($sql) or die(mysql_error());
$battle_get = mysql_fetch_array($result);
$count = mysql_query("
SELECT count(*) FROM user_pokemon WHERE pokemon='".$row['pokemon']."
'AND type='".$row['type']."'
");
$count2 = mysql_fetch_array($count);
I honestly don't understand why it doesn't work, any help would be appreciated :)
$row['pokemon'] is the name of the pokemon
$row['type'] is the type of the pokemon (shiny, normal, ect..)
user_pokemon table as asked:
Field Type Null Default Comments
-----------------------------------------------------------
id int(11) No
hp int(55) No 30
pokemon varchar(50) No
belongsto varchar(50) No
exp int(50) No 500
item varchar(50) No No Item
nickname varchar(50) No No Nickname
move1 varchar(50) No Ember
move2 varchar(50) No Ember
move3 varchar(50) No Ember
move4 varchar(50) No Ember
slot int(50) No
level int(90) No 5
time_stamp timestamp No CURRENT_TIMESTAMP
gender varchar(25) No Male
type varchar(55) No Normal Type: normal/shiny etc
safari_zone int(10) No 0
One of the things I regularly see on StackOverflow is lack of code formatting. In my view fixing this is a great way to make your code much more readable both to yourself (aids learning) and to others (aids your readers here). It additionally reduces the need for horizontal scrolling, allowing you to place several editors side-by-side on your screen.
Here is your code with suggested code formatting:
$sql23 = "
SELECT
*
FROM
user_pokemon
WHERE
belongsto = '{$_SESSION['username']}'
AND (slot = '1')
";
$result = mysql_query($sql23) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$sql = "
SELECT * FROM pokemon WHERE name = '{$row['pokemon']}'
";
$result = mysql_query($sql) or die(mysql_error());
$battle_get = mysql_fetch_array($result);
$count = mysql_query("
SELECT
COUNT(*)
FROM
user_pokemon
WHERE
pokemon='{$row['pokemon']}'
AND type='{$row['type']}'
");
$count2 = mysql_fetch_array($count);
}
This helped indicate that your while was missing a closing brace, and that the variable $sql23 wasn't actually used. Also note that you can use array values inline in double-quoted strings, by wrapping them in braces.
As noted in the comments, you should upgrade to PDO or mysqli. Also, be careful about injecting values directly into the SQL if they have come from the user, as this can lead to SQL injection vulnerabilities. Look into query parameterisation, or at least untainting, to ensure your code is safe.
now i use this code:
$welcome_text = mysql_query("SELECT * FROM `text` WHERE `name` = 'welcome'");
while ($row = mysql_fetch_array($welcome_text, MYSQL_ASSOC)) {
echo $row['content'];
}
Is it possible to use it without WHILE if i know exactly which one column i need?
Something like this:
$welcome_text = mysql_query("SELECT 'content' FROM `text` WHERE `name` = 'welcome'");
echo $welcome_text;
Thanks
mysql_query makes the query, returns a result set.
mysql_fetch_array fetches the first row from the result set.
$welcome_text = mysql_query("SELECT * FROM `text` WHERE `name` = 'welcome'");
$row = mysql_fetch_array($welcome_text, MYSQL_ASSOC);
echo $row['content'];
Of course, you can shorten your code if you want, but this may make your code more difficult to debug and maintain.
Verbose, clear code > one-liner 'show-off' code.
Including 'just-in-case' checking:
$welcome_text = mysql_query("SELECT * FROM `text` WHERE `name` = 'welcome'");
if($row = mysql_fetch_array($welcome_text, MYSQL_ASSOC)){
echo $row['content'];
}
Good practice to make doubly sure you have what you need before printing it.
Finally, please make sure you sanitize user-submitted data before it goes into your database. If you're not going to use prepared statements, at least use mysql_real_escape_string.
Practice safe SQL, wear a prepared statement to prevent SQL Injections.
I would write a function for this:
// Returns content of first column in first result and
// returns null if query returns no records
function mysql_get_result($sql) {
$query = mysql_query($sql); // you may add error handling ...
if (mysql_num_rows($query) != 0) {
$row = mysql_fetch_array($query, MYSQL_NUM));
return $row[0];
} else {
return null;
}
}
And now you can use:
$welcome_text = mysql_get_result("SELECT `content` FROM `text` WHERE `name` = 'welcome'");
Note: You may throw an exception instead of returning the null value. But what is better is hard to say and it may depend on your programming style.
That's not entirely the same thing. In your first sample, you're iterating over multiple rows, not columns.
Unless you're certain that there's only one row with the name welcome, you're still going to need the loop.
You are right that you shouldn't select * when you only need some of the columns, it's wasteful.
In other words, you should use the only slightly modified:
$welcome_text = mysql_query("SELECT `content` FROM `text` WHERE `name` = 'welcome'");
while ($row = mysql_fetch_array($welcome_text, MYSQL_ASSOC)) {
echo $row['content'];
}
$welcome_text = mysql_fetch_row(mysql_query("SELECT 'content' FROM `text` WHERE `name` = 'welcome'"));
echo $welcome_text[0];