How can I separate. to my database variable? - php

I have few data in mysql(link,description,date,del(delete status))
now Im taking this data from mysql with my code. But I wanna show my data's like this photo
as you can see all data's looks clear and separate.
Im using this sql code for take these data's
$sql = "SELECT `vLink` ,`vDescription`, `vDate` FROM `video_table`
But when I use my code output looks like
https://www.youtube.com/watch?v=D8f9IiUXA4EMYSQL | How to auto date time with MYSQL?2021-01-17 02:32:55
now I can use 3 sql code(for link,description,date) to take my all data's and I can put list each by each but Im wondering have we easy way?
thats my code
$sql = "SELECT `vLink` ,`vDescription`, `vDate` FROM `video_table` WHERE `vDel` = 0 ";
$result = mysqli_query($conn,$sql);
$Vdatas = array();
if (mysqli_num_rows($result) > 0)
{
while ($row = mysqli_fetch_assoc($result)) {
$Vdatas[] = $row;
}
}
//print_r($Vdatas);
foreach ($Vdatas[0] as $data) {
echo $data;
}

Related

Storing database results to a variable in PHP

I am trying to store the result from a MySQL statement for later use in PHP. I have this code which gets me the result:
// Get the categories from the db.
$categories = array();
$catSql = "SELECT id, name FROM categories";
if ($catStmt = mysqli_prepare($db, $catSql))
{
$catStmt->execute();
$result = $catStmt->get_result();
// Fetch the result variables.
while ($row = $result->fetch_assoc())
{
// Store the results for later use.
}
}
So I know i will have the results in $row["id"] and $row["name"] and I want to save all of the rows so whenever i need them i can loop through them and for example echo them. I have searched for structures and arrays for keeping them in PHP but I cannot seem to find any information about that or maybe I am not searching in the right direction. Can anyone point me where i should read about this to find out how to do this efficiently and if possible post a small example?
Use sessions:
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
// Get the categories from the db.
$categories = array();
$catSql = "SELECT id, name FROM categories";
if ($catStmt = mysqli_prepare($db, $catSql))
{
$catStmt->execute();
$result = $catStmt->get_result();
// Fetch the result variables.
while ($row = $result->fetch_assoc())
{
// Store the results for later use.
$_SESSION['category_' . $row['id']] = $row['name'];
}
}
Then access it later from a different page
$_SESSION['session_variable_name']
You can also create an array of the information and store the entire array in a single session variable.
Just make sure you add the session_start function at the beginning of each page. The if statement prevents you from trying to start it multiple times.
$categories = array();
$catSql = "SELECT id, name FROM categories";
if ($catStmt = mysqli_prepare($db, $catSql))
{
$catStmt->execute();
$result = $catStmt->get_result();
while ($row = $result->fetch_assoc())
{
$categories[$row['id']]=$row['name'];
}
}
And If you want the name anywhere use below :
$categories[$id]

Create php array from one of all column that retrieve from mysql

I am a newbie on PHP,MySQL and HTML.
I have one question about php array.
I create MySQL query that joins 2 tables in php. From this join, I will get information about one product, but have multiple JIG.
$sql = "SELECT product.product_number,product.product_name,product.product_jitqty,product.product_desc,jit.jit_number,jit.jit_name,jit.jit_drawer,jit.jit_port,jit.jit_specpath
FROM product
JOIN production_jit
ON production_jit.product_number = product.product_number
JOIN jit
ON jit.jit_number = production_jit.jit_number
WHERE product.product_number = '$productnumber'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result))
{ }
from above query, I will get result like picture below.
So I want to create an array for column jit.jit_number. for easy to transfer both or only one data to another page.
I have tried below code.
$sql = "SELECT product.product_number,product.product_name,product.product_jitqty,product.product_desc,jit.jit_number,jit.jit_name,jit.jit_drawer,jit.jit_port,jit.jit_specpath
FROM product
JOIN production_jit
ON production_jit.product_number = product.product_number
JOIN jit
ON jit.jit_number = production_jit.jit_number
WHERE product.product_number = '$productnumber'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
?>
<?php
while($row = mysqli_fetch_assoc($result))
{
$jig = array ($row['jit_number']);
echo "This is JIG " . $jig[0] . ", " . $jig[1] . ".";
}
But the result is like picture below.
Can anyone help me? So if I want to transfer both data to another page, I just can use $jig[0] and $jig[1]. Or anyone can advise me the better way to transfer multiple data form mysql to another page.
I'm not really sure what you're trying to achieve, but $jig is not going to have two indices because $row['jit_number'] ist just a number itself. If you're trying to store all jit_numbers in a separate array, you could try something like this:
$numbers = array();
while($row = mysqli_fetch_assoc($result))
{
$numbers[] = $row['jit_number'];
}
You shall also try to prevent MySQL injection, WHERE product.product_number = '$productnumber'"; is rather horrible from a security point of view. Using a prepared statement or at least some sanitization / escaping is recommendable.
Try this->
$jig = array();
array_push($jig, $row['jit_number']);

