why the variable $link doesn't work inside a funtion? this is the $link
$link = @mysql_connect( "localhost", $users, $pass );
And there is the code that don't work
mysql_query ( $query, $link)
What should I do?
thanks
Is $link actually a valid resource? Did you test it to make sure your connection to the database worked? (If it failed you wouldn't have noticed because you suppressed errors with the "@" sign.)
Try something like this:
if(!$link = @mysql_connect( "localhost", $users, $pass )) { die('Database connection failed.'); }
yes it work it's about the $link can't be in the funtion
What you posted is valid php - your going to need to post the entire code in order to explain what problem you are getting.
Here it goes:
function insertname(){ $firstnames = array("Jonh", "Paul", "Bill"); $lastnames = array("Simon", "Luck","Guy"); $name = $firstnames[array_rand($firstnames)]." ".$lastnames[array_rand($lastnames)]; $sql = mysql_query("SELECT names from home WHERE names = '$name' LIMIT 1") or die(mysql_error()); $resource = mysql_num_rows($sql); print $name; $query = "INSERT INTO home(names) values('$name')"; mysql_query ( $query, $link) }
Just leave $link out, its an optional argument and isn't required.
mysql_query($query);
If you really want it in there you will need to pass it into the function, but as I said, its not needed.
Ah, $link is probably not available in the function's scope. Pass it in:
function insertname($link) {
And then it will probably work. It would be a good idea to verify that it is a valid resource reference within the functions before trying to use it.
it still gives this error:
Parse error: syntax error, unexpected '}' in C:\(..)\tez1.php on line 20
what i'm doing wrong?
Your missing a ; after your query.
thanks...what a mistake however now it says:
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\(...)\tez1.php on line 18
why?