Display values from db without require login in php -
i need display content created user db when user not logged in.
<? php $get_user_content = mysqli_query($conn, "select * content username = '$username'") or die($dataaccess_error); if(mysqli_num_rows($get_user_content) == 1 ) { $row = mysqli_fetch_array($get_user_content); $title = $row['utitle']; $content = $row['ucontent']; } echo $title; echo $content; ?> but getting 2 errors:
undefined variable: username and
warning: mysqli_query() expects parameter 1 mysqli i new php coding. please me on this.
you need define username variable, need connect database too. if going use mysqli, better use object oriented style. mysqli may not ideal solution, see cletus answer here: mysql vs mysqli when using php
you'll need check php guide @ php.net learn language, , make sure check comments. can google tutorials. if fail, stackoverflow place help.
<?php $username = trim($_post['username']); //where username form field sent using post method, trim remove white spaces if user enters spaces, script see empty string. //connect database http://www.php.net/manual/en/mysqli.query.php $conn = mysqli_connect("localhost", "my_user", "my_password", "yourdatabasename"); /* check connection */ if (mysqli_connect_errno()) { printf("connect failed: %s\n", mysqli_connect_error()); exit(); } if ($username){ $username=mysqli_real_escape_string($conn, $username); //escape string after connect http://www.php.net/manual/en/mysqli.real-escape-string.php $get_user_content = mysqli_query($conn, "select * content username = '$username' limit 1") or die(mysqli_error($conn)); //assuming have table called content field called username store username, add limit 1 since need 1 anyway. , databaseaccess_error comes from? it's undefined. use mysqli_error if(mysqli_num_rows($get_user_content) == 1 ) { $row = mysqli_fetch_array($get_user_content); $title = $row['utitle']; $content = $row['ucontent']; echo $title; echo $content; }else echo "user couldn't found"; }
Comments
Post a Comment