I'm trying to make a code that permits the user to show any table in my database in a php page.
I tried with that code:
<?php
include('connection_db.php');
$table = $_POST['table'];
$sql1 = "SELECT * FROM $table";
$sql2 = "SELECT count(*)
FROM information_schema.columns
WHERE table_name = '$table'";
$sql3 = "SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = N'$table'";
$result = mysql_query($sql1);
$column = mysql_query($sql2);
$ncolumn= mysql_query($sql3);
echo "<table width='100%' border='1'>";
while ($row = mysql_fetch_array($result))
{
echo "<tr>";
for ($i = 0; $i <= $column; $i++) {
echo "<td>". $row['$ncolumn'] ."</td>";
}
echo "</tr>";
echo "<br/>";
}
echo "</table>";
?>
It should show the entire table with his records, but it shows only a lot of empty lines
$table = $_POST['table'];
$mysqli = new mysqli(_DB_SERVER_,_DB_USER_,_DB_PASSWD_,_DB_NAME_);
$mysqli->set_charset("utf8");
if (mysqli_connect_errno()) {
$Message = "Connect failed: %s\n" . $mysqli->connect_error;
exit();
}
if ($result = $mysqli->query("SHOW TABLES LIKE '".$table."'")) {
if($result->num_rows == 1) {
$Tableexists = "yes";
} else {
$Tableexists = "no";
}
} else {
$Tableexists = 0;
}
echo $Tableexists;
I have upgrade my code. In the old code I had 2 functions: display_maker_success() and display_maker_fail() but I realised I can combine those two functions into one display_maker_stat() by putting more arguments into the function. I like it very much!
Is better way to do this? I want more code reuse.
function display_maker_success($link, $userid){
$status="closed";
$result="completed";
$sql = "select start, name from wuuk where tasker_id ='$userid' and status ='$status' and result ='$result' order by id desc LIMIT 6;";
$result = mysql_query($sql, $link);
$isempty=mysql_num_rows($result);
If ($isempty ==0) {
echo "No Record";
} else {
echo "<table border=1>";
echo "<tr><th>Date & Time</th><th>Name</th><th>Status</th></tr>";
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "<tr><td>$row[0]</td><td>$row[1]</td><td>Completed</td></tr>";
};
echo "</table>";
};
};
function display_maker_fail ($link, $userid) {
$status="closed";
$result="fail";
$sql = "select start, name from wuuk where tasker_id ='$userid' and status ='$status' and result ='$result' order by id desc LIMIT 1;";
$result = mysql_query($sql, $link);
$isempty=mysql_num_rows($result);
If($isempty ==0){
echo "No Record";
} else {
echo "<table border=1>";
echo "<tr><th>Date & Time</th><th>Name</th><th>Status</th></tr>";
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "<tr><td>$row[0]</td><td>$row[1]</td><td>fail</td></tr>";
};
echo "</table>";
};
};
function display_maker_stat ($link, $userid, $reuslt, $limit) {
$status="closed";
$result="fail";
$sql = "select start, name from wuuk where tasker_id ='$userid' and status ='$status' and result ='$result' order by id desc LIMIT 1;";
$result = mysql_query($sql, $link);
$isempty=mysql_num_rows($result);
If($isempty ==0){
echo "No Record";
} else {
echo "<table border=1>";
echo "<tr><th>Date & Time</th><th>Name</th><th>Status</th></tr>";
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "<tr><td>$row[0]</td><td>$row[1]</td><td>$result</td></tr>";
};
echo "</table>";
};
};
Try the below,
Also there were few errors in your code and i have corrected them.
function display_maker_stat($link, $userid, $reuslt = 'fail', $limit)
{
$status = "closed";
$html = '';
$sql = "select start, name from wuuk where tasker_id ='$userid' and status ='$status' and result ='$result' order by id desc LIMIT 1;";
$query = mysql_query($sql, $link);
if (mysql_num_rows($query) != 0) {
$html .= "<table border=1>";
$html .= "<tr><th>Date & Time</th><th>Name</th><th>Status</th></tr>";
while ($row = mysql_fetch_array($query, MYSQL_NUM)) {
$html.= "<tr><td>$row[0]</td><td>$row[1]</td><td>$result</td></tr>";
}
$html.= "</table>";
echo $html;
}
else {
echo "No Record";
}
}
Read about OOP
$i = 1;
$sql = "
SELECT
heroes.char_id, characters.char_name, heroes.class_id, heroes.count,
heroes.played, heroes.active FROM heroes, characters
WHERE characters.obj_Id = heroes.char_id AND heroes.played = 1
";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query))
{
echo "
<tr>
<td><span id='lefttop'><b><font color='#007aa2'>".$i++." </font></td><td><b><font color='#f6ff00'>".$row['char_name']."</font></td></span><div style='float:right;'><td><b> ".$row['count']."</td></div> <br />
</tr>
";
}
echo "";
?>
Anyone can help? im trying to make a hero status script on a java server, and i need to connect into 2 tables which is "heroes"(this is where i get the hero) and "character" (this is where i get the hero names)
Try this Temple:
<?php
$sql1 = "SELECT * your table1";
$query1 = mysql_query($sql);
while($row1 = mysql_fetch_array($query1))
{
echo "<tr>";
$sql2 = "SELECT * your table2";
$query2 = mysql_query($sql2);
while($row2 = mysql_fetch_array($query2))
{
echo "<td>[yor Vars here]</td>";
}
echo "</tr>";
}
?>
<?php
session_start();
$link = mysqli_connect("localhost", "xxx", "xxxxxx", "xxx");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$id = $_GET['id'];
if ($result = mysqli_query($link, "SELECT * FROM Subscribe WHERE STo = '".$id."' ORDER BY ID LIMIT 6")) {
while($row = mysqli_fetch_array($result))
{
$idS = $row['SWho'];
echo $idS;
if ($result = mysqli_query($link, "SELECT * FROM accounts WHERE ID = '".$idS."' ORDER BY ID LIMIT 6")) {
while($row = mysqli_fetch_array($result))
{
echo "<img src='Member_ProfilePics/";
echo $row['PP'] . '.' . $row['Ext'];
echo "' width=42 height=40 style='float: left'>";
}
}
}
//<img src='Member_ProfilePics/john.jpg' width=42 height=40 style='float: left'>
}
?>
here in my code, i want to output the list of STo. and connect to other database and output the following rows in each STo.
Sample to be output:
(picture of id=1) 1
(picture of id=2) 2
(picture of id=3) 3
(picture of id=4) 4
(picture of id=5) 5
(picture of id=6) 6
How?
my code is not looping.
Sample output of my code:
(picture of id=1) 1
You are using the same variables for both the loops:
if ($result = mysqli_query($link, "SELECT * FROM Subscribe WHERE STo = '".$id."' ORDER BY ID LIMIT 6")) {
while($row = mysqli_fetch_array($result)) {
....
if ($result = mysqli_query($link, "SELECT * FROM accounts WHERE ID = '".$idS."' ORDER BY ID LIMIT 6")) {
while($row = mysqli_fetch_array($result)) {
...
}
}
}
}
Try to change the seconda value to these
if ($result = mysqli_query($link, "SELECT * FROM Subscribe WHERE STo = '".$id."' ORDER BY ID LIMIT 6")) {
while($row = mysqli_fetch_array($result)) {
....
if ($result2 = mysqli_query($link, "SELECT * FROM accounts WHERE ID = '".$idS."' ORDER BY ID LIMIT 6")) {
while($row2 = mysqli_fetch_array($result2)) {
...
}
}
}
}
if(isset($_REQUEST['SAVE'])) {
$sql = "SELECT SecName FROM section WHERE SecID='$section'";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)){
$SecName = $row["SecName"];
}
$sql = "SELECT SubjName FROM subject WHERE SubjID='$subject'";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)){
$SubjName = $row["SubjName"];
}
$check = mysql_query("SELECT SecID FROM service where SubjID='$SubjName'")or die(mysql_error());
$bool = 1;
while($info = mysql_fetch_array( $check ))
if(($info['SecID'] == $SecName)){
$bool = 0;
}
if($bool){
$check = mysql_query("SELECT * FROM service ")or die(mysql_error());
$bool = 1;
while($info = mysql_fetch_array( $check ))
if(($info['Start_date'] == $Start_date) && ($info['Venue'] == $Venue) || ($info['Start_date'] == $Start_date) && ($info['Facilitator'] == $Facilitator)){
$bool = 0;
}
if($bool){
$sql="INSERT INTO service ( ServiceID, Date, Semester, School_year, Start_date, Venue, Facilitator, Stype, SecID, SubjID)VALUES('$RecCode','$Date','$Semester','$School_year','$Start_date','$Venue','$Facilitator','$Stype', '$SecName', '$SubjName')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo '<script type="text/javascript">';
echo 'alert("Save Successfully!");';
echo 'window.location="Admin_RecSchedMapLst.php";';
echo '</script>';
mysql_close($con);
}else
{
echo '<script type="text/javascript">';
echo 'alert("Conflicting schedule for Venue or Facilitator!");';
echo '</script>';
}
}else
{
echo '<script type="text/javascript">';
echo 'alert("The SECTION has been already scheduled!");';
echo '</script>';
}
}
/* $result = mysql_query("SELECT SIDno FROM class WHERE SecID=$SecID AND SubjID=$SubjID");
$row = mysql_fetch_assoc($result);
$SIDno = $row['SIDno'];
$result = mysql_query("SELECT ServiceID FROM service WHERE SecID=$SecID AND SubjID=$SubjID");
$row = mysql_fetch_assoc($result);
$ServiceID = $row['ServiceID'];
$sql="INSERT INTO registered ( ServiceID, IDno, Stype )VALUES('$ServiceID','$SIDno','$Stype')"; */
}
this code is working correctly,. i just want to ask how can i possibly work on the code whcih is commented above. all i want to do is when the serviceid is inserted i want also to immediately insert the serviceID, idno, type to another table which is registered.
It's much easier and better to use the mysql_insert_id function to get the ID of the row you just inserted, rather than doing another SELECT. E.g.:
$sql="INSERT INTO service ( ServiceID, Date, Semester, School_year, Start_date, Venue, Facilitator, Stype, SecID, SubjID) VALUES ('$RecCode','$Date','$Semester','$School_year','$Start_date','$Venue','$Facilitator','$Stype', '$SecName', '$SubjName')";
if (mysql_query($sql,$con)) {
$ServiceID = mysql_insert_id();
$sql="INSERT INTO registered ( ServiceID, IDno, Stype )VALUES('$ServiceID','$SIDno','$Stype')";
mysl_query($sql, $con); // check for error here too though
}
else {
echo "OH NO!!!";
}
Also, beware of SQL injection with the way you're building your SQL strings.
Execute another SQL INSERT command after the first one, like:
$sql="INSERT INTO service ( ServiceID, Date, Semester, School_year, Start_date, Venue, Facilitator, Stype, SecID, SubjID) VALUES ('$RecCode','$Date','$Semester','$School_year','$Start_date','$Venue','$Facilitator','$Stype', '$SecName', '$SubjName')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
else
{
/*Begin your SELECT and INSERT code*/
$result = mysql_query("SELECT SIDno FROM class WHERE SecID=$SecID AND SubjID=$SubjID");
$row = mysql_fetch_assoc($result);
$SIDno = $row['SIDno'];
$result = mysql_query("SELECT ServiceID FROM service WHERE SecID=$SecID AND SubjID=$SubjID");
$row = mysql_fetch_assoc($result);
$ServiceID = $row['ServiceID'];
$sql2 = "INSERT INTO registered ( ServiceID, IDno, Stype )VALUES('$ServiceID','$SIDno','$Stype')";
/*End your SELECT and INSERT code*/
if(!mysql_query($sql2,$con))
{
die('Error: ' . mysql_error());
}
}