How to use PHP to send data over to another page? [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a website which is supposed to show some information about food courts from MySQL server and what I have done is that I made it such that the data would be shown in the website and if they click they would be redirected to another web page. My question is how would I be able to check what is the hyperlink they clicked so that I could display accurate information on the second webpage?
What I have done is that I tried to use get to send the information over but to no avail. Can someone guide me please? I am new to PHP and MySQL coding.
This is the PHP code for the website that I am using to display the food court information.
<?php
require 'dbFunction.php';
$con = getDbConnect();
if (!mysqli_connect_errno($con)) {
$result = mysqli_query($con,"SELECT * FROM foodcourt");
while($row = mysqli_fetch_array($result)) {
echo "Foodcourt " . $row['FCNo'] . "<br />";
echo $row['FCDescription'] . "<br />";
echo $row['FCImage'] . "<br />";
echo "<br>";
$data=$row['FCNo'];
}
} else {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_close($con);
?>
And this is the code I use to "receive" the information.
<?php
require 'dbFunction.php';
$con = getDbConnect();
$data = $_GET['$row[FCNo]'];
if (!mysqli_connect_errno($con)) {
$result = mysqli_query($con,
"SELECT * "
. "FROM StallDetails SD AND FoodDetails FD"
. "WHERE FD.StallID = SD.StallID "
. "AND FD.FCNo = ".$data);
while($row = mysqli_fetch_array($result)) {
echo "Foodcourt " . $row['FCNo'] . "<br />";
echo $row['Details']."<br />";
echo $row['Description'].",<br> ";
echo "Price: $".$row['Price']."<br> ";
} }
else {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_close($con);
This is my getDbConnect();
<?php
function getDbConnect() {
// get a database connect to studentacad database
$con = mysqli_connect("localhost:3306", "waduser01", "password123", "p1315774");
Return $con;
}
?>

You are doing it wrong. Your are using $row in get variable too and You are also not enclosing the $data in quotes ''.And one more thing you are not selecting the database. Use mysqli_select_db function to select your database.You are also using AND in selecting the table. use the code below
<?php
require 'dbFunction.php';
$con = getDbConnect();
mysqli_select_db($con,"db_name");
$data = $_GET['fc'];
if (!mysqli_connect_errno($con)) {
$result = mysqli_query($con,
"SELECT * "
. "FROM StallDetails SD , FoodDetails FD"
. "WHERE FD.StallID = SD.StallID "
. "AND FD.FCNo = '".$data."'");
while($row = mysqli_fetch_array($con,$result)) {
echo "Foodcourt " . $row['FCNo'] . "<br />";
echo $row['Details']."<br />";
echo $row['Description'].",<br> ";
echo "Price: $".$row['Price']."<br> ";
} }
else {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_close($con);
Hope this helps you

You don't need to send any data.
Usually, GET variables are used. Example:
Wayne
Would lead to a file called user.php, in which the GET variable id is checked:
$id = isset($_GET['id']) ? $_GET['id'] : false;
In this example, once you have the id, you can decide on what user details to display:
$userDetails = $user->get($id);
Another example:
Wayne
... translates into:
show=recipe
id=34

You are referencing your variable like this:
$_GET['$row[FCNo]'];
But it looks like the variable in the url is fc, so you would access it like this:
$_GET['fc'];

Related

Can not display MySQL data decimal(16,2) in PHP

I have an MySQL data base that has a cell in a record called balance. The db shows it is stored as a decimal(16,2). When I try to display the vale of the record with echo $row["balance"]; In PHP, it display nothing at all. Can you please point me in the right direction. Thanks you.
$sql = "SELECT id, email, username FROM ppb_users WHERE id = '$USERIDX' ";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
//// output data of each row
while($row = $result->fetch_assoc())
{
echo "<br> id: " . $row["id"] . "<br> email: " . $row["email"] . "<br> username: " . $row["username"] . "<br> Ballance: " . $row["balance"] ."<br>";
$UserEmail = $row["email"];
$balancex = $row["balance"];
}
}
else
{
echo " 0 results <br><br>";
}
$conn->close();
Sorry folks, it would seem I had one of those moments in where when it is over and you realize what you did, how small you feel. I did not add balance to the SQL se3lect line. Uggg! Soryy.

How to run .ktr transformations in PHP

I developed the following API in PHP / HTML:
I need the result of the select option to serve input to the following ETL transformation:
I used "RJ" as an example in the query. But I need this result to come from the API. What step can I use to recover this data?
Is the reverse process possible? Can I make the result of this transformation available (which is an excel worksheet) for the user to download?
Edit 1: That's how I get the form results. It's a simple PHP code.
<?php
$conexaobd = new ConexaoBD;
$conexao = $conexaobd->conectarAoBD();
$buscar=$_POST['buscar'];
$sql = "SELECT p.id_projeto, p.municipio, p.estado, p.nome FROM projeto p WHERE p.estado LIKE '%".$buscar."%' ORDER BY p.id_projeto";
$resultado_query = mysqli_query($conexao, $sql);
if (!$resultado_query) {
echo "Erro: " . $sql . "<br>" . mysqli_error($conexao);
}
if (!$linha = $resultado_query->fetch_array(MYSQLI_ASSOC)){
echo "<h3>Nenhum registro encontrado.</h3>";
} else {
while($linha = $resultado_query->fetch_array(MYSQLI_ASSOC)){
$id = $linha['id_projeto'];
$municipio = $linha['municipio'];
$estado = $linha['estado'];
$nome = $linha['nome'];
echo "<h2><strong>ID do projeto: </strong>".#$id."</h2>";
echo "<br/>";
echo "<strong>MunicĂ­pio: </strong>".#$municipio;
echo "<br/>";
echo "<strong>Estado: </strong>".#$estado;
echo "<br/>";
echo "<strong>Nome do projeto: </strong>".#$nome;
echo "<br/>";
}
}
?>

Creating a PHP API (Convert data from mySQL to JSON);

So my 1st target is to convert expecting data from mySQL as JSON before use XML.
Things i trying to solve- Connect two tables from database for specific user and display it on screen.
Everything is in one PHP file:
<?php
include 'db.php';
session_start();
ob_start();
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM users
INNER JOIN user_quiz ON users.user_uid = user_quiz.user_uid
WHERE users.user_uid = '".$_SESSION['u_uid'] ."'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$outp = array();
$outp = $result->fetch_all(MYSQLI_ASSOC);
// for check if record exist
echo "<br> id: ". $row["id"]. " - Name: ". $row["user_first"]. " " . $row["user_last"] . " id:" . $row["user_uid"] . " Selected qustion " . $row["q_topic"] . " ". $row["q_id"] . "<br>";
}
} else {
echo "0 results";
}
$myJSON = json_encode($outp);
echo $myJSON;
?>
So now where is a issue.. When i do SQL like that:
SQL:
$sql = "SELECT * FROM users";
PHP is convert data as JSON data... but when i trying to do that for specific user i have empty []... That's why i use echo to see if i have output or i have error but... i have displays value... Had no idea what's going on... need advice.

not going inside while loop in 'while($row = mysqli_fetch_array($stmt))' [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I tried to execute the following php program in my friend's laptop, but the nothing is getting executed inside the while loop.
<html>
<body>
<?php
$db_hostname = "localhost";
$db_username = "user";
$db_password = "password";
$db_name = "resulta";
$link=mysqli_connect($db_hostname,$db_username,$db_password,$db_name);
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$htno = $_POST['htno'];
$query="select * from marks where hallno=?";
if ($stmt = mysqli_prepare($link, $query)) {
mysqli_stmt_bind_param($stmt, 's', $htno);
/*http://php.net/manual/en/mysqli-stmt.bind-param.php*/
/* execute query */
mysqli_stmt_execute($stmt);
/* store result */
mysqli_stmt_store_result($stmt);
printf("Number of rows: %d.\n", mysqli_stmt_num_rows($stmt));
if(mysqli_stmt_num_rows($stmt)>0)
{
echo "thank you";
echo "recieved";
echo "<table>";
echo "<tr><th>Hall Ticket No.</th><th>Sub. Code</th><th>Sub. Name</th><th>Internal Marks</th><th>External Marks</th><th>Total Marks</th><th>Credits</th></tr>";
//$stmt = mysqli_query($link,$query);
while($row = mysqli_fetch_array($stmt)) {
echo "inside yeah!!!";
echo "<tr><td>" . $row['hallno'] . "</td><td>" . $row['subcode'] . "</td><td>" . $row['subname'] . "</td><td>" . $row['intemarks'] . "</td><td>" . $row['extmarks'] . "</td><td>" . $row['totalmarks'] . "</td><td>" . $row['credits'] . "</td></tr>";
}
echo "</table>";
echo "over";
mysqli_stmt_close($stmt);
mysqli_close($link);
}
else
{
/* close statement */
mysqli_stmt_close($stmt);
echo "Roll No. not found";
}
}
?>
</body>
</html>
So, why is the code inside the loop not getting executed while all the code outside the while loop (also which prints the no. of rows in the result) is executing properly, I've searched in Google but and tried solutions but nothing is getting displayed if I use
while($row = mysqli_fetch_array($stmt, MYSQLI_ASSOC)) or
while($row = mysqli_fetch_array($stmt, MYSQLI_NUM)) //with $row[0]...
I tried a similar code which is working, in case if it helps:
$con=mysqli_connect($db_hostname,$db_username,$db_password,$db_name);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// select everything from the news table
$st=0;
$result = 'False';
$result = mysqli_query($con,"SELECT * FROM notif ORDER BY dat desc LIMIT $st, 6");
echo "<ul>";
while($row = mysqli_fetch_array($result)) {
echo "<li style='font-family: 'Open Sans',sans-serif;' ><b><u>[" . $row['subject'] . "]</u></b> " . $row['note'] . " <i>by <b>" . $row['user'] . "</b> on <b>" . $row['dat'] . " </b></i></li>";
echo "<br>";
}
echo "</ul>";
// disconnect from the database
mysqli_close($con);
To check if it was problem with the computer I tried the same code in other computer as well, but it wasn't executing, don't know why some code is executing and some are not, even when I'm not able to fetch the result but the function printf("Number of rows: %d.\n", mysqli_stmt_num_rows($stmt)); is giving the correct output, with correct no. of rows.
I'm running ubuntu 14.04 I also tried the code on my friend's laptop who is running unbuntu 14.04, can anyone please check both the codes if they are running, still can't understand why only that code is not working, both the codes(that work and that don't work) look same to me, but don't know why when I write code for different purposes, sometimes they work and sometimes they don't.
Can anyone please try the code?
Your $stmt is of type mysqli_stmt, thats why it works with mysqli_stmt_num_rows
mysqli_fetch_array however expects a mysqli_result which you can get by storing the result of mysqli_stmt_store_result($stmt);
Make the equal sign double, like this:
while($row == mysqli_fetch_array($stmt))

php mysqli select resultset is not showing anything

Ive been trying to crack this for 2 hours, but something is wrong. I am very much used to doing things without mysqli but read that there is a recommended shift towards it from regular mysql commands. Hence am stuck with following:
<?php
$mysqli = new mysqli('localhost', 'admin', 'test123', 'kadmindb');
if ($result = $mysqli->query("SELECT * FROM records WHERE '$queryType' = '$keyword'")) {
while ($row = $result->fetch_object()) {
echo "<h2>Result:</h2><br>";
echo "ID: " . $row->id . "<br>";
echo "Name: " . $row->cust_name . "<br>";
echo "Invoice No: " . $row->invoice_num . "<br>";
echo "Date: " . $row->date_recorded . "<br>";
}
}
?>
This code is shown in the page where the result of the query should be displayed but nothing is displayed. I checked that both keyword and queryType variables are set and they contain the correct values. Any help would be greatly appreciated. All am trying to do is: select statement to retrieve all the details based on invoice_num submitted.
EDIT: from help I received, I was able to get this working:
$query = "SELECT * FROM records WHERE ".$queryType. " LIKE '%$keyword%' ";
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_object()) {
echo "<h2>Result:</h2><br><hr/> ";
echo "ID: " . $row->id . "<br>";
echo "Name: " . $row->cust_name . "<br>";
echo "Invoice No: " . $row->invoice_num . "<br>";
echo "Date: " . $row->date_recorded . "<br>";
echo "<hr/>";
}
}
Are you sure there's data to select? This code will only output data if there actually is.
Make sure that $queryType and $keyword are set and have sane values that will yield a result.
Use var_dump($queryType) and var_dump($keyword) immediately before the query. Now check your output. Are they both strings? Run this query directly in PHPMyAdmin and check how many rows you get.
If you can't do that try echo'ing the number of rows returned along with the query values:
if ($result = $mysqli->query("SELECT * FROM records WHERE $queryType = '$keyword'"))
{
while ($row = $result->fetch_object())
{
echo "<h1>Query WHERE '$queryType' = '$keyword' yielded {$result->num_rows} rows!</h1>";
echo "<h2>Result:</h2><br>";
...
Note, you should not have single quotes around the column ($queryType), if you insist you should use backtick quotes (`) but it's unnecessary really - if you're that pedantic you should be using prepared statements.
Also be sure to filter them for any potentially dangerous values that could allow for sql injections. See: mysqli::real_escape_string
Assuming that $queryType is the name of a column in your records table, then I believe the problem is your WHERE clause.
Rather than:
$mysqli->query("SELECT * FROM records WHERE '$queryType' = '$keyword'")
You should have:
$mysqli->query("SELECT * FROM records WHERE {$queryType} = '{$keyword}'")
Note that I've removed the single quotes around $queryType and have used complex (curly) syntax
Also, in the future you might want to try using an else block to trap errors:
$mysqli = new mysqli('localhost', 'admin', 'test123', 'kadmindb');
if ($result = $mysqli->query("SELECT * FROM records WHERE {$queryType} = '{$keyword}'")) {
while ($row = $result->fetch_object()) {
echo "<h2>Result:</h2><br>";
echo "ID: " . $row->id . "<br>";
echo "Name: " . $row->cust_name . "<br>";
echo "Invoice No: " . $row->invoice_num . "<br>";
echo "Date: " . $row->date_recorded . "<br>";
}
}
else
{
echo "Error: " . $mysqli->error;
}

Categories