convert uppercase letter to lowercase using php [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
<?php
$filename = "your htpasswd file path goes here"; // your htpasswd file name - complete unix path - or relative to this script
$host=“host”;
$dbuser=“usert”;
$dbpswd=“pass";
$mysqldb="db_name";
$table="passwd_table";
mysql_connect("$host", "$dbuser", "$dbpswd");
mysql_select_db ("$mysqldb");
$query = mysql_query("SELECT name FROM $table");
while ($row = mysql_fetch_array($query)) {
$user = $row['name'];
$pass = $row['name'];
$encrypted = crypt($pass);
$record .= "$user:$encrypted\r\n";
}
file_put_contents($filename,$record);
?>
I am using this php to write a file for htpassword , some of my username/password has uppercase letters , i want to convert it to lowercase then encrypt password and write user/pass to file

try this
$lowercase=strtolower('Your input')
solution
mysql_connect("$host", "$dbuser", "$dbpswd");
mysql_select_db ("$mysqldb");
$query = mysql_query("SELECT name,pass FROM $table");
while ($row = mysql_fetch_array($query)) {
$user = strtolower($row['name']);
$pass = strtolower($row['pass']);
$encrypted = crypt($pass);
$record .= "$user:$encrypted\r\n";
}

Use strtolower function
Returns string with all alphabetic characters converted to lowercase.

<?php
echo strtolower("Hello WORLD.");
?>
OUTPUT : hello world.
Try this

I would add that better if to use multibyte functions if you are not 100% sure there will be only standard characters.
For example:
mb_internal_encoding("UTF-8");
...
$user = mb_strtolower($row['name']);
$pass = mb_strtolower($row['pass']);

Related

mySQL dropdown using PHP does not display options from database [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Improve this question
I looked at some other questions online and on here related to this, but none seem to really encounter my error exactly.
I wrote my PHP code and implemented it into my HTML, I get the dropdown box appearing, but it doesn't actually want to display any values. Is there any implementations or fixes I should include in my code? How do I get it to work?
My database is called: Treatments
My column in the database that I want displayed is called: Treatment
treatment_dropdown.php
<?php
$hostname = 'host_name';
$dbname = 'database_name';
$username = 'username';
$password = 'password';
$con=mysql_connect($hostname,$username,$password,$dbname) or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db($dbname,$con) or die("Failed to connect to MySQL: " . mysql_error());
$query = "SELECT * FROM `Treatments`";
$result = mysql_query($con, $query);
$options = "";
while ($row = mysql_fetch_array($result)){
$options = $options . "<option>$row[1]</option>";
}
?>
HTML:
<body>
<select>
<?php
echo $options;
?>
</select>
</body>
Consider changing this line:
$query = "SELECT * FROM 'Treatments'";
to use backticks instead of single quotes like so:
$query = "SELECT * FROM `Treatments`";
In my test query I got an error because of this, let me know if that helps.
Add <?php include 'treatment_dropdown.php'; ?> to the top of your HTML file. This should give you access to the the $options string so it can be used in that file. Note that in order for this to work, treatment_dropdown.php needs to be in the same directory as your HTML file. If it is not, the include statement will need to be changed to reflect the appropriate file path.
Do not use mysql_*() functions, they are deprecated. Use mysqli or PDO instead.
No matter which library use use to access mysql, always check for errors within the sql code separately. Errors in the sql code do not result in errors in the php code.
In this particular case the problem is that you included the table in single quotes instead of backticks.
The correct code:
$query = "SELECT * FROM `Treatments`";
Here's what your PHP file should look like:
<?php
$hostname = 'localhost';
$dbname = 'Treatments';
$username = 'root';
$password = '';
$con = mysql_connect( $hostname, $username, $password, $dbname) or die("Failed to connect to MySQL: " . mysql_error());
$db = mysql_select_db($dbname,$con) or die("Failed to connect to MySQL: " . mysql_error());
/* No single quotes needed for the table name. */
$query = "SELECT * FROM Treatments";
/* First parameter should be $query not $con */
$result = mysql_query($query, $con);
$options = "";
/* Check if no results exist. */
if ( !$result ) {
die( "NO results found." );
}
while ( $row = mysql_fetch_array($result) ) {
$options .= "<option>$row[treatment]</option>";
}
?>
Notes:
dont use mysql_* functions, they're not secure, use PDO instead.
your table name does not need to be wrapped in single quotes.
mysql_query expects parameter 1 to be the query not the DB connection.
you should probably check if no results are found.

telephone directory with mysql [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have been programming a telephone directory and i am now facing a problem.
When I enter ime and prezime (which are name and surname in english), I can not get broj (which is telephone number in english) of that person.
<?php
mysql_connect ("localhost", "root", "");
mysql_select_db ("imenik");
mysql_set_charset('utf8');
if (isset ($_POST['ime']) && isset ($_POST['prezime'])){
if (!empty ($_POST['ime']) && !empty ($_POST['prezime'])){
$trazeno_ime = $_POST('ime');
$trazeno_prezime = $_POST('prezime');
$query = "SELECT broj_telefona, adresa FROM korisnici WHERE ime='$trazeno_ime' AND prezime='$trazeno_prezime'";
if ($query_run = mysql_query($query)){
if(mysql_num_rows($query_run)==NULL){
echo "Nema korisnika u bazi";
}
else {
$query_row = mysql_fetch_assoc($query_run);
$brojtel = $query_row["broj_telefona"];
$adresa = $query_row["adresa"];
echo "Broj: ".$brojtel."<br>";
echo "Adresa: ".$adresa;
}
}
else {
echo "Unesi podatke";
}
}
}
?>
Change these lines:
$trazeno_ime = $_POST('ime');
$trazeno_prezime = $_POST('prezime');
To:
$trazeno_ime = $_POST['ime'];
$trazeno_prezime = $_POST['prezime'];
and you should take a look at PDO or Prepared Statements, since msyql_ functions are depricated.

Why isn't PHP code echoing [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have been reading many different pieces of code an I am confused as to why the $echo row[0] at the bottom of my code here does not return anything.
$dbhost = 'localhost';
$uname = $_POST["uname"];
//***create connection object
$connection = mysql_connect($dbhost, "bc572fsdf", "abcdfsds") or die(mysql_error());
$dbname = "bc57db";
mysql_select_db($dbname) or die(mysql_error());
//***select a random security question
//*** need this to import session variables
session_start();
echo ($_SESSION["ValidUser"] . "\n");
$rq = array('q1', 'q2', 'q3');
$rand_key = array_rand($rq, 1);
echo $rq[$rand_key];
$question = $rq[$rand_key];
$qtoanswer = mysql_query("select '$question' from users where uname = '$uname'");
if (!$qtoanswer) {
echo "Could not run query:" . mysql_error();
exit;
}
echo $qtoanswer;
$row = mysql_fetch_row($qtoanswer);
echo $row[0];
?>
The fault is in this line:
$qtoanswer = mysql_query("select '$question' from users where uname = '$uname'");
You should be using grave marks for the column name, such as:
$qtoanswer = mysql_query("select `$question` from users where uname = '$uname'");
^ ^
Also, you should be using MySQLi/PDO, so you can prepare this, or at the very least, escape $uname.
Because you cannot echo a array....try var_dump or print_r, or use a loop:
foreach($row[] as $result) {
echo $row[], '<br>';
}

use session username as "WHERE" in mysqli query [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I know I am doing something wrong but I really would like to know what it is. I can echo the
username of the session loggedin user using <?php echo $_SESSION['username']; ?>but I don't know why it doesn't work when I try to query database using the same technique. my codes below
I include this in the page
<?php
session_start();
$username=$_SESSION['username'];
?>
and here is the code that was suppose to display firstname and user_id of the sessions logged in user
<?php
$conn = new mysqli('localhost', 'root', 'browser', 'test');
if (mysqli_connect_errno()) {
exit('Connect failed: '. mysqli_connect_error());
}
$username = '$username';
$sql = "SELECT `user_id`, `firstname` FROM `members` WHERE `username`='$username'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo '<br /> user_id: '. $row['user_id']. ' - firstname: '. $row['firstname'];
}
}
else {
echo '0 results';
}
$conn->close();
?>
$username = '$username';
PHP variables inside single-quotes are not expanded. So now your variable is the literal string '$username', which undoubtedly won't match any user in your database.
You probably need to set $username = $_SESSION['username']; in your second PHP script.

How do I fix this comment SQL/PHP code? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Improve this question
I'm working on a simple comment section for a website I'm working on, but there seems to be something awry with my code. Here's the form:
<?php
if($_SESSION['authenticated']) { //// if not logged show registration form
echo "<form name='comment' action='addcomment.php' method='get'>";
echo "<textarea name='CommentText'></textarea>";
echo "<input type='submit' value='Submit' />";
echo "</form><br><br>";}
else {
echo "Please log in to post.";
}
$res=mysqli_query($db, "SELECT * FROM comments");
$row = mysqli_fetch_array($res);
while ($row) {
echo '<p>from</p>';
$row = mysqli_fetch_array($res);
}
?>
And here's the PHP/SQL to add the form data to my database table (the table's named comments)
<?php $dbHost = 'cust-mysql-123-17';
$dbUser = 'user';
$dbPass = 'mypass';
$dbName = 'ollie';
$db = mysqli_connect( $dbHost, $dbUser, $dbPass, $dbName ) or die("Cannot connect");
$CommentText = $_GET['CommentText'];
$email = $_SESSION['authenticated'];
$res=mysqli_query($db, "INSERT INTO 'comments' (email, CommentText) VALUES('$email','$CommentText')");
if ($res){echo"<script lang='javascript' type='text/javascript'>
alert('Post successful');
window.location = 'forum.php';
</script>";}
else{
echo "alert('Post Failed');";
}
?>
Whenever I try to run it it shows the "post failed" alert I set up in the last if statement. Help?
remove quotes from the tablename - if you want to enclose it, use backticks ` (key below escape)
$res = mysqli_query($db, "INSERT INTO comments (email, CommentText) VALUES('$email','$CommentText')");
about security, i recommend you to learn about mysqli::real_espace_string on php.net

Categories