Forum Moderators: coopster
$sql = "SELECT data FROM table WHERE STATUS =1";$result = mysql_query($sql, $dblink) or die("System down");
while ($newArray = mysql_fetch_array($result))
{
$propname = $newArray['propname'];
echo "$prop<br>";
}
As I have just taken on a new server with PHP5 I have plenty of select statements like the above so am busy testing to see what needs changing, but can't even get this simple select to work, thanks in advance
include("adminarea/config.php");
$dblink=mysql_connect($server, $user, $password)or die("System down!");
mysql_select_db($db, $dblink)or die("Database down!");$sleeps = $_POST['sleeps'];
$start_y = $_POST['start_year'];
$start_m = $_POST['start_month'];
$start_d = $_POST['start_day'];
$end_y = $_POST['end_year'];
$end_m = $_POST['end_month'];
$end_d = $_POST['end_day'];$start = "$start_y-$start_m-$start_d";
$end = "$end_y-$end_m-$end_d";if ($end < $start)
{
echo "Input Error<br><br>Your end date is earlier than your start date, please think before you hit the button!";
exit;
}
else
{
# do nothing
}
$diff = strtotime($end) - strtotime($start);
$difference = ceil($diff / (60*60*24)) ;
if I test print any of the above, they work until the $sql, if I print the $sql to see what its getting it prints fine, its just it wont print/echo propname in PHP5 but does in PHP4
$sql = "SELECT propname FROM table WHERE STATUS =1";
$result = mysql_query($sql, $dblink) or die("System down");
while ($newArray = mysql_fetch_array($result))
{
$propname = $newArray['propname'];
echo "$propname<br>";
}
mysql_close($dblink);
The $sql above I have chopped from my original for testing purposes and it calls a lot more details and would be harder to work with in learning what needs changing, here is my original incase it is wrong, although it works perfect
$sql = "SELECT propname, short_desc, sleeps, sleeps2, count( num ) AS days FROM bookings WHERE STATUS =1 AND $sleeps BETWEEN sleeps AND sleeps2 AND day_booked BETWEEN '$start' AND '$end' GROUP BY num";
Thanks for any help
Also, displaying array elements, might require a for statement..
My logic for this: your asking the prog to assign data to elemnts in an array called propname.
your then echoing propname... after the 1st instance of it, which element of the array would print?
for(int i=1;i<max_no;i++);
{ echo $propname[i]
}
As i say, firly new to PHP.. im more used to C++ and a wee bit of Java.
[edited by: Esqulax at 3:57 pm (utc) on Jan. 27, 2008]
Any issues you have are much more likely to be php configuration issues than actual coding ones. So far the only difference I've noticed with php5 is stronger type checking. For example:
$string = "1234";
$num = substr($string,2,2);
If you use $num as a parameter in a function call that expects a number, php5 will throw a warning; you solve by casting it to a number before using it:
$num = intval(substr($string,2,2));
You should also change all of these:
$start_y = $_POST['start_year'];
to
$start_y = intval($_POST['start_year']);
to protect against SQL injection attacks.
My burning question, though <grin>, how does this translate to yank/US: "(her indoors was calling)"
Esqulax,
echo "Some $variable text";
is perfectly legal, it's quite a bit more sophisticated than C's printf. Also, look closer - the way it's written, $propname isn't an array.
Some functions have been deprecated though in PHP5 and I believe the "global" modifier is also being phased out. If your code doesn't work in PHP5, your environment may have changed as well, which is causing your application to break. For instance, your sys admin may have turned off support for short tags (<?= ...?> vs. <?php echo ...?> or if you are utilizing any PEAR packages that haven't been installed yet.
The best thing is to reference PHP.net when you use a standard PHP function to see if it's been deprecated (or is only available in PHP 5.x). Then make sure your environment is consistent to how you previously had it set up.
$sleeps = intval($_POST['sleeps']);
or just
$_POST['sleeps'];
but I have been struggling all day with my image upload script, using the standard "file" type inputs.
To make sure everything was coming over to be processed I have tried numerous things, from $_POST, $_REQUEST, $_FILE to print_r to see what if anything is coming through, IF I make one of the inputs a normal "text" type it gets the details through fine.
Obviousley something needs to be done so the processing script can see the files.
What do I need to change?
[php.net...]
if you can't get at $_FILES then I'm not sure what is going on.
the strange thing about all of this is none of these issues really have anything to do with changing to 5, as cameraman mentioned above.
print_r ($_FILES);
And it prints an array for each input, which is good it means the details are getting through, heres the code that its passing to, which doesnt work anymore
flush();
$piccount=0;if ($propname=="")
{echo "Missing picture index".$back;die;}for ($i=1;$i<11;$i++)
{
$xpic=$i;
if ($i<10){
$i="0".$i."";
}if (($$xpic<>"none") and ($$xpic<>""))
{
$dest="../images/".$propname."-".$i.".jpg";
$sql = "insert into images values('', '$propname-$i.jpg', '$propname', '$i')";
if (mysql_query($sql, $dblink)){
echo "IMAGE added!<br><br>";
}else{
echo "Something was wrong<br><br>";
}If (move_uploaded_file($$xpic, $dest))
{
$log.=""; $piccount+=1;
chmod ($dest, 0644);
}
else
{
$log.="$dest upload error!($$xpic)<br>";
}
}
}
echo $log."<br>";
echo "Number of updated picures: ".$piccount."<br>";
echo $back;
Number of pictures comes back as a 0, can you spot anything obvious?
this is a hack, and not secure, but try this at the top of your code
extract($_FILES);
if it starts working then it is just a register_globals being off problem
what you then need to do is either change some of those vars to using $_FILES['elementname'] or assign values from the $_FILES array to local vars
$propname = $_POST['propname'];$dblink=mysql_connect($server, $user, $password)or die("System down!");
mysql_select_db($db, $dblink)or die("Database down!");while(list($key,$value) = each($_FILES[images][name]))
{
if(!empty($value))
{
$i = $key+1;
if ($i<10)
{
$i="0".$i."";
}
$add = "../images/".$propname."-".$i.".jpg";
$sql = "insert into images values('', '$propname-$i.jpg', '$propname', '$i')";
if (mysql_query($sql, $dblink)){
echo "IMAGE added!<br><br>";
}else{
echo "Something was wrong<br><br>";
}
copy($_FILES[images][tmp_name][$key], $add);
chmod("$add",0644);
}
}
Thanks
I have just found a problem with my select statements, they wont list how I want them to?
SELECT *
FROM images
WHERE propname = 'Margedale'
ORDER BY 'display_order'
It lists them as they are in the db, instead of propname-01 then 02 etc the db has 02 listed first and thats how they come out, ignoring ORDER BY? display_order is stored as a varchar
setcookie:
(this is a guess) try sticking a period in front of your domain:
setcookie( "favourites", "$user_id", time()+60*60*24*30, "/", ".domain.co.uk", "0" );
Technically, www.domain.co.uk is a subdomain of domain.co.uk - the leading period tells your browser that the cookie is ok for the domain's subdomains as well.
My upload form now looks in the db to see the number (property-01.jpg etc) I extract the 01 02 etc and disable those upload fields like this
$max_no_img=10;
$imagearray = array();$sql = "SELECT * FROM images WHERE propname = '$propname'";
$result = mysql_query($sql, $dblink) or die("System down");
while ($newArray = mysql_fetch_array($result)){
$image = $newArray['image'];
$image = explode(".", $image);
$image = explode("-", $image [0]);
$image = $image[1];
$imagearray[] = "$image";
}for($i=1; $i<=$max_no_img; $i++)
{
if ( in_array($i, $imagearray) )
{
echo "<tr><td>Don't use</td><td>";
}
else
{
echo "<tr><td>Images $i</td><td>";
}
if ( in_array($i, $imagearray) )
{
echo "<input type=file name='$i' disabled></td></tr>";
}
else
{
echo "<input type=file name='$i'></td></tr>";
}
}
The above works perfect and passes this
Array (
[1] => Array (
[name] => image.jpg [type] => image/pjpeg [tmp_name] => /tmp/phpGQ9boR [error] => 0 [size] => 15589 )
[6] => Array (
[name] => image.jpg [type] => image/pjpeg [tmp_name] => /tmp/phpGypkhP [error] => 0 [size] => 20466 )
[7] => Array (
[name] => [type] => [tmp_name] => [error] => 4 [size] => 0 )
[8] => Array (
[name] => [type] => [tmp_name] => [error] => 4 [size] => 0 )
[9] => Array (
[name] => [type] => [tmp_name] => [error] => 4 [size] => 0 )
[10] => Array (
[name] => [type] => [tmp_name] => [error] => 4 [size] => 0 ) )
I need [1] + [6] as they were the only fields that were used, 2 3 4 & 5 were disabled, using while(list( I can get the 1, 6, 7, 8, 9, 10 no problem HOW do I ignore the empty fields 7 8 9 & 10?
Thanks
foreach ($_FILES as $key=>$value)
{
if ($value[size] > '0')
{
$add = "../apartments/images/".$propname."-".$key.".jpg";$sql = "insert into images values('', '$propname-$key.jpg', '$propname', '$key')";
if (mysql_query($sql, $dblink)){
echo "IMAGE added!<br><br>";
}else{
echo "Something was wrong<br><br>";
}
copy($_FILES[$key][tmp_name], $add);
chmod("$add",0644);
}
}
One of the things that takes me and other newbies time is working out WHAT to use and WHERE to use it.
I now need to make sure that the user only uploads images of a certain size ie 400 pixels wide, I dont need to create thumbnails, just either check the size OR resize it, I think resizing would be the best option, check first, continue if OK or resize if not.
What do I use and where do I put it in my script, I just need a few pointers.
for resizing this works
Resizing a JPG [webmasterworld.com]
it's a thumbnail generator but you just need to use a larger size, the process is exactly the same
as far as where to put this stuff goes
you could do these tests wherever, you just need to roll everything back if one part fails