small php fetch query with "WHERE" issue - php

$u_ress = mysql_query("SELECT * FROM `blackjack` WHERE `brukernavn`='$spiller->brukernavn' AND `by`='$by'");
$bj = mysql_fetch_object($qry);
This code wont work. It wil only show my $by, but its not what i want. I want it to get from blackjack where brukernavn is ( as it said) AND from by aswell.
How can i this?

SELECT * FROM blackjack WHERE brukernavn='{$spiller->brukernavn}' AND by='$by'
Notice when doing advanced variables within the string you must use brackets.

Try below code,
echo "Query:=".$u_ress = ("SELECT * FROM blackjack WHERE `brukernavn` ='{$spiller->brukernavn}' AND `by` ='$by'");
$query = mysql_query($u_ress);
//$bj = mysql_fetch_object($query);
mysql_query is not recommended.

Related

ajax variable mysql search does not work with space

I got problem with MYSQL selecting when I use ajax variable which cointains space.
e.g.
$city = $_POST["from_ajax"];
$query = "SELECT * FROM `rest` WHERE city='".$city."'";
problem is when variable cointains "space" e.g.
variable which ajax sends is: "New York";
mysql query doesn't work.
what can couse this problem?
THANK YOU
use mysql TRIM()
$city = $_POST["from_ajax"];
$query = "SELECT * FROM `rest` WHERE TRIM(city)=TRIM('$city')";
Use this:
$city = $_POST["from_ajax"];
$query = "SELECT * FROM `rest` WHERE city LIKE '%$city%';

Put mysql results in one php array, like this?

To get an array like this array("123","456","789"); I use the code:
$Regids = mysql_query("SELECT regid FROM $tabel WHERE active = '1'");
while($row = mysql_fetch_array($Regids))
{
$result_array[] = "\"".$row['regid']."\"";
}
$regIDs = implode(',', $result_array);
$registrationIDs = array($regIDs); // array("123","456","789");
but I would expect PHP/mySQL has a simpler/faster solution for this?
I doubt that your code produces the result you want.
// assuming the this query produces 123,456,789
$Regids = mysql_query("SELECT regid FROM $tabel WHERE active = '1'");
// $row contains: array("123")
while($row = mysql_fetch_array($Regids))
{
$result_array[] = "\"".$row['regid']."\"";
}
// $result_array now contains: array("\"123\"", "\"456\"", "\"798\"");
$regIDs = implode(',', $result_array);
// $regIDS now contains a single string: "\"123\",\"456\",\"798\"";
$registrationIDs = array($regIDs);
// registrationIDs now is an array containing a single string: array("\"123\",\"456\",\"798\"");
If you really need an array that looks like this: array("123","456","789"); it is much simpler.
$Regids = mysql_query("SELECT regid FROM $tabel WHERE active = '1'");
while($row = mysql_fetch_array($Regids))
$registrationIDs[] = $row['regid'];
and that's all.
If your mysql result contains the number as an integer instead of an string you can convert it like this:
$Regids = mysql_query("SELECT regid FROM $tabel WHERE active = '1'");
while($row = mysql_fetch_array($Regids))
$registrationIDs[] = strval($row['regid']);
Also, keep in mind that the mysql_* functions are becoming deprecated. Don't start new code with it and make plans to port your existing code to mysqli_* or PDO.
You can use PDO implementation. At first sight, it may be more difficult to understand, but once you get used to it, it reveals to be really powerful and handy (IMHO! One year ago i switched to it and i love it)!
For your example, the PDO implementation would be like this:
/*CONNECT TO DB, FIRST. $dbh contains a handler to the current DB connection*/
$stmt = $dbh->prepare("SELECT regid FROM table WHERE active = '1'");
$stmt->execute();
$Regids = $stmt->fetchAll(PDO::FETCH_COLUMN,0);
There are many formatting options you can specify, like
PDO::FETCH_COLUMN
PDO::FETCH_ASSOC
and more...These options will allow you to get the array formatted as you prefer. As you can see i got the result in just 3 simple rows.
EDIT
Note: you are not escaping PHP variables before inserting them in your Query, and your code may suffer SQL INJECTION. Be careful!! Here is a simple guide to prevent it.
(In my code, just to be clear, i avoided the problem by just putting the table name instead of $table, just to show simply how to get the result you wanted.)
try this .. use Group concat in query ...
$Regids = mysql_fetch_array(mysql_query("SELECT GROUP_CONCAT(regid) as regids FROM $tabel WHERE active = '1'"));
echo $Regids[0]['regids']; // 123,456,789
for getting result "123","456","789" try this
$Regids = mysql_fetch_array(mysql_query("SELECT GROUP_CONCAT('\"',CONCAT(regid),'\"') as regids FROM $tabel WHERE active = '1'"));
echo $Regids[0]['regids']; // "123","456","789"

MySQL query not working after adding AND operator

I'm pretty sure i messed up the quotes, but can't find where exactly. It stopped working after adding the AND operator. Can anybody guide me to the right direction?
$result = mysql_query("SELECT * FROM pl_table WHERE p_num=".$w[0] AND l_num='.$w[0]);
Try this:
$result = mysql_query("SELECT * FROM pl_table WHERE p_num='".
$w[0]."' AND l_num='".$w[0]."'"
);
Basically, you messed up w/ opening/closing quotes.
What error message did PHP return to you? Most of the times, if not always your solution lies therein.
You broke your query string. See this difference
$result = mysql_query(
"SELECT * FROM pl_table
WHERE p_num='".$w[0]."'
AND l_num='".$w[0]"'");
It is the quotes, your AND needs to be inside the quotes, and it is not.
Yours:
$result = mysql_query("SELECT * FROM pl_table WHERE p_num=".$w[0] AND l_num='.$w[0]);
What it should be:
$result = mysql_query("SELECT * FROM pl_table WHERE p_num=".$w[0]." AND l_num=".$w[0]);

building a sql query string with php parameters

I'm trying to do this, but it returns null?
$query_1=$field_name[0]."='{".$field_value[0]."}'";
and then
getType = mysql_query("SELECT * FROM wines WHERE $query_1") or die(mysql_error());
while if i do like this:
$getType = mysql_query("SELECT * FROM wines WHERE $field_name[0]='{$field_value[0]}'") or die(mysql_error());
it works fine.
is this even possible, or am I missing something too obvious?
thank you in advance!
You are building it the wrong way. You should never use curly brackets (or any other string) in a SQL query. Concatenate your query instead.
Like this:
$query_1=$field_name[0]."='".$field_value[0]."'";
and oh, you missed a $ before your query, thats why its null.
This works for me:
$field_name[0] = "test";
$field_value[0] = "someting";
$query_1=$field_name[0]."='".$field_value[0]."'";
echo ("SELECT * FROM wines WHERE $query_1") or die(mysql_error());
Hope it helps

php sql where command to check for containg

I get a string vaiable using POST in my php. I want to append this string so that i can use in the sql query to check for containing this string. I want something like this %string%
What i am doing now this gives me error:
$hotelName = '%'+hotelName_old+'%';
just to be sure: did you change your statement to ... WHERE fieldname LIKE '%value%'??
it should be like this
$hotelName = "%'".$hotelName_old."'%";
try this.
just directly use your variable.
$sql = "select * from `tbl_name` where `field_name` like '%{$find}%'"
$result = mysql_query($sql);

Categories