My database is called pica
It has the following table called gallery
Example:
gallery==>
id pic1
__ ___
1 picture1.jpg
2 picture2.jpg
3 picture3.jpg
How can I check column sql Belong to the same row in php
Example:
I want to check that picture1.jpg Belong to id 1 and
picture2.jpg Belong to id 2
After that I get a message Verified ,
It's that possible ?
You can achieve this with a query like this:
$row = $db->getOne("SELECT count(*) as count
FROM pica WHERE id = 1
AND pic1 = 'picture1.jpg'");
if ($row['count'] > 0)
{
print 'message Verified';
}
You can use a condition for this:
IF EXISTS (SELECT * FROM pica WHERE id=1 AND pic1='picture1')
SELECT 'verified'
ELSE
SELECT 'failed'
Assuming you want to check $php_pic and $id are in same row or not
Do it this way :
Method 1
/*Fetch records*/
$sql = 'SELECT id,pic1 from gallery where id = $id';
$rs= $mysqli->query($sql); /* fetch details */
$id = $rs['id'];
$pic1 = $rs['pic1'];
/* match using php */
if ($id == $php_id and $pic1 == $php_pic)
{
/* they are in same row*/
}
Method 2
/*Fetch records*/
$sql = "SELECT id,pic1 from gallery where (id = $id and pic1=`$php_pic`)";
$rs= $mysqli->query($sql); /* fetch details */
$row_cnt = $rs->num_rows;
/* match using php */
if ($row_cnt >= 1)
{
/* they are in same row*/
}
Related
i am working on a timeline for my website but i am having some problem when i ran the query to select all id that is less than given identifier its still return the identifier result upon every query
example if identifier is id=4 i want to select everything less than 4 and not from 4 > 3 > 2 > 1 i want it to be 3 > 2 > 1
here is my php. i know its not secure or what not but i have written it in prepared statement and get the same thign so i need some here.
if(isSet($_POST['lastmsg']))
{
$feed_id = mysqli_real_escape_string($con, $_POST['lastmsg']);
$get1 = mysqli_query($con, 'SELECT receiver FROM connection where sender="'.$_SESSION['userid'].'"');
$id_feed = array();
while($id_result1 = mysqli_fetch_array($get1)){
$id_feed[] = $id_result1['receiver'];
$ids1 = join(',', $id_feed);
$get_feed1 = mysqli_query ($con, "select * from feed where users in '".$ids1."' or users='".$_SESSION['userid']."' and 'feed_id' < '".$feed_id."' ORDER BY feed_id DESC LIMIT 2");
}
while($res1 = mysqli_fetch_array($get_feed1)){
echo $load = $res1['feed_id'];
}
}
I have a table
id | field1 | field2 | ... | field[x-1] | field[x]
1 | val1 | val2 | ... | val[x-1] | val[x]
And I'm doing this search
for($i = 1; $i <= $x; $i++){
$getvalue = mysql_query("SELECT * FROM table WHERE id='1' AND field".$i." LIKE 'some_value%'")or die(mysql_error());
while($row = mysql_fetch_array($getvalue)){
$j=$i+1;
$val = $row['field'.$j.''];
}
}
Some values in the table (val[1-x]) will be the same but I need to get only the LAST value. Limit 1 doesn't seem to work.
Update.
Unfortunately as David suggested, I can't change the database. It has to be as it is. I have a text file (a settings dump from a sensor) that I insert it line by line into the db and do a check to see if there are any errors. Most check run ok but I have a few lines in that I need to choose only the last one to do the check.
I have about 10 lines like this
D?
String
R?
String
...
D?
String
R?
String
I'm interested in the last string after R?. I use explode () and each value checked that they are with in limits.
Using ORDER BY id DESC LIMIT 1:
To get last row (with highest=last=newest id), you should use this:
SELECT * FROM table ORDER BY id DESC LIMIT 1;
Your updated code (with mysqli_ extension) to get last row:
$query= mysqli_query("SELECT * FROM table ORDER BY id DESC LIMIT 1") or die (mysqli_error());
}
$lastRow = mysqli_fetch_row($query);
echo $lastRow['id']; //get the last id
Using MAX():
You also can do it using SQL MAX() function:
SELECT * FROM table WHERE id=(SELECT MAX(id) FROM table)
Managed to get it working as I need it by doing this:
for($i = 1; $i <= 250; $i++){
$getvalue = mysql_query("SELECT * FROM full_dump WHERE file_name='".$_FILES['datafile']['name']."' AND field_".$i." LIKE ' 100,%'")or die(mysql_error());
while($row = mysql_fetch_array($getvalue)){
$i_temp = $i;
}
}
if($i_temp != NULL){
$getvalue = mysql_query("SELECT * FROM full_dump WHERE file_name='".$_FILES['datafile']['name']."' AND field_".$i_temp." LIKE ' 100,%'")or die(mysql_error());
while($row = mysql_fetch_array($getvalue)){
$r = $row['field_'.$i_temp.''];
}
}
Ex : I have 1 table below
ID USER1 USER2 USER3
1 X X
2 X X
3 X X
4 X X
5 X X
How i can check ALL value USER2 comlumn is empty in PHP & MySQL? I code below but it's not working
$res = mysqli_query($conn, "SELECT USER2 FROM TABLE")
if(count($res)== "0") echo "OK";
select sum(user2 <> '') as count
from your_table
This would help,
$res = mysqli_query($conn, "SELECT COUNT(*) AS count FROM TABLE where USER2 IS NULL OR USER2=''");
if($res['count']==0)
{
echo "It is Empty";
}
else
{
echo $res['count']." rows have User2 empty";
}
count(*) returns the no of rows coming as the output of the query, which will be in integer form. $res == "0" means that you are comparing with a string "0", not an integer.
$res = mysqli_query($conn, "SELECT * FROM TABLE where USER2=''")
$n_rows_void_user2 = mysql_num_rows($res);
if($n_rows_void_user2) echo "OK";
else
echo "There are $n_rows_void_user2";
First of all you have to write the correct query:
"SELECT * FROM mytable WHERE --user2 condition--"
Where user2 condition may be different depend on your data:
WHERE ISNULL(user2) OR user2=''
It depends on your data.
Then to execute this query in php:
$res = mysqli_query($conn, $query);
$n_rows = mysql_num_rows($res);
if ($n_rows > 0) {
// echo your rows here
}
Also you should connect to database before, i hope you did that.
you can do something like this:
$res = mysqli_query($conn, "SELECT * FROM TABLE where USER2 IS NULL OR USER2 = ''");
$num = $res->num_rows;
if($num != 0){
echo "number of empty row for USER2".$num;
}else{
echo "on empty column for USER2";
}
An extension to the answer by #juergen :
select sum(user2 IS NOT NULL OR some_col <> '') as count
from your_tabl;
table 1
two column >> paid - order_num1
table 2
two column >> order_num2
I want get order_num2 value from table 2 and
update paid(insert paid = 1) in table one with same order_num value
If order_num1=order_num2 then paid = 1 in table 1
$q = mysql_query("select order_num2 from table2 where samevalue = samevalue ");
$x = mysql_fetch_row($q);
mysql_query("update table1 set paid=1 where order_num1='$x['order_num2']'");
But it does not work!
First get from one table and update paid from another table if order_num have same value
Try
$table2 = mysqli_query("SELECT * FROM table2");
$row = mysqli_fetch_array($table2);
$num2 = $row['order_num2'];
$table1 = mysqli_query("SELECT * from table1");
$roww = mysqli_fetch_array($table1);
$num1 = $row['order_num1'];
if ($num2 == $num1) {
$updateDB = mysqli_query("UPDATE table1 SET paid = 1 WHERE order_num1 = '$num2'");
} else {
//Not equal so the paid column wont get updated
}
I made this php script and i am tryin to make it to return next and previus row, but there is one problem when i input my id the script return different thing for example :
This is my DB
ID String
1 Test 1
2 Test 2
3 Test 3
4 Test 4
So if i put ./index.php?id=1 this returns the result of id=2 and id=2 => id=3 and so on...
My question is how to fix it to return accurate result not +1. I tried with <= or => operators the result is correct, but then my links doesnt work.
Here is the script
<?php
if(isset($_GET['id']))
{
$id = (int)$_GET['id'];
}else
{
$id = 0;
}
$stmt1 = $db->prepare("SELECT * FROM records WHERE id > ? ORDER BY id ASC LIMIT 1");
$stmt1->bindValue(1,$id);
$stmt1->execute();
$row = $stmt1->fetch();
$stmt2 = $db->prepare("SELECT * FROM records WHERE id < ? ORDER BY id DESC LIMIT 1");
$stmt2->bindValue(1,$id);
$stmt2->execute();
$row = $stmt2->fetch();
echo $row['id'];
echo "<br/>";
echo $row['string'];
?>
I am not sure if the problem as silly as that, but I have no other explanation.
To have your page you need to make 3 selects:
to get current page data
to get prev id
to get next one
But I can see only 2 selects
So, you have to select data for the very page to show
if(isset($_GET['id']))
{
$sql = "SELECT * FROM records WHERE id = ?";
$stm = $db->prepare($sql);
$stm->execute(array($_GET['id']));
} else {
$sql = "SELECT * FROM records ORDER BY id ASC LIMIT 1";
$stm = $db->query($sql);
}
$row = $stm->fetch();
and now you can go for getting prev and next ids
$sql = "SELECT id FROM records WHERE id < ? LIMIT 1";
$stm = $db->prepare($sql);
$stm->execute(array($row['id']));
$prev = $stm->fetchColumn();
$sql = "SELECT id FROM records WHERE id > ? LIMIT 1";
$stm = $db->prepare($sql);
$stm->execute(array($row['id']));
$next = $stm->fetchColumn();
i am tryin to make it to return next and previus row
There is no such thing as "previous" or "next" row in a table. Without explicit ordering, tables must be considered as unordered set of rows. And you shouldn't rely on auto_increment field to be sequentially numbered. For example:
because there was interleaved insert on the table,
because the server is allowed to reuse auto_increment after row deletion.
You probably have to modify your table structure to add a sequence number:
CREATE TABLE tbl (id in primary key not null auto_increment,
sequence_number int unique,
value char(40));
While inserting your data you might rely on something like that:
INSERT INTO tbl (sequence_number, value)
VALUES (SELECT COUNT(*) FROM tbl, ?)
And the query for the "next" and "prev":
SELECT * FROM tbl WHERE sequence_number = ?-1 OR sequence_number = ?+1
ORDER BY sequence_number;