Hi All I keep getting stuck with array errors on the following, any ideas? Ta gkz <html> <head> </head> <body> <?php // Open a connection to the database $link = mysql_connect ("localhost", "xxxxx", "xxxxx") or die ('I cannot connect to the database because: ' . mysql_error()); mysql_select_db ("xxxxx"); // Define a query that retrieves each field $query = "SELECT sForename, sSurname, sCountry FROM tblusers WHERE (nUser_ID = '2')"; // Run the query and store the result $result = mysql_query($link, $query); // Assign each record in the result to an array while ($row = mysql_fetch_array($result)) { // Assign each array element to a variable $sForename = $row['sForename']; $sSurname = $row['sSurname']; $sCountry = $row['sCountry']; // Print results echo $sForename; echo $sSurname; echo $sCountry; } ?> </body> </html>
Sorry - error message follow - Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/xxxxx/public_html/authors/index.php on line 15 Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/xxxxx/public_html/authors/index.php on line 18 Ta gkz
The order of the parameters in mysql_query() is wrong. $result = mysql_query($query, $link); PHP: The link identifier should be the second, like above. (The identifier is optional though, you only need it if you have multiple connections)
Cool, that worked (why is it always something so simple yet you never see it til it's pointed out - LOL)!! Thanks gkz
LOL - tired? What's that (as my eyeballs rebound of the keyboard and pop back into their sockets . . .) Another one- with WHERE (nUser_ID = '2') is it possible to substitute the '2' with part of the referring URL? I have no idea if this can be done. e.g. if a member is handing out their 'member page' URL and the '2' is their member number, can you grab their profile by referring to the '2' on the end of the URL? I'll probably have to start a new thread with the correct subject line. Ta gkz
You can get values from the URL using the $_GET variable. For example for the ID, if the URL was file.php?id=2 Then in PHP, you'd do this to get it: $id = $_GET['id']; PHP: And to combine that with your query: $query = sprintf(" SELECT sForename, sSurname, sCountry FROM tblusers WHERE nUser_ID = %d LIMIT 1" , intval($_GET['id'])); PHP:
Ahh (slaps head), sprintf returns a formatted string, then I can get the integer. That should do it, I'll give it a go and see what happens. Thanks again gkz
WOW, that worked to (am currently dancing on the ceiling, but don't look up, I'm not wearing any knickers - LOL). Thanks again gkz