Hi to all!
I have a query that gets the name and an id.
The results is like this :
54 - Rian Ree Barrientos
I wanted to get the number 54.
I used echo (int)$_GET['number'];
But the result is "0". How can I get the number?
You can't get it that way, you need to make two query string vars for it eg:
<a href="somepage.php?number=54&str=some_string">
Make sure that if you do so, you use the urlencode function.
Now you can use:
echo (int) $_GET['number'];
And:
echo $_GET['str'];
Otherise you can use the explode function to get the two values by specifying the - delimiter.
I think you want id from the value you get from the query
I think you want something like following
$strPrice1 = mysql_query("SELECT id, name FROM table_name where id=".$_GET['id'] );
$strPrice = mysql_fetch_array($strPrice1);
echo (int)$_strPrice['id'];
But the result is "0". How can I get the number?
Really?
When I run:
print (int)('54 - Rian Ree Barrientos') . "\n";
I get 54
(php v 5.1.6)
Maybe $_GET['number'] doesn't contain what you think.
C.
Related
I have some data resembling the following:
444444444444444
I want to add a mask to the data so it looks like this:
44.444.444.4-444.444
I tried using:
$x_data = preg_replace('~.{3}\K(?!$)~', '.', $x_data);
Which got me:
444.444.444.444.444
How do I modify my preg_replace to give me the exact output I am looking for?
You could use a capture group approach, and then connect the subcomponents according to the format you are expecting.
$x_data = "444444444444444";
echo $x_data . "\n";
$x_data = preg_replace('/^(\d{2})(\d{3})(\d{3})(\d)(\d{3})(\d{3})$/',
'$1.$2.$3.$4-$5.$6',
$x_data);
echo $x_data;
This prints:
444444444444444
44.444.444.4-444.444
I need one help. I need to fetch all data from query string using PHP but some special charcters like (i.e-+,- etc) are not coming. I am explaining my code below.
http://localhost/test/getmethod.php?name=Goro + Gun
Here I need to get the value assign to name using the below code.
<?php
$name=$_GET['name'];
echo $name;
?>
Here I am getting the output like Goro Gun but I need the original value i.e-Goro + Gun .Please help me to resolve this issue.
#subhra try this for this case name=Goro + Gun:
<?php
$nameArr = explode('=', $_SERVER['QUERY_STRING']);
$name = str_replace("%20", " ", $nameArr[1]);
echo $name;
?>
$_SERVER['QUERY_STRING'] - this will return you full query string
I'm working on a project that uses the Facebook API. Im using methods from that API to automaticly create a username. But for that I need to add the firstname and the lastname.
The way on how I retrieve the firstname and lastnames is:
$userNode->getLastName();
$userNode->getFirstName();
How do I add those return values togheter?
If I do this:
$string = $userNode->getFirstName() + $userNode->getLastName();
echo "$string";
the echo shows "0".
Thanks in advance!
Mats de Waard
The first string operators example in the manual will show you:
$string = $userNode->getFirstName() . $userNode->getLastName();
You need to make a concatenation in php
in your example use "." instead of "+"
$string = $userNode->getFirstName() . $userNode->getLastName();
I have a php file that returns a single number (i.e. 360). How can I get that number to appear in my android textview. I am able to do this with arrays that look like this:
{"success":1,"message":"Post Available!","posts":[{"name":"John Smith","id":"1"}, ...]}
but how can I do it with just a single number.
Here is the php:
<?php
include('connect.php');
$gettotal=mysql_query("SELECT * FROM records");
totalrecords=mysql_num_rows($gettotal);
echo json_encode($totalrecords);
?>
Thanks.
A number is STILL going to be just a number after your json_encode() it.
$num = 42;
$json = json_encode($num);
echo "$num / $json"
will just output 42 / 42 - there will be no practical difference between the two.
Let's say i check if
$strig = "how can i do this";
already exists in my database with all words order options?
Like:
"how i can do this"
or
"i do this can how"
...
...
my database looks like:
id string
1 how can i do this
2 hello how are you
3 how i can do this world
4 another title
etc etc
Thanks
The number of possible combinations is n! (120 in your sample) so checking if this string already exists is quite complex task.
I would recommend to use the following algorithm:
Add new column StringHash to your table
On insert order your string (e.g. alphabetically), calculate its hash and store in StringHash:
"how can i do this" => "can do how i this" => md5("can+do+how+i+this")
If you want to check if a certain string exists in the db then again calculate its hash as described above and query the db on YourTable.StringHash
This is a tricky problem if you want to fix this in sql only, but that aside:
As #er.anuragjain says, you can do a query with LIKE %word%, but you would also get a hit on your example '3'.
So if you have a query like this:
SELECT * FROM table WHERE
column LIKE '%how%'
AND column LIKE '%can%'
AND column LIKE '%i%'
AND column LIKE '%do%'
AND column LIKE '%this%'
Then you also get number 3. So you need to check if there are no other words. You can do this by checking the word count (if you have 5 words and all of your words are in there, you are done.).
Checking wordcount is not trivial, but there is a trick. From several sources*:
SELECT LENGTH(total_words) - LENGTH(REPLACE(total_words, ' ', ''))+1
FROM tbl_test;
should do the trick. So check the LIKE's, and check the wordcount, and you're done. But I'm not really sure this is a pretty sollution :)
http://www.webtechquery.com/index.php/2010/03/count-number-of-words-in-mysql-mysql-words-count/
and http://www.mwasif.com/2008/12/count-number-of-words-in-a-mysql-column/
(random google hits :) )
you can ask if the string where you are searching in, contains: "how" and "can" and "I" and "do" and "this"
something like this:(I don't know the syntax in mysql but see the concept)
if(string.contain("how")&&
string.contain("can")&&
string.contain("I")&&
string.contain("do")&&
string.contain("this"))
{
//you find the string
}
If you are using mysql then try this..
select * from tablename where columnname Like '%how%' AND columnname LIKE '%can%' AND columnname LIKE '%I'% AND columnname LIKE '%DO'% AND columnname LIKE '%This'%;
Here if u have dynamic value in $string then first convert it into an array spliting by space.then create a $condition varriable from the array and append that in select * from tablename where and run that query.
thanks
should be the wildcard call in mysql
select * From tablename Where columnname LIKE '%how%'
you can use regex first crate a function like this
public function part($str){
$str = str_replace('',' ',$str);
$arr = explode(' ',$str);
$rejex = '';
foreach($arr as $item){
$rejex .= "(?=.*$item)";
}
return $rejex;
}
and then use sql regex
$sql = "SELECT * FROM `table` WHERE `column` REGEXP ".part($str);