Solved

php mysql out put data

Posted on 2016-11-29
4
21 Views
Last Modified: 2016-11-30
Dear Experts,

I do it by heart ( memorized it ), I want to learn what $result variable do?

without while loop $result variable does not give anything, not even row number,

what does this variable do?

$sql = "SELECT * FROM ilan where uid='$uid'";
$result = $conn->query($sql);
0
Comment
Question by:Braveheartli
4 Comments
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 167 total points
ID: 41906859
As mentioned on this page http://php.net/manual/en/mysqli.query.php ,  a 'mysqli' query returns a mysqli_result object  or FALSE on failure.  The mysqli_result object  is used to reference the query you made in the functions that follow.  It is not something that is used by itself.

Do Not memorize these things by heart, look them up.  In addition to the many basic PHP language features, there are an additional 1500 plus functions that are available.  They are all well documented at http://php.net/
1
 
LVL 54

Assisted Solution

by:Julian Hansen
Julian Hansen earned 167 total points
ID: 41906880
When you run a query there are many potential actions you can take once it is complete. For instance - get a row which you may want as an object or an associative array. You may want to know how many rows were affected.

The $result variable is an instance of the mysqli_result class (as Dave pointed out) that gives access to the various functions you may want to perform on the result set.

As a value on its own it is only really usable on failure when it will be set to false or when the query() does not return data in which case it will be true.

If you are expecting data to be returned from the query you can do this
$result = $mysqli->query($query);
if ($result) {
   // query was successful so process result here
}
else {
   // report error
}

Open in new window

0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 166 total points
ID: 41907054
+1 for Dave's recommendation to look up the examples in the documentation.  PHP is a living language, and there are features being added to it all the time.  If you read the man pages, you'll find out about these features and you'll see examples of how to use them.  I could not write code without having a window open to the online documentation at http://php.net!

Many examples of MySQL, MySQLi, and PDO are available here, with the scripts built in parallel construction so you can see how each extension accomplishes the same tasks.  In these examples I named the results variable $res.
https://www.experts-exchange.com/articles/11177/PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html

As a general rule, var_dump() is your friend.  It will not always expose everything about a variable (some things like SimpleXML_Element and PHP "resources" are hidden), but for any initial exploration, the first thing to do is use var_dump() and see what's in the variable.  There are examples in the article showing how to use var_dump() to visualize the results variable.

MySQL is a complicated thing, with a history that is sometimes nonlinear.  Information you need to provide or retrieve is often scattered across different objects.  Sometimes it's useful to look at the MySQLi object, and other times a statement or results object contains what you want.  The man pages, and var_dump(), can help you discern what is where.
0
 
LVL 1

Author Closing Comment

by:Braveheartli
ID: 41907792
Thank you Dave, Julian and Ray. var_dump() is good idea Ray. I would remember next time. Thank you all.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question