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.
Related
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>"
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
Ok Before you tell me to use mysqli I am using the depreciated methods on purpose for a webapp lesson. I am not a student, this is not homework, it is to help me teach an understanding for web application security.
I cannot figure out why this wont work. Basically, All I want to do is create a page that takes the data from the mysql database and places it into a table as shown. At this point I am open to anything.
Thank you in advance.
<html>
<head><title>Untitled</title></head>
<body>
<h1>Weblog Example</h1>
<dl>
<?php
mysql_connect("localhost","root","");
mysql_select_db("blog1");
$query ="SELECT entrytitle, entrytext,";
$query.=" DATE_FORMAT(entrydate, '%M %d, %Y') AS entrydate";
$query.=" FROM weblog ORDER BY entrydate DESC LIMIT 10";
$result=mysql_query($query);
?>
<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td width="15%" align="center" bgcolor="#E6E6E6"><strong>Title</strong></td>
<td width="75%" align="center" bgcolor="#E6E6E6"><strong>Entry</strong></td>
<td width="15" align="center" bgcolor="#E6E6E6"><strong>Date/Time</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['entrytitle']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['entrytext']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['entrydate']; ?></td>
</tr>
<?php
}
?>
</dl>
</body>
</html>
Ok- made some minor edits- it now gives me 3 rows in the table but doesn't populate the data...
Do a var_dump on $entrytitle and $entrytext and you'll understand you error.
Data is temporary stored into $rows when you do a mysql_fetch_array.
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><? echo $rows["entrytitle"]; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows["entrytext"]; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows["entrytext"]; ?></td>
</tr>
<?php
}
?>
You're using the wrong variable in your while loop, you're also not referencing the correct date column from your query result.
This should give you what you're looking for:
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><?php echo $rows['entrytitle']; ?></td>
<td align="center" bgcolor="#FFFFFF"><?php echo $rows['entrytext']; ?></td>
<td align="center" bgcolor="#FFFFFF"><?php echo $rows['entrydate']; ?></td>
</tr>
<?php
}
?>
Both your $query.= should read as $query .= that alone will be an issue and will not concatenate properly because of the missing space before the dots.
You're also missing a </table> tag.
Plus, make sure that short tags are set/on, otherwise do <?php echo $rows...
instead of <? echo $rows....
You should also check for possible query errors using:
$result = mysql_query($query) or die(mysql_error());
and using error reporting.
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Error reporting should only be done in staging, and never production.
I also suggest you switch to mysqli with prepared statements, or PDO with prepared statements, they're much safer.
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
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);
}
}