This Query is Not Working - php

$qurym="SELECT * FROM referal_member WHERE `ref_cusid` = '$id' && `confirm` = '1'";
$resm=mysqli_query($con,$qurym);
$rowm=mysqli_fetch_array($resm);

Just try like this: [ if fields ( ref_cusid and confirm) are of INT type, then pass values $id and 1 without single quotes ( ' ), otherwise make change as below ]
$qurym = "SELECT * FROM referal_member
WHERE `ref_cusid` = ' " . $id . "'
AND `confirm` = '1'";
$resm = mysqli_query($con,$qurym);
// check query returns something or not
if(mysqli_num_rows($resm)){
// fetch data
$rowm=mysqli_fetch_array($resm);
}
else{
// no data found
}

Please remove quotes from confirm if it is integer datatype.
$qurym="SELECT * FROM referal_member WHERE `ref_cusid` = '$id' && `confirm` = 1";
$resm=mysqli_query($con,$qurym);
$rowm=mysqli_fetch_array($resm);
print_r($rowm); // to check results.
Or If ref_cusid is also integer type.
$qurym="SELECT * FROM referal_member WHERE `ref_cusid` = $id && `confirm` = 1";
$resm=mysqli_query($con,$qurym);
$rowm=mysqli_fetch_array($resm);
print_r($rowm); // to check results.

Related

Correct but not displaying result

$sql = "SELECT * FROM one_chat WHERE chat_email = '{$get_mail}' && chat_email = '{$session_email}' ";
Query is CORRECT, but not displaying result on AND condition, if use OR operator then both conditions becomes true and displays both data! Why so disguisting!!!
i think use this and check if it gives any result
$sql = "SELECT * FROM one_chat WHERE chat_email = '{$get_mail}' AND chat_email = '{$session_email}' ";
basically try to replace && with AND
according to the comments , this var value ( $get_mail ) is null , and you try to select data with condition this ( chat_email = '{$get_mail}' AND chat_email = '{$session_email}') ,
you need chat_email to be null and another value come from session .
you can change condition to "or"
or , if the both variables have the same value in your business, you can do that :-
$email = !empty($get_mail) ? $get_mail : $session_email;
$sql = "SELECT * FROM one_chat WHERE chat_email = '{$email}'";
You can also change your query like this
$sql = "SELECT * FROM one_chat WHERE chat_email = COALESCE('{$get_mail}','{$session_email}')";

how to get max value of a sql database query in php

I want to find out the max value of sql data column on the basis of email id and username. (there is no primary entity)
To get the email id, i store the user email id from session.
here goes my code:
$emailid = $userRow['emailid'];
$sql = "select max(item) from product where email = '$emailid' AND username = 'arun'";
$result = $conn-> query($sql);
$row = $result->fetch_assoc();
echo "Max item : " .$row['result'];
It's giving me first value of sql table but not highest.
you can either change your column data-type or use CAST or CONVERT.
$sql = "SELECT MAX( CAST( `item` AS UNSIGNED) ) as max FROM `product` WHERE `email` = '$emailid' AND `username` = 'arun'";
If possible its better to change the data-type.
Try this,
$emailid = $userRow['emailid'];
$sql = "SELECT MAX(item) AS item FROM product WHERE email = '$emailid' AND username = 'arun'";
$result = $conn-> query($sql);
$row = $result->fetch_assoc();
echo "Max item : " .$row['item'];
Try this way,
$rowSQL = mysql_query( "SELECT MAX( ID ) AS max FROM `tableName`;" );
$row = mysql_fetch_array( $rowSQL );
$largestNumber = $row['max'];

How to loop php using ref data from mysql?

How to loop php using ref data from mysql ?
work flow :
Loop If column 'xxx' == ''
{
$sql = "SELECT * FROM table WHERE xxx == '' order by id desc";
$result = mysql_query($sql);
$datas=mysql_fetch_array($result);{
$id = stripslashes(str_replace('\r\n', '<br>',($datas['id'])));
}
$sql = "UPDATE table SET xxx = 'test' WHERE id = '$id' ";
$dbQuery = mysql_query($sql);'
}
ypur query is confusing. why would you store ids with html in them? regardless, this should work
$sql = "SELECT * FROM table WHERE xxx != '' "; //ORDER BY will not work since ids are not int
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result){
$id = stripslashes(str_replace('\r\n', '<br>',($row['id'])));
$sql = "UPDATE table SET xxx = 'test' WHERE id = '$id' ";
$dbQuery = mysql_query($sql);'
}

Cant figure out how to code this in php

I am trying to $_GET some variables that a user may enter (busy making a basic web-server):
$users= strval($_GET['user']);
$price= intval($_GET['price']);
$productID= intval($_GET['productid']);
This is my query:
$query = "SELECT * FROM `table` WHERE `user` = '" . $user . "' AND `price` <= " . $price . " AND `productID` = " . $productID;
(something like this)
Given this theoretical link:
www.example.com/api.php?price=300
And don't use the rest of the GET (user and productID) will automatically be filled with either 0 or '' (int/string).
So I thought I could use an if statement:
if(price == 0){
// change the query without the price as WHERE
}
But what if I have combinations of price and productID or productID and user as variables.
What is the best way to handle this? Maybe it's a stupid question but I can't figure it out.
You can use combined IF statements to build the appropriate query using the variables if they are supplied (and ignoring them if not)
$query = "SELECT * FROM `table` WHERE 1"; // WHERE 1 means "return all rows from table"
if ( isset( $_GET['price'] ) ){ // Only if price variable is set
$query .= " AND price <= '{$_GET['price']}'"; // Restrict results by price
}
if ( isset( $_GET['user'] ) ){ // Only if user variable is set
$query .= " AND user LIKE '{$_GET['user']}'"; // Restrict results by user
}
if ( isset( $_GET['productID'] ) ){ // Only if user variable is set
$query .= " AND productID = '{$_GET['productID']}'"; // Restrict results by productID
}
You can use ternary operator to make query string correctly, concatenating price clause when it is set.
$query = "select * from table where user = $user" . ($price ? " AND price <= $price" : "") . " AND productID = $productID";
Your english is poor, we are brazilians o/
$users = $_GET['user'] || "";
$price = $_GET['price'] || 0;
$productID = $_GET['productid'] || 0;
$query = "SELECT * FROM table WHERE 1";
$query .= (isset($_GET['user'))?"AND user = '{$users}'";
$query .= (isset($_GET['price'))?"AND price <= '{$price}'";
$query .= (isset($_GET['productid'))?"AND productID = '{$productID}'";
In case you want to use the variables for something else. This sets the values to 0 or "" (empty string) if they aren't set in the $_GET data.

Checking for empty fields in mysql table

I have a table with 12 columns and 200 rows. I want to efficiently check for fields that are empty/null in this table using php/mysql. eg. "(col 3 row 30) is empty". Is there a function that can do that?
In brief: SELECT * FROM TABLE_PRODUCTS WHERE ANY COLUMN HAS EMPTY FIELDS.
empty != null
select * from table_products where column is null or column='';
SELECT * FROM table WHERE COLUMN IS NULL
As far as I know there's no function to check every column in MySQL, I guess you'll have to loop through the columns something like this...
$columns = array('column1','column2','column3');
foreach($columns as $column){
$where .= "$column = '' AND ";
}
$where = substr($where, 0, -4);
$result = mysql_query("SELECT * FROM table WHERE $where",$database_connection);
//do something with $result;
The = '' will get the empty fields for you.
you could always try this approach:
//do connection stuff beforehand
$tableName = "foo";
$q1 = <<<SQL
SELECT
CONCAT(
"SELECT * FROM $tableName WHERE" ,
GROUP_CONCAT(
'(' ,
'`' ,
column_name,
'`' ,
' is NULL OR ',
'`' ,
column_name ,
'`',
' = ""' , ')'
SEPARATOR ' OR ')
) AS foo
FROM
information_schema.columns
WHERE
table_name = "$tableName"
SQL;
$rows = mysql_query($q1);
if ($rows)
{
$row = mysql_fetch_array($rows);
$q2 = $row[0];
}
$null_blank_rows = mysql_query($q2);
// process the null / blank rows..
<?php
set_time_limit(1000);
$schematable = "schema.table";
$desc = mysql_query('describe '.$schematable) or die(mysql_error());
while ($row = mysql_fetch_array($desc)){
$field = $row['Field'];
$result = mysql_query('select * from '.$schematable.' where `'.$field.'` is not null or `'.$field.'` != ""');
if (mysql_num_rows($result) == 0){
echo $field.' has no data <br/>';
}
}
?>
$sql = "SELECT * FROM TABLE_PRODUCTS";
$res = mysql_query($sql);
$emptyFields = array();
while ($row = mysql_fetch_array($res)) {
foreach($row as $key => $field) {
if(empty($field)) || is_null($field) {
$emptyFields[] = sprintf('Field "%s" on entry "%d" is empty/null', $key, $row['table_primary_key']);
}
}
}
print_r($emptyFields);
Not tested so it might have typos but that's the main idea.
That's if you want to know exactly which column is empty or NULL.
Also it's not a very effective way to do it on a very big table, but should be fast with a 200 row long table. Perhaps there are neater solutions for handling your empty/null fields in your application that don't involve having to explicitly detect them like that but that depends on what you want to do :)
Check this code for empty field
$sql = "SELECT * FROM tablename WHERE condition";
$res = mysql_query($sql);
while ($row = mysql_fetch_assoc($res)) {
foreach($row as $key => $field) {
echo "<br>";
if(empty($row[$key])){
echo $key." : empty field :"."<br>";
}else{
echo $key." =" . $field."<br>"; 1
}
}
}
Here i'm using a table with name words
$show_lang = $db_conx -> query("SHOW COLUMNS FROM words");
while ($col = $show_lang -> fetch_assoc()) {
$field = $col['Field'];
$sel_lan = $db_conx -> query("SELECT * FROM words WHERE $field = '' ");
$word_count = mysqli_num_rows($sel_lan);
echo "the field ".$field." is empty at:";
if ($word_count != 0) {
while($fetch = $sel_lan -> fetch_array()){
echo "<br>id = ".$fetch['id']; //hope you have the field id...
}
}
}
There is no function like that but if other languages are allowed, you can extract the structure of a table and use that to generate the query.
If you only need this for a single table with 30 columns, it would be faster to write the query by hand...

Categories