NoDoze Posted January 6, 2009 Share Posted January 6, 2009 I need to use php to generate a url from mysql data, possible? For example: I have different values in a mysql table that need to be sent via URL to upload to a server. Xvalue, Zvalue, Uvalue I need to generate a url like: http://domain.com/script.php?ID=Xvalue&ID2=Zvalue&ID3=Uvalue How would I do this? Thanks for any help! Quote Link to comment https://forums.phpfreaks.com/topic/139735-solved-generate-url-from-mysql-table-data/ Share on other sites More sharing options...
Maq Posted January 6, 2009 Share Posted January 6, 2009 You have to be a little more specific. Something like this: $sql = "SELECT * FROM table WHERE something = something"; $result = mysql_query($sql); while($row = $mysql_fetch_array($result)) { ?> http://domain.com/script.php?ID=&ID2=&ID3= Quote Link to comment https://forums.phpfreaks.com/topic/139735-solved-generate-url-from-mysql-table-data/#findComment-731086 Share on other sites More sharing options...
NoDoze Posted January 6, 2009 Author Share Posted January 6, 2009 Oh cool! Thanks! One questions.... WHERE something = something What would this be used for? I don't think I'd need it...? Quote Link to comment https://forums.phpfreaks.com/topic/139735-solved-generate-url-from-mysql-table-data/#findComment-731188 Share on other sites More sharing options...
NoDoze Posted January 6, 2009 Author Share Posted January 6, 2009 To add to this... I'm also trying to import a text file into mysql from bash script: #!/bin/bash mysql -uroot -e "LOAD DATA INFILE '/home/user/prelim.txt'; INTO TABLE database.table; FIELDS TERMINATED BY ',';" ...but I keep getting: ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 ...when I run the script. However, when I copy/paste this: mysql -hlocalhost -uroot LOAD DATA INFILE '/home/user/prelim.txt' INTO TABLE database.table FIELDS TERMINATED BY ','; \q ...into the CLI via ssh it works perfectly! Can someone help and tell me what I'm doing wrong? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/139735-solved-generate-url-from-mysql-table-data/#findComment-731223 Share on other sites More sharing options...
fenway Posted January 7, 2009 Share Posted January 7, 2009 Why all the extra semi-colons? Quote Link to comment https://forums.phpfreaks.com/topic/139735-solved-generate-url-from-mysql-table-data/#findComment-731432 Share on other sites More sharing options...
NoDoze Posted January 7, 2009 Author Share Posted January 7, 2009 makes no difference, still get the same error... Quote Link to comment https://forums.phpfreaks.com/topic/139735-solved-generate-url-from-mysql-table-data/#findComment-731841 Share on other sites More sharing options...
NoDoze Posted January 7, 2009 Author Share Posted January 7, 2009 Curious about the first question I asked... In this... $sql = "SELECT * FROM table WHERE something = something"; $result = mysql_query($sql); while($row = $mysql_fetch_array($result)) { ?> http://domain.com/script.php?ID=<?php echo $row['Xvalue']; ?>&ID2=<?php echo $row['Yvalue']; ?>&ID3=<?php echo $row['Zvalue']; ?> <?php } ?> I'm trying to pull the most recent data row from the database to enter into the url, then send this url. Which is why I don't think I need the WHERE clause, but is it required? And how would I send the URL? Quote Link to comment https://forums.phpfreaks.com/topic/139735-solved-generate-url-from-mysql-table-data/#findComment-731869 Share on other sites More sharing options...
NoDoze Posted January 7, 2009 Author Share Posted January 7, 2009 ok so far I have this: <?php include 'config.php'; include 'opendb.php'; $query="SELECT * FROM table"; $result=mysql_query($query); header('Location: http://domain.com/subfolder/update.php?ID=value1&PASSWORD=value2&dateutc='.$_POST['date'].'&data1='.$_POST['data_1'].'&data2='.$_POST['data_2'].'&data3='.$_POST['data_3'].'&data4='.$_POST['data_4'].'&weather=&clouds&action=updateraw'); include 'closedb.php'; ?> But it doesn't post the data from the table... Should it be POST, or GET? ...or something else. I'm really confused on what the difference is between POST and GET... thanks. Quote Link to comment https://forums.phpfreaks.com/topic/139735-solved-generate-url-from-mysql-table-data/#findComment-732064 Share on other sites More sharing options...
NoDoze Posted January 7, 2009 Author Share Posted January 7, 2009 Ok, I'm SO close... But still no cigar... This is what I have so far: <?php include 'config.php'; include 'opendb.php'; $sql = "SELECT * FROM hicks ORDER BY date ASC LIMIT 1"; header('Location: http://domain.com/update.php?ID=username&PASSWORD=password&dateutc='.$_GET['date'].'&tempf='.$_GET['airt_f_avg'].'&winddir='.$_GET['winddir'].'&windspeedmph='.$_GET['ws_mph'].'&windgustmph='.$_GET['0'].'&rainin='.$_GET['rain_in_tot'].'&baromin='.$_GET['0'].'&dewptf='.$_GET['dewpt_f_avg'].'&humidity='.$_GET['rh_avg'].'&weather=&clouds=&action=updateraw') include 'closedb.php'; ?> But when executed, the page comes up blank. And when I remove the database open/config/close info, I get a URL, but with no data... And this... <?php include 'config.php'; include 'opendb.php'; $result = mysql_query("SELECT * FROM hicks ORDER BY date ASC LIMIT 1"); while($row = mysql_fetch_array($result)) { echo $row['date'] . " " . $row['time'] . " " . $row['record'] . " " . $row['pt_ft'] . " " . $row['pipeq_gal_tot'] . " " . $row['rain_in_tot'] . " " . $row['ws_mph'] . " " . $row['winddir'] . " " . $row['slrw_avg'] . " " . $row['airt_f_avg'] . " " . $row['tw_f_avg'] . " " . $row['dewpt_f_avg'] . " " . $row['rh_avg'] . " " . $row['e_kpa_avg'] . " " . $row['eto_mm'] . " " . $row['solarradcalc']; } include 'closedb.php'; ?> Gets the data but not in URL form. I need to somehow combine the two! HELP! Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/139735-solved-generate-url-from-mysql-table-data/#findComment-732088 Share on other sites More sharing options...
Maq Posted January 8, 2009 Share Posted January 8, 2009 If you want to produce a link for each row you need to: 1) Use a while loop so it can extract ALL the rows from the DB. 2) Change post to $row (or whatever variable you want to use). $_POST is for transferring data from page to page. 3) The header redirects people automatically. I imagine you want to have people click on a desired link, so just display the links as hrefs. Try this *not tested*: include 'config.php'; include 'opendb.php'; $query="SELECT * FROM table"; $result=mysql_query($query); $i = 0; while($row = mysql_fetch_array($result)) { $i++; ?> Link include 'closedb.php'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/139735-solved-generate-url-from-mysql-table-data/#findComment-732091 Share on other sites More sharing options...
Maq Posted January 8, 2009 Share Posted January 8, 2009 You can combine HTML and PHP in two ways: 1) echo out your HTML. 2) close PHP and open it back up when you need it. (I prefer the 2 because I hate dealing with single and double quotes, gets confusing sometimes.) But here's #1: $sql = "SELECT * FROM hicks ORDER BY date ASC LIMIT 1"; $row = mysql_query($sql) or die(mysql_error()); $row = mysql_fetch_array($result); echo "your link!"; Quote Link to comment https://forums.phpfreaks.com/topic/139735-solved-generate-url-from-mysql-table-data/#findComment-732094 Share on other sites More sharing options...
NoDoze Posted January 8, 2009 Author Share Posted January 8, 2009 No links.... I want the php page to pull the data into url form then redirect to that new url. Quote Link to comment https://forums.phpfreaks.com/topic/139735-solved-generate-url-from-mysql-table-data/#findComment-732096 Share on other sites More sharing options...
Maq Posted January 8, 2009 Share Posted January 8, 2009 Can I ask what exactly you're trying to do? Is this page going to another server or something? If not, why can't you just access the data straight from the DB? Quote Link to comment https://forums.phpfreaks.com/topic/139735-solved-generate-url-from-mysql-table-data/#findComment-732098 Share on other sites More sharing options...
NoDoze Posted January 8, 2009 Author Share Posted January 8, 2009 yes uploading data via HTTP GET...it's what they require... I tried: <?php include 'config.php'; include 'opendb.php'; $sql = "SELECT * FROM hicks ORDER BY date ASC LIMIT 1"; $row = mysql_query($sql) or die(mysql_error()); $row = mysql_fetch_array($result); echo "<a href='http://domain.com/update.php?ID=username&PASSWORD=password&dateutc=" . $row['date'] . "&tempf=" . $row['airt_f_avg'] . ">your link!</a>"; include 'closedb.php'; ?> After clicking the link, still creates a URL with no data. Quote Link to comment https://forums.phpfreaks.com/topic/139735-solved-generate-url-from-mysql-table-data/#findComment-732103 Share on other sites More sharing options...
Maq Posted January 8, 2009 Share Posted January 8, 2009 $row = mysql_query($sql) or die(mysql_error()); should be $result = mysql_query($sql) or die(mysql_error()); *my fault Quote Link to comment https://forums.phpfreaks.com/topic/139735-solved-generate-url-from-mysql-table-data/#findComment-732106 Share on other sites More sharing options...
fenway Posted January 8, 2009 Share Posted January 8, 2009 Next time, don't double-post. Quote Link to comment https://forums.phpfreaks.com/topic/139735-solved-generate-url-from-mysql-table-data/#findComment-732268 Share on other sites More sharing options...
NoDoze Posted January 8, 2009 Author Share Posted January 8, 2009 Yes, I know. That was actually for a different thing. But then added it to this one. Hence why you see it twice. Quote Link to comment https://forums.phpfreaks.com/topic/139735-solved-generate-url-from-mysql-table-data/#findComment-732494 Share on other sites More sharing options...
fenway Posted January 8, 2009 Share Posted January 8, 2009 Yes, I know. That was actually for a different thing. But then added it to this one. Hence why you see it twice. Then be sure to explicitly link back to the original post if you truly feel that it's completely different Quote Link to comment https://forums.phpfreaks.com/topic/139735-solved-generate-url-from-mysql-table-data/#findComment-732514 Share on other sites More sharing options...
NoDoze Posted January 8, 2009 Author Share Posted January 8, 2009 Thanks Maq. Worked perfectly! But how would I get it to submit the link automatically, so no one has to click the link? Quote Link to comment https://forums.phpfreaks.com/topic/139735-solved-generate-url-from-mysql-table-data/#findComment-732515 Share on other sites More sharing options...
NoDoze Posted January 8, 2009 Author Share Posted January 8, 2009 <?php include 'config.php'; include 'opendb.php'; $sql = "SELECT * FROM hicks ORDER BY date DESC LIMIT 1"; $result = mysql_query($sql) or die(mysql_error()); $row = mysql_fetch_array($result); header("http://domain.com/update.php?ID=username&PASSWORD=password&dateutc=" . $row['date'] . "&tempf=" . $row['airt_f_avg'] . "&winddir=" . $row['winddir'] . "&windspeedmph=" . $row['ws_mph'] . "&windgustmph=" . $row[''] . "&rainin=" . $row['rain_in_tot'] . "&baromin=" . $row[''] . "&dewptf=" . $row['dewpt_f_avg'] . "&humidity=" . $row['rh_avg'] . "&action=updateraw"); include 'closedb.php'; ?> This is the only way I can think it would work. But it doesn't. Can someone enlighten me, thanks. Newbie Quote Link to comment https://forums.phpfreaks.com/topic/139735-solved-generate-url-from-mysql-table-data/#findComment-732659 Share on other sites More sharing options...
NoDoze Posted January 8, 2009 Author Share Posted January 8, 2009 ok, I think I figured it out... I tested this and it worked!!!! SWEETNESS!!! LOL <?php include 'config.php'; include 'opendb.php'; $sql = "SELECT * FROM hicks ORDER BY date DESC LIMIT 1"; $result = mysql_query($sql) or die(mysql_error()); $row = mysql_fetch_array($result); $weather = "http://domain.com/update.php?ID=username&PASSWORD=password&dateutc=" . $row['date'] . "&tempf=" . $row['airt_f_avg'] . "&winddir=" . $row['winddir'] . "&windspeedmph=" . $row['ws_mph'] . "&windgustmph=" . $row[''] . "&rainin=" . $row['rain_in_tot'] . "&baromin=" . $row[''] . "&dewptf=" . $row['dewpt_f_avg'] . "&humidity=" . $row['rh_avg'] . "&action=updateraw"; header('Location:' . $weather); include 'closedb.php'; ?> Let me know if you see any error in the code, if it looks good! Thanks!!! Quote Link to comment https://forums.phpfreaks.com/topic/139735-solved-generate-url-from-mysql-table-data/#findComment-732743 Share on other sites More sharing options...
Maq Posted January 9, 2009 Share Posted January 9, 2009 Yeah you can do it that way. I guess you could call it a prepared link. Header should automatically redirect the user. Glad you got it working. Quote Link to comment https://forums.phpfreaks.com/topic/139735-solved-generate-url-from-mysql-table-data/#findComment-732986 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.