Forum Moderators: coopster

Message Too Old, No Replies

Writing for PHP5

Current code does'nt work in PHP5

         

wheelie34

12:35 pm on Jan 27, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This code produces results in PHP4 but not in PHP5, what do I need to change?

$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

jatar_k

1:07 pm on Jan 27, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



it shouldn't work in either

$sql = "SELECT data FROM table WHERE STATUS =1";

that says you are calling a column called 'data'

and this '$propname = $newArray['propname']; ' is trying to grab a column called 'propname' which doesn't seem to have been queried

henry0

1:53 pm on Jan 27, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



also =1 to be ='1'

jatar_k

2:48 pm on Jan 27, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



true but that would depend on the column type

Esqulax

3:01 pm on Jan 27, 2008 (gmt 0)

10+ Year Member



What would you expect the output to be?
SELECT * FROM tablename
etc.. saves time, if the table is like real small

Looks like $prop isnt defined anywhere...

also

echo $propname,'<br>' ;

perhaps?

[edited by: Esqulax at 3:03 pm (utc) on Jan. 27, 2008]

henry0

3:17 pm on Jan 27, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



true but that would depend on the column type

Yes indeed did not think enough...
One is not supposed to use quote if is an int

any other reason?

wheelie34

3:46 pm on Jan 27, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



damn the code I posted was wrong, as you all spotted, I edited it for posting purposes, heres the correct code, sorry for the confusion chaps, I was in a hurry (her indoors was calling) this is the data coming to the page and how I accept it.


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

Esqulax

3:51 pm on Jan 27, 2008 (gmt 0)

10+ Year Member



i could be wrong, as im fairly new to PHP,
but should your $propname in your echo be outside the inverted commas?
i thought hat everything in echo 'Here was plaintext or<html>',$variables_were_here);

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]

wheelie34

3:57 pm on Jan 27, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi
I thought that was personal prefference, it works the same either way AFAIK

I just tested it with '$propname' it 5 it made no difference, in 4 I got a page of $propname instead of what propname should be, changed it back to "" and I got the properties listed again in 4 still nothing in 5

Little_G

4:18 pm on Jan 27, 2008 (gmt 0)

10+ Year Member



Hi,

What is

echo "$propname<br>";
actually echoing?
Are you getting a <br> with nothing in front of it or nothing at all?

ps. The SQL in your test code references the table 'table' whereas your original refs 'bookings', is this correct?

Andrew

[edited by: Little_G at 4:21 pm (utc) on Jan. 27, 2008]

wheelie34

4:26 pm on Jan 27, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have just been told the server (which was upgraded yesterday to PHP5 is having php issues that they are looking into it) BUT I still don't know if any of my above code needs changing, does it all look OK, it works perfect in PHP4 and I was hoping it would in 5

cameraman

5:38 pm on Jan 27, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



wheelie34, I don't see any technical problems with your php. You may want to remove these single quotes:
BETWEEN '$start' AND '$end' GROUP

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.

phnord

5:40 pm on Jan 27, 2008 (gmt 0)

10+ Year Member



Most of the changes from PHP4 to 5 have to do with PHP's increased supported for object oriented programming. For instance, the addition of the public, private and protected modifiers and changes in how objects are referenced (i.e. pass by value vs. pass by reference)...amongst other things. Since you don't seem to be utilizing PHP's object support, then I think you should be OK if you say your code works with PHP4.

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.

henry0

7:00 pm on Jan 27, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$start_y = intval($_POST['start_year']);
be advised that it only (as in the above $_POST) works with an integer

NB
cameraman: I am wondering too about its meaning..:)?

wheelie34

7:36 pm on Jan 27, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



thanks guys.

Ans: her indoors is the wife ;)

Esqulax

2:03 pm on Jan 28, 2008 (gmt 0)

10+ Year Member



Thanks Cameraman.
Im beginning to quite like PHP, as it seems a bit more forgiving than C :p

wheelie34

8:28 pm on Jan 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK most of my forms are working now in PHP5 by using either


$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?

jatar_k

10:49 pm on Jan 28, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



is it a misprint that you used $_FILE? beause it's $_FILES

[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.

wheelie34

9:46 am on Jan 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi jatar_k I tried

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?

jatar_k

3:40 pm on Jan 29, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I don't see that code pulling anything out of the $_FILES array

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

wheelie34

6:33 pm on Jan 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hey ho
I actually worked it out, it took all day so far, yes I see what you mean about the array, heres what I have come up with, it works how I want it, is it ok?

$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

jatar_k

6:53 pm on Jan 29, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



that's exactly what I was talking about

nice work

wheelie34

7:24 pm on Jan 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thank you

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

cameraman

9:11 pm on Jan 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try removing the single quotes surrounding display_order.

wheelie34

9:19 am on Jan 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks cameraman it worked, but why? is that a PHP5 thing now

wheelie34

12:05 pm on Jan 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



next step is my cookies, heres the original code, it isn't dropping the test cookie on my machine?

setcookie( "favourites", "$user_id", time()+60*60*24*30, "/", "domain.co.uk", "0" );

$user_id is generated by looking at the db to see the 'next' available id

Anything obvious?

cameraman

7:06 pm on Jan 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



display_order:
You probably didn't realize that it wasn't working (it's a mysql thing, not php) - the records don't start getting mixed about until mysql is optimizing space left over from record deletions. The quotes that you had were telling mysql to order by the literal string "display_order" - removing them made it clear that it was the field name, not literal text.

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.

wheelie34

1:47 pm on Feb 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi
I have just found a problem with my image upload script, when creating the FILES boxes I was using images[] to send an array BUT (the problem) IF you upload 3 images all is well, if you remove the 3rd image to replace it with another it will still work as it will still be number 3, BUT if the first image is deleted and another uploaded it would become number 3 instead of number 1.

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

wheelie34

2:18 pm on Feb 13, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ok I have managed to pull out only the $_FILES I needed using this.


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.

jatar_k

4:31 pm on Feb 13, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



try this thread for a look at how you can do comparisons using getimagesize() [webmasterworld.com]

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

This 34 message thread spans 2 pages: 34