简体   繁体   中英

Where does these functions? “variables”? come from?

I haven´t been able to find this anywhere (mainly because i don´t know what to search for), but when i was reading on W3Schools i stumbled upon the following piece of code:

$sql="SELECT Lastname,Age FROM Persons ORDER BY Lastname";

if ($result=mysqli_query($con,$sql))
  {
  // Return the number of rows in result set
  $rowcount=mysqli_num_rows($result);
  printf("Result set has %d rows.\n",$rowcount);
  // Free result set
  mysqli_free_result($result);
  }

Then i wondered the $result, what is that? where does it come from?

When i search for "$result mysqli" nothing helpful really appears...

I´m used to $ being the sign for variables, but this clearly ain´t defined anywhere in the code. Is it some way of calling a function?

The phrase mysqli_query($con,$sql) is a function call. The tip-off that this is a function call is the parentheses following the function name.

When the code hits that phrase it calls some other PHP code called mysqli_query (which is in a library somewhere). That code receives from your program two values (the $con and $sql values, performs some actions and returns a newly-created value (an object containing information about and results of your query).

That value is then assigned to the variable $result.

This variable is a PHP object, and it contains the result of a sql query. Thanks to this you can extract data from a data base

The variable is assigned right here:

if ($result=mysqli_query($con,$sql))

You may be more used to this construct:

if ($foo == $bar)

That's a comparison (double equal sign == ).

However, assignments can happen perfectly well inside an if statement as well. The assignment expression will return the assigned value as a result which is then used for if . So:

if ($foo = bar())

is equivalent to

$foo = bar();
if ($foo) 

$result is an array which got values from the mysqli_query.

Php's mysqli extension functions are used to run queries with the database. You need configure a connection before running any queries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM