Cannot assign PHP variable to mysqli query -
so i'm trying echo each row of users 1 of databases @ website set on wamp server, version 2.5. wrong, i've googled , search site's posts... don't think there's been problem quite 1 addressed yet.
in practice of using php following tutorials/exercises, had of course found mysql prefers either pdo or mysqli these days, decided give shot @ using mysqli.
the weird thing is, i've included php file configures mysql connection, , know connection variable being called properly, since i've printed out result of query variable being assigned to:
mysqli_result object ( [current_field] => 0 [field_count] => 3 [lengths] => [num_rows] => 1 [type] => 0 )
above output when use:
print_r($db_con->query("select * users"));
yet when try assign result variable, $user_rs, , print value of variable, "notice: undefined variable: user_rs in c:\wamp\www\index.php on line 58."
here files.
dbconfig.php:
<?php $db_con= new mysqli("localhost","root","abc123", "otongadgethub"); if($db_con->connect_errno){ echo $db_con->connect_error; } ?>
index.php:
<?php session_start(); include ("dbconfig.php"); ?> <html> <head> <title>my webpage</title> <link rel = "stylesheet" type = "text/css" href = "site.css" /> <link rel="shortcut icon" href="index.html?img=favicon" type="image/ico" /> <script> function validatingform(){ var x; var y; x = document.getelementbyid('namecheck').value; y = document.getelementbyid('password').value; if(x !="" && y !=""){ return true; } else if(x =="" && y == ""){ document.getelementbyid('errormsg').innerhtml='<font color="red">(required) name:</font>'; document.getelementbyid('errorpass').innerhtml='<font color="red">(required) password:</font>'; return false; } else if(x =="" && y!=""){ document.getelementbyid('errormsg').innerhtml='<font color="red">(required) name:</font>'; document.getelementbyid('errorpass').innerhtml='<font color="blue">password:</font>'; return false; } else if(y =="" && x!=""){ document.getelementbyid('errorpass').innerhtml='<font color="red">(required) password:</font>'; document.getelementbyid('errormsg').innerhtml='<font color="blue">name:</font>'; return false; } } </script> </head> <body> <?php include("header.php"); ?> <?php if (isset($_session['user'])) echo "<p class ='welcome' id='greeting'> hi, ". $_session['user'] . "! welcome site!</p>"; else echo "<p class ='welcome' id='greeting'> please login:</p> <form action='welcome.php' method='post'> <center><b id = 'errormsg'>name:</b> <input type='text' id='namecheck' name = 'username' /></center> <br /> <center><b id='errorpass'>password:</b> <input type='text' id ='password' name = 'password'/></center> <br /><br /> <center><input type='submit' value='log in' onclick='return validatingform()'/></center> </form>"; ?> <?php if (isset($_session['user'])) $user_rs = $db_con->query("select * users"); print_r($db_con->query("select * users")); print_r($user_rs); echo "<center><h1> user list:</h1> <table border='1'> <tr> <td><b>user</b></td> <td><b>login password</b></td> </tr> <tr>"; while($record = $user_rs->fetch_object()){ echo "<td>" . $record['id'] . "</td>"; echo "<td>" . $record['username'] . "</td>"; echo "</tr>"; } echo"</table></center>"; ?> <?php if (isset($_session['user'])) echo "<center><a href='logout.php'>logout</a></center>";?> <p class = "content"> page scrap work in progress. </p> <?php include("footer.php"); ?> </body> </html>
i have other files, believe these outside scope of problem. happy supply other files if requested.
a reminder: concern fact cannot use result should have been assigned $user_rs . mainly, appears nothing though appear have assigned query correctly. in advance help.
this typo-like error.
in essence, asks how assign variable expression result, , answer obvious can be:
$user_rs = $db_con->query("select * users");
and particular problem in question can bolied down to
"why getting
undefined variable: user_rs
error code below
$user_rs = $db_con->query("select * users"); print_r($user_rs);
and answer "that's impossible".
check file running , stuff that.
Comments
Post a Comment