How can I get this php to return the entire column of an sql db

I am trying to query a db for an entire column of data, but can't seem to get back more than the first row.
What I have so far is:
$medicationItem = array();
$medicationItemSql = "SELECT medication FROM medication";
$medicationItemObj = mysqli_query($connection, $medicationItemSql);
if($row = mysqli_fetch_array($medicationItemObj, MYSQLI_NUM)){
echo count($row);
}
It's not my intention to just get the number of rows, I just have that there to see how many it was returning and it kept spitting out 1.
When I run the sql at cmd line I get back the full result. 6 items from 6 individual rows. Is mysqli_fetch_array() not designed to do this?
Well, I had a hard time understanding your question but i guess you are looking for this.
$medicationItem = array();
$medicationItemSql = "SELECT medication FROM medication";
$medicationItemObj = mysqli_query($connection, $medicationItemSql);
if($row = mysqli_num_rows($medicationItemObj))
{
echo $row;
}
Or
$medicationItem = array();
$medicationItemSql = "SELECT medication FROM medication";
$medicationItemObj = mysqli_query($connection, $medicationItemSql);
$i = 0;
while ($row = mysqli_fetch_array($medicationItemObj))
{
$medicationItem[] = $row[0];
$i++;
}
echo "Number of Rows: " . $i;
If you just want the number of rows i would suggest using the first method.
http://php.net/manual/en/mysqli-result.num-rows.php
You can wrote your code like below
$medicationItem = array();
$medicationItemSql = "SELECT medication FROM medication";
$medicationItemObj = mysqli_query($connection, $medicationItemSql);
while ($row = mysqli_fetch_assoc($medicationItemObj))
{
echo $row['medication'];
}
I think this you want
You could give this a try:
$results = mysqli_fetch_all($medicationItemObj, MYSQLI_NUM);
First, I would use the object oriented version of this and always use prepared statements!
//prepare SELECT statement
$medicationItemSQL=$connection->prepare("SELECT medication FROM medication");
// execute statement
$medicationItemSQL->execute();
//bind results to a variable
$medicationItemSQL->bind_result($medication);
//fetch data
$medicationItemSQL->fetch();
//close statement
$medicationItemSQL->close();
You can use mysqli_fetch_assoc() as below.
while ($row = mysqli_fetch_assoc($medicationItemObj)) {
echo $row['medication'];
}

Setting PHP variables from Database Using MYSQL

Looking to select multiple values from the database and echo with PHP. (Newbie)
For instance:
SELECT sponser, contract, script FROM Copy WHERE day = '11092014' and time = 4
SELECT sponser, contract, script FROM Copy WHERE day = '11092014' and time = 5
SELECT sponser, contract, script FROM Copy WHERE day = '11092014' and time = 6
How would I set the variables.. something along the lines of this using MYSQLi for multiple variables?
$sqlStremail = "SELECT subcheckr
FROM login
WHERE username = '$u'";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
$variable = $row["subcheckr"];
Truly appreciate any help.
Yes. Though if your query returns multiple rows, you'll need to use:
while ($row = mysqli_fetch_assoc($result))
{
//do something here
}
you need to make a loop for , foreach or a while loop
ex
while ($row = mysqli_fetch_assoc($result))
{
}
This should do it. First you need to connect, then you build your query. If query fails display an error so you know what went wrong. Then build your data array and use it.
$db = mysql_connect("localhost", "mysql_user", "mysql_password");
$sqlStremail = "SELECT `subcheckr`
FROM `login`
WHERE `username` = '".$u."'"; //needs to be concatenated
$result = mysql_query($sqlStremail, $db);
if(!$result) {
echo "query failed:". mysql_error();
exit;
}
$data = array();
while ($row = mysql_fetch_assoc($result)) {
$data = $row;
}
echo $data['sponsor'];
echo $data['contact'];
echo $data['script'];
//etc

PHP : Construction of Multidimensional Array to JSON

Say I have a database which has the following layout:
Fields: |Business_Name| |Business_ID|
Data : |business_1A | |ABC_1 |
I want to query the database and retrieve the business name and business ID at the same time and then encode this result to JSON for further usage.
How do I go about doing this?
Here's some code as requested:
$sql = "SELECT Business_Name,Business_ID FROM biz_table";
$businessArray = array();
$bizResult = mysql_query($sql);
while($row = mysql_fetch_assoc($bizResult)) {
$businessArray[][] = $row['Business_Name']$row['Business_ID'];
}
$result = json_encode($businessArray);
echo $result;
Thanks in advance!
Based on your question - your question was "how do i do that?" without really giving an example of the output you want :
$sql = "SELECT Business_Name,Business_ID FROM biz_table";
$businessArray = array();
$bizResult = mysql_query($sql);
while($row = mysql_fetch_assoc($bizResult)) {
$businessArray[$row['Business_Name']] = $row['Business_ID'];
}
$result = json_encode($businessArray);
echo $result;
$result will be
{"business_1A" : "ABC_1" }
Is that what you want ?

Categories