By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,664 Members | 1,623 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,664 IT Pros & Developers. It's quick & easy.

sql query error

100+
P: 114
Dear Friends,

I don't know I am not able to run this query. I am able to insert records but I don't know why I am not able to select and display the records....Please help me.......

[PHP]<?php

$hostname="mysql3.secureserver.net";
$username="mydb";
$password="mypassword";
$dbname="mydb";

mysql_connect($hostname,$username, $password) OR DIE ("Unable to connect to database! Please try again later.");
mysql_select_db($dbname);

$numfiles = $_POST['numfiles'];

$queryIns = ("INSERT INTO numfile(numfile) values('$numfiles')");

if(!@mysql_query ($queryIns)) {
echo mysql_error();
}
else{

$querySel = "SELECT * from numfile where id = (SELECT MAX(id) from numfile)"; // I am facing problem in this query
$result = mysql_query($querySel);
if($result)
{
while($row = mysql_fetch_array($result)){

echo "maximum id is : ".$row['numfile'];

}
}
else
{
echo "no records selected";
}

}

?>[/PHP]




Thanks
Deepak
May 8 '07 #1
Share this Question
Share on Google+
1 Reply


pbmods
Expert 5K+
P: 5,821
[PHP]
$numfiles = $_POST['numfiles'];
[/PHP]
As a sidenote, this should at least be:
[PHP]
$numfiles = addslashes($_POST['numfiles']);
[/PHP]

You don't want to know what might happen if $_POST['numfiles'] happens to contain "'; DROP TABLE `numfile`;".

[PHP]
$querySel = "SELECT * from numfile where id = (SELECT MAX(id) from numfile)"; // I am facing problem in this query
[/PHP]
Try:

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM `numfile` ORDER BY `id` DESC LIMIT 1;
  2.  
It's also faster ;)
May 8 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.