get path of a result of query in php - php

I am trying to get the direct path of the $result below:
$query="SELECT videoid from blabla";
$result=mysql_query($query);
$destination="ftp:/.../";
I am trying to get the direct path of the videoid which is in $result so i can send all the actual video files to an ftp.
So far I have:
ftp_put ($connection,$destination,$result,FTP_ASCII);
When I run it it says that the source has to be a string or something like that.

$result is not the field your are looking for, you have to fetch the data from the $result. Note the mysql_fetch_assoc call which pulls data out of the query result.
Here is an example from the mysql_query documentation
// Formulate Query
// This is the best way to perform an SQL query
// For more examples, see mysql_real_escape_string()
$query = sprintf("SELECT firstname, lastname, address, age FROM friends WHERE firstname='%s' AND lastname='%s'",
mysql_real_escape_string('fred'),
mysql_real_escape_string('fox'));
// Perform Query
$result = mysql_query($query);
// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
// Use result
// Attempting to print $result won't allow access to information in the resource
// One of the mysql result functions must be used
// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc.
while ($row = mysql_fetch_assoc($result)) {
echo $row['firstname'];
echo $row['lastname'];
echo $row['address'];
echo $row['age'];
}
// Free the resources associated with the result set
// This is done automatically at the end of the script
mysql_free_result($result);
You can therefore modify your code as follows:
$query="SELECT videoid from blabla";
$result=mysql_query($query);
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
die($message);
}
while ($row = mysql_fetch_assoc($result)) {
// loop through every videoid returned, ftp each individually
ftp_put ($connection,$destination,$row['videoid'],FTP_ASCII);
}
mysql_free_result($result);

$query = "SELECT videoid from blabla";
$result = mysql_query( $query );
$row = mysql_fetch_row( $result );
$videoid = $row[0]; // <== there is your videoid
$destination="ftp:/.../";
ftp_put( $connection, $destination, $videoid, FTP_ASCII );

You probably need something like this:
To select one video:
// select all fields, not just the videoid
$query = "SELECT * FROM blabla WHERE videoid = 1"; // replace 1 with actual video id
// or only fields videoid and path
$query = "SELECT videoid, path FROM blabla WHERE videoid = 1"; // replace 1 with actual video id
$result = mysql_query( $query );
$row = mysql_fetch_assoc( $result );
$path = $row[ 'path' ]; // presuming 'path' is the column name
/* do some ftp stuff */
To select all videos:
// select all fields, not just the videoid
$query = "SELECT * FROM blabla";
// or only fields videoid and path
$query = "SELECT videoid, path FROM blabla";
$result = mysql_query( $query );
while( $row = mysql_fetch_assoc( $result ) )
{
$path = $row[ 'path' ]; // presuming 'path' is the column name
/* do some ftp stuff per row */
}

Related

Need Some Help Regarding Fetching Data from Mysql using explode function

In the below script I want to fetch data from mysql using a explode function and also a variable within an explode function.
Here's how I want to get
<?php
include ('config.php');
$track = "1,2,3";
$i = 1
$trackcount = explode(",",$track);
$sql = "SELECT * FROM tracks WHERE id='$trackcount['$i']'";
$retval = mysql_query($sql, $conn);
while ($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "{$row['track_name']}";
}
mysql_free_result($retval);
?>
This is the code
$sql = "SELECT * FROM tracks WHERE id='$trackcount[$i]'";
I want sql to fetch data from tracks table where id = $trackcount[$i]
Whatever the value of $trackcount[$i] mysql should fetch but it shows a blank screen.
If I put this
$sql = "SELECT * FROM tracks WHERE id='$trackcount[1]'";
It works perfectly
save your $trackcount[$i] in one variable and then pass it in the query as given below
<?php
include ('config.php');
$track = "1,2,3";
$i = 1;
$trackcount = explode(",",$track);
$id=$trackcount[$i];
$sql = "SELECT * FROM tracks WHERE id='$id'";
$retval = mysql_query($sql, $conn);
while ($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "{$row['track_name']}";
}
mysql_free_result($retval);
?>
and one more thing check your previous code with echo of your query and see what is passing ok.
echo $sql = "SELECT * FROM tracks WHERE id='$trackcount['$i']'";//like this
problem is with your query
$sql = "SELECT * FROM tracks WHERE id='$trackcount['$i']'";//change
to
$sql = "SELECT * FROM tracks WHERE id='$trackcount[$i]'";
Generally you would want to use the IN operator with this type of query, so for you this would be:-
$sql="SELECT * FROM `tracks` WHERE `id` in (".$track.");";
or, if the $ids are in an array,
$sql="SELECT * FROM `tracks` WHERE `id` in (".implode( ',', $array ) .");";

