an odd mysql_num_rows() and mysql_fetch_array() error

Have have similar code to below on multiple pages on my site. This code was referenced from php.net and other site and tutorials as a way of looking through a mysql table. This code has worked then not worked then worked again… with no changing of the code.

The code

[php]
$sql_len1 = mysql_num_rows($result);
for ($i = 0; $i <= $sql_len1; $i++) {
$row = mysql_fetch_array($result);
if (stripslashes($row[‘photo_num’]) > $photo_num) {
$photo_num = stripslashes($row[‘photo_num’]);
}
}
[/php]

The code for $result is as follows:

[php]
$db = mysql_connect(“localhost”, “user”, “pass”);
mysql_select_db(“user_photoalbum”, $db);
$result = mysql_query(“SELECT * from photo”, $db);
[/php]

Now when I run this I will get an error like this

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

Can you help for why this works then doesn’t and how to get around that?

Thank you for your help.

Firstly, make sure that your photo table exists. :slight_smile:

So i tried that and the mysql_fetch_array() error went away, but the other remains. Any thoughts?

Also can you explain why you had me do those steps; I would really like to learn this language.

Thank you again

If the mysql_num_rows error remains, then there is an error in your syntax.

Change:

$result = mysql_query(“SELECT * from photo”);

to:

$result = mysql_query(“SELECT * from photo”) or die(mysql_error());

See if that gives you an error message.

As for changing what I did, well firstly, you are making the connection in the first part of your code, so there is no need to add the $db variable to the query. The mysql_query() function is just that, the query.

mysql_fetch_array() returns your result set as an associative array. This you display always using a while() loop. Its the easiest way.

If you are only fetching one row of data, you would use mysql_fetch_row or the one I always use mysql_fetch_object.

There are other ways of getting data from a database, but these are the most common.

:)

Well you are being very helpful, so I have another question.

I am using this function

[php]
ftp_put($conn_id, $file_name, $new_file, FTP_ASCII)
[/php]

And it keeps returning FALSE. Any idea why or any idea how I could test for why?

Thank you again

That I`m afraid I cant help you with, its something I very rarely use.

Sorry.

Just use fopen() in write mode and specify the path to the file with full FTP connection string/credentials and you can write the file in place. I used FTP functions before but they are a little too complex for such a simple task. I would only use them if I had to list files or something like that.

I have made some code with fopen() but am getting an error

Warning: fopen(H:\Pictures\San Juan.jpg): failed to open stream: No such file or directory in /home/user/public_html/editalbum/script/add_event.php on line 65

What I am trying to do is have a user upload files to the server. Can I open a file on the users box to have that copied over?

My code:

[php]

if ( !($out = fopen($file_name, r)) ) {
echo “File “. $file_name.” could not be opened”;
} // end if
if ( !($in = fopen($new_file, w) ) ) {
echo “File “.$new_file.” could not be opened”;
} // end if
else {
for ($inchar = fgetc($out); $inchar != EOF; $inchar = fgetc($out) ) {
if ( !(fwrite($in, $inchar) ) ) {
echo “Unable to upload
”;
} // end if
}// end for
} // end else

[/php]

$file_name is the entire extension. Like in this example as shown in the error above.

Thank you for your help

carella

I did what you suggested with the mysql_num_rows() and mysql_fetch_array() on a different page and the error is not going away.

There is also no mysql_error() error.

Do you have any suggestions?

Thank you again for all your help

The only way the user can send files to the server using PHP is by using the file form element.

http://ca2.php.net/manual/en/features.file-upload.php

There is no way your script can access your user’s informations. It would be way too much of a security hole. If you are dealing with a restrained set of users and know exactly what you want to do, you could use a shared network drive to do the transfers.

So I created the upload code similar to that on php.net and other tutorials out there. I am currently getting an error though.

Upload failed /tmp/phpr8YV4v
Array ( [file] => Array ( [name] => Array ( [0] => San Juan.jpg [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => [17] => [18] => [19] => [20] => [21] => [22] => [23] => [24] => ) [type] => Array ( [0] => image/pjpeg [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => [17] => [18] => [19] => [20] => [21] => [22] => [23] => [24] => ) [tmp_name] => Array ( [0] => /tmp/phpr8YV4v [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => [17] => [18] => [19] => [20] => [21] => [22] => [23] => [24] => ) [error] => Array ( [0] => 0 [1] => 4 [2] => 4 [3] => 4 [4] => 4 [5] => 4 [6] => 4 [7] => 4 [8] => 4 [9] => 4 [10] => 4 [11] => 4 [12] => 4 [13] => 4 [14] => 4 [15] => 4 [16] => 4 [17] => 4 [18] => 4 [19] => 4 [20] => 4 [21] => 4 [22] => 4 [23] => 4 [24] => 4 ) [size] => Array ( [0] => 259423 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 [8] => 0 [9] => 0 [10] => 0 [11] => 0 [12] => 0 [13] => 0 [14] => 0 [15] => 0 [16] => 0 [17] => 0 [18] => 0 [19] => 0 [20] => 0 [21] => 0 [22] => 0 [23] => 0 [24] => 0 ) ) ) Debugging 1
Upload complete

Now the first and last line are outputs I made. Can anybody help me with this issue?

Thanks

Sponsor our Newsletter | Privacy Policy | Terms of Service