I have two tables in database and I want to use the result of ist table and compare it with 2nd table, like:
// we connect to example.com and port 3307
mysql_connect("localhost", "root", "pass123") or die(mysql_error());
mysql_select_db("PhGateway") or die(mysql_error());
$result = mysql_query("select mtMsgId from SMS where SMS.`result` = '0' ");
while($row = mysql_fetch_array($result))
{
$mtMsgid=$row['mtMsgId'];
}
I want to compare and then display the result of $mtMsgid with another table
the other table name is DN and has two fields mtMsgId and msgStatus
like:
select * from DN where mtMsgId = 'the whole above result'
I think you're looking for a JOIN. You can do this in SQL:
$result = mysql_query("select s.mtMsgId,j.msgStatus from JN j, SMS s WHERE s.mtMsgId = j.mtMsgId AND s.result = '0' ");
This names two tables to get data from, "SMS" as s and "JN" as j. You 'synchronize' the results with the s.mtMsgId = j.mtMsgId (marry them up according to their mtMsgIds) and you are interested in results for which SMS.result is 0.
Related
Im searching for a function in MySQL in order to Select rows from multiple tables that have a similar name. For example: Proyect_1, Proyect_2, Proyect_3
All of the tables have the same column names, the only difference between the tables is the table name. It starts with the prefix 'proyect'. The issue is that the program doesn´t know how many 'proyect' tables there are, so i can´t make a list of them and select data like always
I need something like this:
SELECT mydata FROM TABLES LIKE 'Proyect_%';
Any ideas? Thanks!
To get all tables with a common prefix
SHOW TABLES LIKE 'Proyect_%';
This will return rows of tables that matched the prefix. Example:
Proyect_1
Proyect_2
Proyect_3
In PHP you can create a UNION query that will pick up the tables returned by the above query,
$sql = "SHOW TABLES LIKE 'Proyect_%'";
$result = $conn->query($sql);
$dataQuery = array();
$query = "";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_array(MYSQLI_NUM)) {
$dataQuery[] = "SELECT * FROM {$row[0]}";
}
$query = implode(' UNION ', $dataQuery);
}
echo $query;
if you want to search for all tables with name like Proyect then you can get from MySQL information schema.
SELECT * FROM information_schema.tables
From here you can find table by table name
Echo out the right row from an array compiled from a mysql database.
I have extracted information from a database (locations) containing three fields: id, name, city into an array called $array. I want to loop through another database (events) in which the id's from the first database (locations) are stored in a field. When looped I want to display the corresponding name and city from the locations database.
Is this possible without having to fetch information every loop?
This is my first try
$query = "Select id, name, city FROM locations WHERE typ = '1'";
$result = mysqli_query($conn, $query);
$row = array();
while ($row = mysqli_fetch_assoc($result)) {
$array[] = $row;
}
And then I thought I could specify the key myself like this:
$query = "Select id, name, city FROM locations WHERE typ = '1'";
$result = mysqli_query($conn, $query);
$row = array();
while ($row = mysqli_fetch_assoc($result)) {
$array[$row['id']] = $row;
}
But I couldn't figure out how to echo the right row.
You can join both tables in a single query, using something like this:
Select locations.id, locations.name, locations.city, events.name
FROM locations
JOIN events ON locations.id = events.id
WHERE locations.typ = '1'
The events.id on the JOIN statement is assuming that this is the column name of the id in the events table. I also made the assumption that these are the two fields that will match between the two tables. Adjust accordingly if your matching criteria is different.
The SELECT statement was modified to pull columns from both tables. Add whichever fields are relevant to your needs.
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.
I am troubles with this method not returning results. There are ids within 'table' that match the array. I guess it's not liking something, except I cannot quite put my finger on it.
$define = ",8,9,10,";
// ** Data Retrieve ** //
mysql_select_db($database_db, $db);
$query_data = "SELECT * FROM table WHERE FIND_IN_SET('".$define."', id)";
$data = mysql_query($query_data, $db) or die(mysql_error());
$row_data = mysql_fetch_assoc($data);
$totalRows_data = mysql_num_rows($data);
In FIND_IN_SET the target value is first, and the list second. So, the query should be as follows:
SELECT * FROM table WHERE FIND_IN_SET(id,'".$define."');
I have 2 tables in the database, the table circle and the table gossips.
The table circle has 3 columns: id, idfrom, idto.
The table gossips has id, userid, gossip.
My objectif is to echo all the gossips only if the userid of this gossip (idto) and the id of the pageuser(idfrom) are on the same row in the table circle.
I wrote this code,
$query = 'SELECT * FROM gossips ORDER BY id DESC';
$result = mysqli_query($cxn, $query)
or die("Couldn't execute query");
$querycircle = 'SELECT idfrom,idto FROM circle WHERE idfrom="{$pageuserid}" ';
$resultcircle = mysqli_query($cxn, $querycircle)
or die("Couldn't check if user in circle.");
while($row = mysqli_fetch_assoc($result))
{
while($rowcheck = mysqli_fetch_assoc($resultcircle))
{
if($row['userid'] == $rowcheck['idto'] || $row['userid'] == $_COOKIE['id']){
echo '<div class="gossip">'.$row['gossip'].'</div>';
}
}
}
But it doesn't seem to work properly.
Do you intend to get the same data with mysqli_fetch_assoc($resultcircle)) on each round of the first while-loop?
Then you should buffer this data in an array first, as executing mysqli_fetch_assoc will always give you the next data set on each successive call. It will not restart on top of the data.
(Btw, this is better: while(null !== ($rowcheck = mysqli_fetch_assoc($result))))
Another method to realize your objective is to do only one combined SQL request.
EDIT: Sudip Pal is fast, look at his answer for a combined request! ;)
Try the single SQL query for this and then try with one while loop.
Select A.gossip from gossips A, circle B where A.userid=B.idto and B.idfrom="{$pageuserid}" order by A.id DESC;
NOTE: you can also compare the COOKIE value by adding the extra comparison on the query, like and B.userid=$_COOKIE["id"] (better to save the cookie value in a variable)