Comparing two ids from two tables in php using mysql - php

This is the SQL statement I have written.
$result_array
= mysql_query("SELECT * FROM pl_fixtures WHERE id = pl_divisions.id", $con1);
It is written to compare values of dbfix.id with dbdiv.id. Can someone please tell me what is my error in comparing these two "id"s? dbfix and dbdiv are two different tables in a same database. Both have id as primary keys. I am using PHP 5.
Any help will be appreciated. Thank you :)
Regards,
BG

You aren't including the pl_divisions table in your select. Try the following:
$result_array = mysql_query("SELECT * FROM pl_fixtures, pl_divisions WHERE pl_fixtures.id = pl_divisions.id", $con1);

Related

Query mysql record and convert to array using php

I am looking for a help in getting a certain record from MySQL table and then convert it to an array. I have two MySQL tables:
Table(1)name: **sent_msg** ... with the following columns:
serial
tonum
fromch
Table(2)name: **channel** ... with the following columns:
serial
phone
the fromch at table(1) matches serial at table(2). The record I want to get is tonum.
Please advice.
Regards.
First thing is to make a connection using$db = mysqli_connect("localhost", "root", "INSERT_MYSQL_PASSOWRD", "DATABASE") or die(mysqli_error());
After that, make a query using php through: $query = mysqli_query($db, "SELECT * FROM table_name WHERE condition");
Last step is to convert it into a query:
while ($row = mysqli_fetch_array($query)) {
$array[] = $row;
}
$array is now a multidimensional php array. Hope this helped.

Select all cells that are not empty MYSQL

I have a table where I need to find wheter the cell is empty or not.
I don't have the specific column name so I need to display all of the cells that are not empty. (PHP solution would fit too.) Thank you!
Here is my piece of code:
$result1 = mysql_query("SELECT * FROM `FACILITIES` WHERE `room_id` = '{$row['id']}'");
while($row1 = mysql_fetch_assoc($result1)) {
if(empty($row[''])) { //What should I fill in in the $row variable?
alert("Empty");
}
}
I have tried doing in in PHP, but a MYSQL solution would fit too.
You should prevent that problem in the first place. Handle that on DB design level. If you want all your records having values in every column, then define the columns so: not null.
a simple sql that you can rewrite and use would be similar...
sqlfiddle
select * from t where
(col1 is null or col1='')
or
(col2 is null or col2='')
;
sqlfiddle

MYSQL Statement with Array Variable

So I have the following mysql statement saving to an array:
$sql = "SELECT did FROM did WHERE id = '$did_id'";
$result = mysqli_query($link, $sql, MYSQLI_STORE_RESULT);
while($row = $result->fetch_row()){
$did[] = $row;
}
That part works great. But now I need to take the values in the $did array and perform another lookup using the values in it. The way it works is we have users assigned to certain did's. So I find the did's that the user is assigned to (the $did array) and only show them results from another table based on those did values. I have no idea how this part works, but this is what my next statement needs to do:
SELECT * FROM log WHERE did_id = "the values in $did array"
Hope someone can help. I really appreciate it. I haven't really been able to find anything on it.
You can use php's join with mysql's IN to make comma separated strings you can also use implode , join() is an alias of implode();
SELECT * FROM log WHERE did_id IN( ".join(',',$did).")
One thing to mention here that your $did should contains ids in manner like
array("1","2","3"....)
So in your loop fetch first index that holds did column value
$did[] = $row[0];
Note: i assume did , did_id type is int or bigint

How to sum value from one table in phpmyadmin

am new to PHP. my question is i have a table with values and i want to sum the values of one of the column but its not working pls i would appreciate if you can help me with this
mysql_select_db($database_ebsConn, $ebsConn);
$transact4 = "SELECT sum(credit) FROM account WHERE integral_no ='$intergra'";
$transact5 = mysql_query($transact4, $ebsConn) or die(mysql_error());
$transact6 = mysql_fetch_assoc($transact5);
pls i am waiting for your reply
try to go with prepared Statements.... at least use separate database file
$sqlSEND=mysql_query("SELECT sum(credit) FROM account WHERE integral_no ='$intergra'");
while($result = mysql_fetch_assoc($sqlSEND)){
echo $result['sum(credit)'];
}

How do I write a MySQL query that looks for elements whose keys are in a PHP array?

One of the conditions for my query is that one of table.id should belong to the array $table_arr. Is there a way that will allow me to implement this? Any ideas will be appreciated.
I'm guessing $table_arr is a PHP array of ints or strings.
<?php
$query = "SELECT * FROM table WHERE id IN (".implode(", ", $table_arr).")";
$result = mysqli_query($mysql, $query);
// etc

Categories