php - How do I echo the contents of a MySQL table field? -


i seem having trouble understanding concept of how use information in mysql database using php/mysqli. understand it, generate variable representing connection object:

$connectionobject = mysqli_connect('serverstring', 'userstring', 'passstring', 'databasestring'); 

then, generate variable representing query string want use:

$querystring = "select rowname tablename"; 

then, generate variable representing result object returned successful query:

$resultobject = mysqli_query($connectionobject, $querystring); 

then, use fetch_assoc() function generate array result object , assign variable:

$resultarray = myqli_fetch_assoc($resultobject); 

then, can use while loop (i have trouble one) sort through array , use content of row somehow:

while ($resultarray) { echo $resultarray["rowname"]; } 

do have concept wrong way, somehow, because not working me, output text content of text-based char(10) field contents of no more than: "blah".

the need loop through array pick out array item name in end anyway seems moot me begin with, no matter look, find same concept.

my script code, minus few key details, is:

if ($connectionobject=mysqli_connect("host0", "username0", "password0", "mysqldatabase0")) { echo "con"; }  if ($querystring="select 'testdata' 'testtable'") { echo "query"; }  if ($resultobject=mysqli_query($connectionobject, $querystring)) { echo "result"; }  if ($resultarray=mysqli_fetch_assoc($resultobject)) { echo "array"; }  while ($row=$resultarray) { echo $row["testdata"]; print_r ($row); } 

mysqli_fetch_assoc returns associate array of string representing fetched row in result set $resultobject.

the problem you're using while loop. want capture returned associative array in variable , access data via variable follows:

while ($row = $resultarray) {     echo $row["rowname"]; } 

to sort rowname can use mysql order by clause in query follows returns results sorted rowname:

$querystring = "select rowname tablename order rowname"; 

update after op posted full code:

in first if statement happen if connection failed? want add error handling there:

$connectionobject=mysqli_connect("host0", "username0", "password0", "mysqldatabase0")); if (!$connectionobject) {     // exist out of script showing error     die("error connecting database " . mysqli_error($connectionobject)); } else {     // don't need else i'll keep here since had     echo "con"; } 

the problem here using single quotes column name , table name mysql identifiers. mysql identifiers quote character backtick not single quote.
need use backticks if 1 of these identifiers 1 of mysql reserved words (mysql reserved words), other cases don't need use them.

update query:

if ($querystring="select `testdata` `testtable`") {     echo "query"; // leaving is, not required } 

lastly, improvement. want add error handling here too:

if ($resultobject=mysqli_query($connectionobject, $querystring)) {   echo "result"; // leaving is, not required } else {   echo "error executing query " . mysqli_error($connectionobject); } 

please note when use script error messages printed @ client i.e. when use script in web application errors shown in user's browser. want implementing logging , not printing them directly.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -