Putting MYSQL table info into html table - php

I'm making a guestbook, and I have a problem, that when I want my info from my mysql table to be placed into a table it only creates an empty table.
Code's here:
mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){
?>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td><table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td>ID</td>
<td>:</td>
<td><? echo $rows['id']; ?></td>
</tr>
<tr>
<td width="117">Vardas</td>
<td width="14">:</td>
<td width="357"><? echo $rows['name']; ?></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><? echo $rows['email']; ?></td>
</tr>
<tr>
<td valign="top">Komentaras</td>
<td valign="top">:</td>
<td><? echo $rows['comment']; ?></td>
</tr>
<tr>
<td valign="top"> Laikas </td>
<td valign="top">:</td>
<td><? echo $rows['datetime']; ?></td>
</tr>
</table></td>
</tr>
</table>
<?php }
mysql_close();
?>

If short tags aren't ON, <? echo $rows['id']; ?> will fail.
Either turn them on, or change to <?php echo $rows['id']; ?> and do the same for the others.
Footnotes
mysql_* functions deprecation notice:
http://www.php.net/manual/en/intro.mysql.php
This extension is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future. Instead, either the mysqli or PDO_MySQL extension should be used. See also the MySQL API Overview for further help while choosing a MySQL API.
These functions allow you to access MySQL database servers. More information about MySQL can be found at » http://www.mysql.com/.
Documentation for MySQL can be found at » http://dev.mysql.com/doc/.