Copy document from links that doesn't same with data in database

I crawled pdf link from web, I want to copy the pdf that doesn't exist yet in database by check out the name of document (that I get from link) with the name of document that exist in database.
here's the code :
$input = explode(" ",trim(basename($pdfLink, ".pdf"),"() "));
$sql = mysql_query("SELECT doc_name FROM tb WHERE doc_name ='$input'")or die(mysql_error());
if (!$sql){
copy($pdfLink, $savePath . basename($pdfLink));
}
$pdfLink is string of PDFlinks. but, copy process didn't work. what's wrong? thank you :)
It is better for you to check count of related records in DB
$input = explode(" ",trim(basename($pdfLink, ".pdf"),"() "));
$sql = mysql_query("SELECT COUNT(*) AS cnt FROM tb WHERE doc_name ='$input'")
or die(mysql_error());
$row = mysql_fetch_assoc($sql);
if ($row['cnt'] < 1){
copy($pdfLink, $savePath . basename($pdfLink));
}
mysql_free_result($sql);
or to count affected rows:
$input = explode(" ",trim(basename($pdfLink, ".pdf"),"() "));
$sql = mysql_query("SELECT doc_name FROM tb WHERE doc_name ='$input' LIMIT 1")
or die(mysql_error());
$rows = mysql_affected_rows($sql);
if ($rows < 1){
copy($pdfLink, $savePath . basename($pdfLink));
}
mysql_free_result($sql);
If your SELECT statement returns an empty set (i.e. does not find anything), it still returns a valid handle, which PHP interprets as true. So if no database error occurs, the variable $sql always results to true.

Select certain MySQL fields and display them using php

So what I want to do is I want to search for a certain name in a column called 'filename' and display the 'count' on that row.
This might help explain:
I want to search for Packages in the 'filename' column but I want to display the 'count' on it (4)
id filename count
1 Packages 4
2 Another 7
$filename = 'Packages';
$result = mysql_query("SELECT `count` FROM `downloads` WHERE `filename` = $filename");
Also note that if the filename can come from user input you have to be aware of injection attacks.
$filename = 'Packages'; // From user
$filename = mysql_real_escape_string($filename);
$result = mysql_query("SELECT `count` FROM `downloads` WHERE `filename` = $filename");
Here is an example that selects all Filenames and counts and lists them in a table:
$result = mysql_query("SELECT `filename` `count` FROM `downloads`");
?><table><tr><td>Filename</td><td>Count</td></tr><?php
while($row = mysql_fetch_assoc($result)){
?>
<tr><td><?php echo $row['filename']; ?></td><td><?php echo $row['count']; ?></td></tr>
<?php
}
?></table><?php
Assuming what you actually wants is a report of download counts for each filename, you need to use the GROUP BY clause in your SQL statement:
<?php
$res = mysql_query('SELECT id, filename, COUNT(*) AS count FROM downloads GROUP BY filename');
if (is_resource($res)) {
while (false !== ($row = mysql_fetch_assoc($res))) {
printf('%d - %s - %d', $row['id'], $row['filename'], $row['count']);
}
}
?>
$query = "SELECT count FROM downloads WHERE filename='Packages'";
EDIT:
$result = mysql_query($query, $con) or die(mysql_error());
$arr_down = mysql_fetch_assoc($result);
echo $arr_down["count"];
UPDATE: For one or more rows with 'Packages' in it, you can show the results in a table like this:
$con = mysql_connect("localhost", "MySQL_username", "MySQL_password") or die ("Unable to connect to MySQL Server " . mysql_error());
$fname = "Packages";
$query = "SELECT filename, count FROM downloads WHERE filename='" . $fname . "'";
$result = mysql_query($query, $con) or die(mysql_error());
$html = "<table>\n<tr><th>Filename</th><th>Count</th></tr>\n";
while ($row = mysql_fetch_assoc($result))
$html .= "<tr><td>" . $row['filename'] . "</td><td>" . $row['count'] . "</td></tr>\n";
$html .= "</table>\n";
echo $html;
$count = mysql_num_rows($result)
echo($count)
Check out the PHP mysql documentation for more. In particular you'll want to learn about the fetch() functions, which you'll use to display the data you retrieve with your query.
the raw query would be:
select count( filename ) from downloads;
Edit: Okay, you want to loop them onto the page:
foreach( $result as $i )
{
if( !empty( $i ) )
{
echo "<div>$i['column_name']</div>";
}
}

