Query mysql record and convert to array using php - 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.

Related

Updating JSON Array in PHP

The following code is used to get all information and fetch it into a json array. Now I changed the 'author' to userids instead of username strings and I want to get the username strings from another table called 'login'.
But how can I get all 'author'-ids, queue another table and get all usernames with this id and update these in the fetched json array so I echo the usernames with the 'author'- id? I am not really used to JSON arrays, although this code always worked for me until I changed my database structure.
I tried researching some information but I did not find what I was looking for. Please excuse my bad english.
$con = mysqli_connect(HOST,USER,PASS,DB);
$sql = "SELECT title, content, author, id, date, timestamp, importance, version FROM news ORDER BY timestamp DESC";
$res = mysqli_query($con,$sql);
$result = array();
while($row = mysqli_fetch_array($res)){
array_push($result,
array('title'=>$row[0],
'content'=>$row[1],
'author'=>$row[2],
'id'=>$row[3],
'date'=>$row[4],
'timestamp'=>$row[5],
'importance'=>$row[6],
'version'=>$row[7]
));
}
$oldjson = json_encode(array("result"=>$result));
echo json_encode(array("result"=>$result));
mysqli_close($con);
You can change your SQL query to a join of 'news' and your 'author' table. Take a look at https://www.w3schools.com/sql/sql_join.asp there is an example for joining two table by customer id

Using mysql_num_rows with multiple tables?

Here's the code I've started with:
$result = mysql_query("SELECT * FROM apis_hashes_a", $link);
$count = mysql_num_rows($result);
What I need to do is to get the total sum of all rows for all tables that start with apis_hashes_.
There are tons of tables and new ones are being added all the time, but all of these tables start with apis_hashes_ at the beginning. Is this something that would be possible to do or do I have to list every table individually in the PHP code?
JUST use SUM() IN SQL.Use the code below
<?php
$query = "SELECT SUM(TABLE_ROWS) as score
FROM INFORMATION_SCHEMA.TABLES
WHERE SCHEMA = '{your_db_name}'";
$result = mysql_query($query, $link);
while($row=mysql_fetch_array($result)){
$total_rows = $row['score'];
}
echo $total_rows;
?>
Hope this helps you
The sql:
show tables like 'apis_hashes_%'
will return the list of all your tables, then you need loop thru all the names of the tables with the mysql sum() function.

Loop for taking element if other = 1 (from a row)

I would like to have a loop which will go through all elements, and if LoggedIn='1' then it should take country from this row, and then I will connect to the other table, and increment this row, where country=country from 1st table, but I don't know how to start this loop (I mean I don't know how to do if LoggedIn='1', then take this row's country). This is my code:
<?php
require_once("config.php");
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$take = "SELECT * FROM acc WHERE LoggedIn='1'";
$data = mysqli_query($dbc, $take);
while($row = mysqli_fetch_assoc($data))
{
}
?>
just correct your sql
SELECT * FROM acc WHERE LoggedIn = "1"
no need to filter the data in php if you can narrow down your results with sql
now I dont know what you with incerent mean so I wait for an edit or comment to help your further
OK, first off this isn't a great question. A simple Google search should help you to your solution - every tutorial of MySQL will cover SELECT and WHERE query's, every PHP tutorial will cover iterative loops.
You don't want to query the whole database and then loop over it within PHP when you can limit that dataset by simply using
SELECT country FROM table WHERE loggedin = 1

Compare the results of two tables using foreach

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.

How to query all fields in a row

I know this is very simple, but I haven't used PHP/MySQL in a while and I have been reading other threads/php website and can't seem to get it.
How can I query a single row from a MySQL Table and print out all of the fields that have data in them? I need to exclude the NULL fields, and only add those that have data to an html list.
To clarify, I would like to display the field data without specifying the field names, just for the reason that I have a lot of fields and will not know which ones will be NULL or not.
What you've outlined requires 4 basic steps:
Connect to the database.
Query for a specific row.
Remove the null values from the result.
Create the html.
Step 1 is quite environment specific, so that we can safely skip here.
Step 2 - SQL
SELECT * from <tablename> WHERE <condition isolating single row>
Step 3 - PHP (assuming that $query represents the executed db query)
//convert the result to an array
$result_array = mysql_fetch_array($query);
//remove null values from the result array
$result_array = array_filter($result_array, 'strlen');
Step 4 - PHP
foreach ($result_array as $key => $value)
{
echo $value \n;
}
Just SELECT * FROM table_name WHERE.... will do the trick.
To grab data from specific fields, it would be SELECT field_1,field_2,field_3....
you have to make a string which represent mysql query. Then there is function in php named mysql_query(). Call this function with above string as parameter. It will return you all results. Here are some examples
You need to do it like this...
First connect to your sql... Reference
Now make a query and assign it to a variable...
$query = mysqli_query($connect, "SELECT column_name1, column_name2 FROM tablename");
If you want to retrieve a single row use LIMIT 1
$query = mysqli_query($connect, "SELECT column_name1, column_name2 FROM tablename LIMIT 1");
If you want to fetch all the columns just use * instead of column names and if you want to leave some rows where specific column data is blank you can do it like this
$query = mysqli_query($connect, "SELECT * FROM tablename WHERE column_name4 !=''");
Now fetch the array out of it and loop through the array like this..
while($show_rows = mysqli_fetch_array($query)) {
echo $show_rows['column_name1'];
echo $show_rows['column_name2'];
}
If you don't want to include the column names in the while loop, you could do this:
while($show_rows = mysqli_fetch_array($query)) {
foreach( $show_rows as $key => $val )
{
echo $show_rows[$key];
}
}

Categories