I'm trying to create a page that shows the name of multiple mysql tables and creates a link for each one. When a user clicks a link, he's taken to a page that shows him the table contents.
For example:
Link with table name.
Table name
The user clicks the link and is taken to the page contents.php. That page prints the table contents.
For example if I have a table with Name and Age columns and John and 24 inserted in the columns, the page would print John and 24.
I appreciate if anybody can help me.
It would be way easier to do this with a GET request rather than a post request.
Table name
Then in your contents.php file:
<?php
$tablename = $_GET["tableName"]
/* all your queries that you want to do with tablename goes here */
?>
try this one...
$result = mysql_query("show tables"); // run the query and assign the result to $result
while($table = mysql_fetch_array($result)) { // go through each row that was returned in $result
echo "<a href=\"content.php?tableName=".$table[0]."\" >".$table[0] . "</a><BR>"; // print the table that was returned on that row.
}
content.php...
if(isset($_GET["tableName"]))
{
$select="select *from ".$_GET["tableName"];
$result=mysql_query($select);
while($row=mysql_fetch_array($result))
{
echo $row["name"];
echo $row["age"];
}
}
Related
Basically, I want to take a value from running a stored procedure that randomly generates a number, and use it later on in the website. Eventually, I'm going to need to pass that randomly generated number back to the MySQL database depending upon the actions that the user takes.
Stored Procedure:
BEGIN
SET #leftplayerone = 0;
SET #rightplayerone = 0;
SET #leftplayerone = (SELECT FLOOR(RAND()*((SELECT COUNT(*) FROM
Players)-1+1))+1);
WHILE #rightplayerone = 0 OR #rightplayerone = #leftplayerone
DO
SET #rightplayerone = (SELECT FLOOR(RAND()*((SELECT COUNT(*) FROM
Players)-1+1))+1);
END WHILE;
SELECT #leftplayerone;
SELECT #rightplayerone;
END
I have a separate table that includes a number of players. I want to randomly pull 1 player from that table and then display that player on my website. Then, I want to randomly display a different player from that same table. The user should then be presented with these two randomly generated players and then choose an option presented to them with 5 different buttons. I want to then send that response, along with the two players that were randomly generated back to the database to store that information.
I'm just looking for some help on how to pull the two randomly generated players from the stored proc and display that on the site - I just put the rest of the details for context.
Update:
My PHP code is:
<?php
$sql = "CALL FantasyUnited.GetRandomPlayersForTradeProposal();";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr><td>". $row["PlayerOne"] ."</td><td>". $row["PlayerTwo"] ."
</td></tr>";
}
echo "</table>";
}
else {
echo "0 result";
}
$conn-> close();
?>
I am now able to display the random Player IDs - now I need to figure out how to display the player's name that correlates to the player ID instead of just displaying the ID number. Then save that information and pass it back to the database when a user clicks on a button regarding those two players.
http://pr0digy.me/trades.php
The user will select one of the options (buttons) below the names of the players. I want the button they pushed and the names of the players stored and sent back to the database.
Change the last line of the procedure to:
SELECT #leftplayerone AS leftplayer, #rightplayerone AS rightplayer;
so that you can fetch both results at once in PHP.
Then in PHP you can do:
$result = $pdo->query("CALL yourProcedure");
$row = $result->fetch(PDO::FETCH_ASSOC);
$leftplayer = $row['leftplayer'];
$rightplayer = $row['rightplayer'];
This is PDO syntax, the corresponding mysqli syntax is not very different.
I am trying to display couple of fields from sql table and want to add a link in each row that takes user to a page that shows all results for that specific table row.
Let me explain more: I have a MySql table that has the following fields: filename, intro_data, content_data, conclusion_data. I have created a php page that displays the list of all data for the filenames & intro_data fields in that sql table using this code:
//Query to select rows or data in table
$query= "SELECT filename,intro_data FROM file_data";
$result=mysqli_query($connect, $query);
if (!$result)
{
die('Error fetching results: ' . mysqli_error());
exit();
}
echo '<table border="1">'; // start a table tag in the HTML
//Storing the results in an Array
while ($row = mysqli_fetch_array($result)) //Creates a loop to loop through results
{
echo "<tr><td>" . $row['filename'] . '</td><td>' . $row['intro_data'] . '</td><td> More Info</td></tr>';
}
echo '</table>'; //Close the table in HTML
//Closing DB Connection
mysqli_close($connect);
If you noticed in the above code, I have a "More Info" anchor tag that links to 'full_table_row_results.php'. In this page, I want it to show all the field results (filename, intro_data, content_data, conclusion_data) for that particular row. I know that I can query all results for a table like this:
$query= "SELECT * FROM file_data";
But how can I create the 'full_table_row_results.php' that queries all fields for that particular row that the user is clicking on? Since the row results are from an array, how do I know which row number in that array has the user clicked on? I am not sure how to code the More Info page.
I am stuck on this and not sure how to implement this.
One solution (as always, there's many other).
First, you need an id for each row in your table (if you do not have already one). With MySQL an auto_increment integer field does the job.
Next, you need to get the id in your php code.
$query= "SELECT id, filename,intro_data FROM file_data";
Then you use it as a parameter when you link to your full_table_row_results.php script. The link will be:
echo '<a href="full_table_row_results.php?id=' . $row['id'] . '">' /* etc. */ ;
(Adapt it in your code, I did not copy all your code to easier readability).
And in this last script you get access to this parameter with $_GET['id']. Then you can query your database for this one row only (with a WHERE clause).
Hope this help
Hi I am just a beginner with phpmysql and trying to display result in separate table by a row.
I have this query:
$albania = mysqli_query($con,"SELECT * FROM `data` ORDER BY citid ASC");
while($row = mysqli_fetch_array($albania))
I know that this will output all results onto 1 table. I want to put results onto separate tables dependent on the $row['citizenship']
I have about 40 different results in that row, so I want to separate it by that row and output separately.
If you order by the field you want to split on (citizenship) and keep track of the current value (done below with $currentCitizenship), then you should be able to just look for changes in that value and start a new table when the next section starts.
$albania = mysqli_query($con,"SELECT * FROM `data` ORDER BY citizenship ASC, citid ASC");
$currentCitizenship = ""
while($row = mysqli_fetch_array($albania)){
//handle the first time through the loop and start the first table
if ($currentCitizenship = ""){
//Start the first table
echo "<table>";
$currentCitizenship = $row['citizenship'];
}elseif ($currentCitizenship != $row['citizenship']){
//change to next citizenship table
echo "</table><table>";
$currentCitizenship = $row['citizenship'];
}
/*
Your current row printing code
*/
}
//close off the last table
echo "</table>"
What you wrote will not output anything yet. It will just fetch all rows from the database. You need to add code in PHP to diplay it in the screen.
Something like this. Need to put the correct php syntax:
$albania = mysqli_query($con,"SELECT * FROM `data` ORDER BY citid ASC");
while($row = mysqli_fetch_array($albania))
{
if($row['citizenship'] equals 'valuea')
print in one table
else
print in another table
}
i have a form and i am able to retrieve the data from database and display it.on display i used only 5 coloums to display and i want that if user click a specific row in it then new window should open which contains only data specified to that user.?? i have 20 columns in table and and id with auto increment?? how to display the data specified to a particular id clicked by user..?? when user first view the table with 5 columns and want more details he should click on that row and then data belonging to that id should be displayed only.!
You should have a separate page which will show the data for the entry you've selected, so:
First page will contain a db selection and a loop like:
$query = mysql_query("SELECT field_id, field_name, etc FROM table");
while($row = mysql_fetch_array($query)) {
echo '' . $row['field_name'] . '<br />';
}
show-info.php should have a db selection based on id in url:
$query = mysql_query("SELECT * FROM table WHERE field_id='" . mysql_real_escape_string($_GET['id']) . "'");
if(mysql_num_rows($query) == 0)
echo 'There is a problem showing you more info about this product';
else {
$row = mysql_fetch_array($query);
// do something with your extracted data
}
I'm sorry if this question frustrates anyone ... I am truly a beginner to PHP + MYSQL but I would love to make some progress on this one!
My Goal:
To create a table that I can view in a web page.
To be able to add data (rows) to this table and still have the data be there the next time the page is loaded.
Step 1: Creating the table
<html>
<body>
<?php
// Conect to MYSQL
$con = mysql_connect("localhost", "My_Username", "My_Password") or die(mysql_error());
echo "Connected to MYSQL </br>";
mysql_select_db("My_Database") or die(mysql_error());
echo "Connected to Database";
// Create a MySQL table in the selected database (called ExampleTable)
$sql = "CREATE TABLE ExampleTable
(
// Set the primary key (personID)
personID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(personID),
ColumnOne varchar(15)
ColumnTwo varchar(15)
ColumnThree varChar(30)
)";
// Execute query
mysql_query($sql,$con);
mysql_query("INSERT INTO ExampleTable(ColumnOne, ColumnTwo, ColumnThree)
VALUES ('Data1', 'Data2', 'Data3')");
mysql_query("INSERT INTO ExampleTable(ColumnOne, ColumnTwo, ColumnThree)
VALUES ('Data_Data1', 'Data_Data2', 'Data_Data3')");
mysql_close($con);
?>
</body>
</html>
So, I have now created a MYSQL table with two three columns, and two rows of data. How can I get this table to show up on a web page?
Step 2: Saving / Retrieving saved data
Is there some way that I can add data to a table, so that the data will be there permanently? - or is this how it works by default?
For example: Let's say that I have a form with a button on it. When the button is clicked a new row is added to the table 'ExampleTable'. The next time the user visits the page the table will be updated with his newly added data.
Thank you very much for any help. I understand that I am a beginner and do not fully understand these topics yet. Any responses will be greatly appreciated.
I'd recommend creating the table in a separate step, not from PHP; you don't want every time your web page is refreshed to create a new table.
Once your table is created, you can get the data from it by executing (within your PHP, using mysql_query) a query like "SELECT * from {tablename}". Once you've got that query result from mysql, then you can use the various PHP looping and mysql record reading methods to output the results from your query in the form you want into the page your PHP script will be serving to your client.
There are SELECT and UPDATE queries to do this. For example if you want to show data in a table, you would use query looking like this:
SELECT * FROM ExampleTable
In PHP, you can work with these data for example like this:
$query = mysql_query("SELECT * FROM ExampleTable");
while ($row = mysql_fetch_array($query)) {
echo $row["ColumnOne"];
}
And to the UPDATE query:
UPDATE ExampleTable SET ColumnOne = 'some value'
Usage in PHP is also with mysql_query. You can also use WHERE conditions in the query.
1 - Displaying All Data
<?php
// Conect to MYSQL
$con = mysql_connect("localhost", "My_Username", "My_Password") or die(mysql_error());
echo "Connected to MYSQL </br>";
mysql_select_db("My_Database") or die(mysql_error());
echo "Connected to Database";
// Get all the data from the "ExampleTable" table
$result = mysql_query("SELECT * FROM ExampleTable") or die(mysql_error());
echo "<table border='1'>";
echo "<tr> <th>ColumnOne</th> <th>CoumnTwo</th> <th>CoumnThree</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['ColumnOne'];
echo "</td><td>";
echo $row['ColumnTwo'];
echo "</td><td>";
echo $row['ColumnThree'];
echo "</td></tr>";
}
echo "</table>";
?>
From: http://www.tizag.com/mysqlTutorial/mysqlselect.php
2 - Data Storage
Anything INSERT-ed into a database will remain* in the database until it is explicitly deleted.
*with few exceptions - but you'll learn about them a little later in your database journey :)