mysql query result in php variable

Is there any way to store mysql result in php variable? thanks
$query = "SELECT username,userid FROM user WHERE username = 'admin' ";
$result=$conn->query($query);
then I want to print selected userid from query.
Of course there is. Check out mysql_query, and mysql_fetch_row if you use MySQL.
Example from PHP manual:
<?php
$result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($result);
echo $row[0]; // 42
echo $row[1]; // the email value
?>
There are a couple of mysql functions you need to look into.
mysql_query("query string here") : returns a resource
mysql_fetch_array(resource obtained above) : fetches a row and return as an array with numerical and associative(with column name as key) indices. Typically, you need to iterate through the results till expression evaluates to false value. Like the below:
while ($row = mysql_fetch_array($query)){
print_r $row;
}
Consult the manual, the links to which are provided below, they have more options to specify the format in which the array is requested. Like, you could use mysql_fetch_assoc(..) to get the row in an associative array.
Links:
http://php.net/manual/en/function.mysql-query.php
http://php.net/manual/en/function.mysql-fetch-array.php
In your case,
$query = "SELECT username,userid FROM user WHERE username = 'admin' ";
$result=mysql_query($query);
if (!$result){
die("BAD!");
}
if (mysql_num_rows($result)==1){
$row = mysql_fetch_array($result);
echo "user Id: " . $row['userid'];
}
else{
echo "not found!";
}
$query="SELECT * FROM contacts";
$result=mysql_query($query);
I personally use prepared statements.
Why is it important?
Well it's important because of security. It's very easy to do an SQL injection on someone who use variables in the query.
Instead of using this code:
$query = "SELECT username,userid FROM user WHERE username = 'admin' ";
$result=$conn->query($query);
You should use this
$stmt = $this->db->query("SELECT * FROM users WHERE username = ? AND password = ?");
$stmt->bind_param("ss", $username, $password); //You need the variables to do something as well.
$stmt->execute();
Learn more about prepared statements on:
http://php.net/manual/en/mysqli.quickstart.prepared-statements.php MySQLI
http://php.net/manual/en/pdo.prepared-statements.php PDO
$query = "SELECT username, userid FROM user WHERE username = 'admin' ";
$result = $conn->query($query);
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$arrayResult = mysql_fetch_array($result);
//Now you can access $arrayResult like this
$arrayResult['userid']; // output will be userid which will be in database
$arrayResult['username']; // output will be admin
//Note- userid and username will be column name of user table.

How to display MySQL Select statement results in PHP

I have the following code and it should return just one value (id) from mysql table. The following code doesnt work. How can I output it without creating arrays and all this stuff, just a simple output of one value.
$query = "SELECT id FROM users_entity WHERE username = 'Admin' ";
$result = map_query($query);
echo $result;
I do something like this:
<?php
$data = mysql_fetch_object($result);
echo $data->foo();
?>
You have to do some form of object creation. There's no real way around that.
You can try:
$query = "SELECT id FROM users_entity WHERE username = 'Admin' ";
//$result = map_query($query);
//echo $result;
$result = mysql_query($query); // run the query and get the result object.
if (!$result) { // check for errors.
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($result); // get the single row.
echo $row['id']; // display the value.
all you have is a resource, you would still have to make it construct a result array if you want the output.
Check out ADO if you want to write less.
Not sure I exactly understood, what you want, but you could just do
$result = mysql_query('SELECT id FROM table WHERE area = "foo" LIMIT 1');
list($data) = mysql_fetch_assoc($result);
if you wish to execute only one row you can do like this.
$query = "SELECT id FROM users_entity WHERE username = 'Admin' ";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
echo $row[0];
there have been many ways as answered above and this is just my simple example. it will echo the first row that have been executed, you can also use another option like limit clause to do the same result as answered by others above.

Categories