Update your code like;
while($rows=mysql_fetch_array($result)){
to
while($rows=mysql_fetch_array($result, MYSQL_ASSOC)){
You need to use associative array. Also this library is deprecated. Use Mysqli or PDO

Related

why am i getting Resource id #5 error in mysql_query? [duplicate]

I have a pretty basic forum template I am working on for testing purposes
When I create a topic, and press submit, the proccess updates the database but doesn't output on the screen. Why is this? and Why am I getting a Resource id #4 when I echo the $result from this code below:
<?php
$host="server"; // Host name
$username="usernamehere"; // Mysql username
$password=""; // Mysql password
$db_name="forum"; // Database name
$tbl_name="question"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
// OREDER BY id DESC is order result by descending
$result=mysql_query($sql);
echo $result;
?>
<html>
<body>
<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td width="6%" align="center" bgcolor="#E6E6E6"><strong>#</strong></td>
<td width="53%" align="center" bgcolor="#E6E6E6"><strong>Topic</strong></td>
<td width="15%" align="center" bgcolor="#E6E6E6"><strong>Views</strong></td>
<td width="13%" align="center" bgcolor="#E6E6E6"><strong>Replies</strong></td>
<td width="13%" align="center" bgcolor="#E6E6E6"><strong>Date/Time</strong></td>
</tr>
<?php
// Start looping table row
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['topic']; ?><BR></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['view']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['reply']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['datetime']; ?></td>
</tr>
<?php
// Exit looping and close connection
}
mysql_close();
?>
<tr>
<td colspan="5" align="right" bgcolor="#E6E6E6"><strong>Create New Topic</strong> </td>
</tr>
</table>
</body>
</html>
You are getting resource id #4 because $result is an resource,you must extract the values contained in it by this way,
$result=mysql_query($sql);
$values = mysql_fetch_array($result);
var_dump($values);
More about resource variable
Update 2(From OP comments)
You are printing values using field name,In that case you will have to change to
while($rows=mysql_fetch_array($result,MYSQL_ASSOC))
Or you can directly use mysql_fetch_assoc(),which in your case will be
while($rows=mysql_fetch_assoc($result)){
echo $rows['id'];
}
Problem is in your code:
$result=mysql_query($sql);
echo $result;
$result is resource type, since mysql_query($sql) returns resource
Stop echoing $result.
If you check the link - http://php.net/manual/en/function.mysql-query.php
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error
Hence you are seeing the Resource#4
. What is it you want to achieve?
<?php
$host="server";
$username="usernamehere";
$password="";
$db_name="forum";
$tbl_name="question";
mysql_connect("$host", "$username", "")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
// OREDER BY id DESC is order result by descending
$result=mysql_query($sql);
echo $result; //remove it
?>
<html>
<body>
<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td width="6%" align="center" bgcolor="#E6E6E6"><strong>#</strong></td>
<td width="53%" align="center" bgcolor="#E6E6E6"><strong>Topic</strong></td>
<td width="15%" align="center" bgcolor="#E6E6E6"><strong>Views</strong></td>
<td width="13%" align="center" bgcolor="#E6E6E6"><strong>Replies</strong></td>
<td width="13%" align="center" bgcolor="#E6E6E6"><strong>Date/Time</strong></td>
</tr>
<?php
// Start looping table row
while($rows=mysql_fetch_array($result)){
echo"<tr>
<td bgcolor=#FFFFFF>$rows['id']</td>
<td bgcolor=#FFFFFF><a href='view_topic.php?id=$rows['id']'>$rows['topic']</a><BR></td>
<td align=center bgcolor=#FFFFFF>$rows['view']</td>
<td align=center bgcolor=#FFFFFF>$rows['reply']</td>
<td align=center bgcolor=#FFFFFF>$rows['datetime'];</td>
</tr>";
// Exit looping and close connection
}
mysql_close();
?>
<tr>
<td colspan="5" align="right" bgcolor="#E6E6E6"><strong>Create New Topic</strong> </td>
</tr>
</table>
</body>
</html>
just check above code i think your problem should be done
You don't have to use mysql_fetch_array(). If you want, try something like this:
$sql="SELECT * FROM $tbl_name ORDER BY id DESC"; //that's your query
$result=mysql_query($sql);
$rows=mysql_num_rows($result);
$iteration=0;
echo "<table>";
while($iteration < $rows){
$cell_in_your_html_table = mysql_result($result , $iteration , 'column_name_from_database');
echo "<tr><td>".$cell_in_your_html_table."</td></tr>";
$iteration++;
}
echo "</table>"

mysql_fetch_array is returning blank array, but correct number of rows

I am using a while loop to parse through my database, and I know that the information is there. It is a forum style setup, and after I add a topic and return to the main page, the new line is added, but still blank. I will include my code, I'm not quite sure what I am missing that would cause it to not bring the data from the DB. Also, I am not using PHPMyAdmin, and I believe this tutorial code was originally written for use with it, if this helps address the difference in syntax I might need to solve the problem. Thanks in advance.
mysql_connect("$host","$username","$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name ORDER BY id DESC;";
//order result by descending
$result=mysql_query($sql);
?>
<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td width="6%" align="center" bgcolor="#E6E6E6"><strong>#</strong></td>
<td width="53%" align="center" bgcolor="#E6E6E6"><strong>Topic</strong></td>
<td width="15%" align="center" bgcolor="#E6E6E6"><strong>Views</strong></td>
<td width="13%" align="center" bgcolor="#E6E6E6"><strong>Replies</strong></td>
<td width="13%" align="center" bgcolor="#E6E6E6"><strong>Date/Time</strong></td>
</tr>
<?php
//start looping table row
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td bgcolor="#FFFFFF"><? echo $rows['id'];?></td>
<td bgcolor="#FFFFFF"><a href="view_topic.php?id=<? echo $rows['id'];?>"><?echo $rows['topic']; ?>
</a><BR></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['view']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['reply']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['datetime']; ?></td>
</tr>
<?php
// Exit looping and close connection
}
mysql_close();
?>
Test this : replace <? by <?php
So <? echo $rows['view']; ?> by <?php echo $rows['id']; ?> idem view, reply, datetime

For loop fetch array in php

I want to delete mySQL rows with checkboxes. This is a code that should work according to someone on the internet, but some reason it doesn't for me. When I click delete it only refreshes but the row doesn't disappear. Has this something to do with my ID in the table?
<body>
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="*****"; // Mysql password
$db_name="test"; // Database name
$tbl_name="deviation"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">Åtgärda</td>
<td align="center" bgcolor="#FFFFFF"><strong>Chassinummer</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Problem detail</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Fault code</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Position</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Help object</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Operation step</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFFF"><?php echo $rows['chassi']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['problem_detail']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['fault_code']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['fault_code']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['position']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['help_object']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['operation_step']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
<?php
**$checkbox = $_POST['checkbox'];**
**$delete = $_POST['delete'];**
// Check if delete button active, start this
if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
**$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";**
$result = mysql_query($sql);
}
// if successful redirect to delete_multiple.php
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=deletetable.php\">";
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
</body>
There are certain things I want to direct your attention towards:
1) There are a number of lines in PHP enclosed in nonsensical double-asterisks. I'm shocked the PHP actually ran through those.
2) You have a $result defined within the global scope at the top of your script (where it was given a resource pointer from mysql_query()). This means that at the bottom, where you check for if($result), that check will always come to true (unless there were syntactic errors). This also means that the page will always refresh when you click submit regardless of whether the deletion actually happened.
As I've mentioned in the comments, that code is (frankly speaking) a piece of crap. It's vulnerable to SQL injection, the code doesn't actually do what it's supposed to do, it's using deprecated attributes and functions... The failure of the code to do what you want isn't because of the IDs in your table, it's because of the broken, non-functioning code you copied off the Internet.
Don't iterate over whole table
Replace this
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
**$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";**
$result = mysql_query($sql);
}
with
if(isset($_POST['checkbox']) && is_array($_POST['checkbox')){
foreach($_POST['checkbox'] as $id){
$id = (int)$id;
$sql = "DELETE FROM $tbl_name WHERE id='$id'";
$result = mysql_query($sql);
}
}

PHP - Unable to find variable

I am trying to implement a UI which the user would be able to select entries by selecting checkboxes to delete them from the database. However, while everything looks fine and able to display, there is an error saying Notice: Undefined variable: delete in /opt/uiForm.php on line 154 whereas $delete is not readable. I am following the example given here. Am I missing anything?
<body>
<form name="frm" method="get" action="doSubmit();">
<?php
// Connect to server and select database.
mysql_connect("127.0.0.1:3306", "root", "")or die("cannot connect");
mysql_select_db("PushApplication")or die("cannot select DB");
$sql="SELECT * FROM Device";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>ID</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>DeviceID</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>DeviceType</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Description</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>OS_Version</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Status</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['ID']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo $rows['ID']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['DeviceID']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['DeviceType']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['Description']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['OS_Version']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['Status']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="7" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
<?php
// Check if delete button active, start this
if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM Device WHERE ID='$del_id'";
$result = mysql_query($sql);
}
// if successful redirect to delete_multiple.php
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_multiple.php\">";
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
</form>
</body>
</html>
You really should be using
if (!empty($_POST['delete'])) {
instead of
if ($delete) {
to avoid the notice you are getting. Also your code is prone to the so called SQL injection vulnerability which you read upon if you are considering to put it somewhere on the web.
Also you should be using method="post" instead of GET for this type of operations.
I suspect the other example was not displaying all errors.
When checking for the existence of a variable, you need to use if(!empty($delete)), because if returns an error when the variable it's checking does not exist. empty() does not.
Your code supposes that server's PHP has option register_globals = On; Which is not true by default for the late versions of PHP setup. Read this What are register_globals in PHP?

What is the problem with this php code?

This the simple php code I am using to view contents of a forum. The problem is , it's working fine in one of my laptop but in second it do not show the output.
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
// OREDER BY id DESC is order result by descending
$result=mysql_query($sql);
?>
<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td width="6%" align="center" bgcolor="#E6E6E6"><strong>#</strong></td>
<td width="53%" align="center" bgcolor="#E6E6E6"><strong>Topic</strong></td>
<td width="15%" align="center" bgcolor="#E6E6E6"><strong>Views</strong></td>
<td width="13%" align="center" bgcolor="#E6E6E6"><strong>Replies</strong></td>
<td width="13%" align="center" bgcolor="#E6E6E6"><strong>Date/Time</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){ // Start looping table row
?>
<tr>
<td bgcolor="#E6E6E6"><? echo $rows['id']; ?></td>
<td bgcolor="#E6E6E6"><? echo $rows['topic']; ?><BR></td>
<td align="center" bgcolor="#E6E6E6"><? echo $rows['view']; ?></td>
<td align="center" bgcolor="#E6E6E6"><? echo $rows['reply']; ?></td>
<td align="center" bgcolor="#E6E6E6"><? echo $rows['datetime']; ?></td>
</tr>
I checked everything else and they seems fine. Data is present in the database but the it's not showing in forum. Can anybody help me in this?
operating sys : win7
time to learn debugging, pal.
Good place to start: http://www.ibm.com/developerworks/library/os-debug/
for mysql it's extremely handy to run queries this way:
$result=mysql_query($sql) or trigger_error(mysql_error()." ".$sql);
and make sure you can see all errors occurred.
if you can't as a quick fix you can add these lines at the top of the script
ini_set('display_errors',1);
error_reporting(E_ALL);
but for the production state display_errors value should be changed to 0
It is also good practice to check HTML source instead of watching rendered page in the browser. It will tell you if you have any problem with short tags or not. It's always good to know if you have any problem before going to fix it.
you should use
<?php echo $rows['id']; ?> instead of <? echo $rows['id']; ?>
short tag may be disabled.

Categories