How to convert mysql function into sqlsrv? [closed] - php

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 7 years ago.
Improve this question
$query = sqlsrv_query("select * from sessions where password='$password' AND username='$username'");
$rows = sqlsrv_num_rows($query);
if ($rows == 1) {
$_SESSION['login_user']=$username;
header("location: profile.php");
} else {
$error = "Username or Password is invalid";
}
Hi, I have this code but it's written in mysql and I want it to work in sqlserver so I changed mysql_query into sqlsrv_query and it didn't work properly, and I changed mysql_num_rows into sqlsrv_num_rows and it didn't work either so can anyone help me and tell me how to write them please?

You are missing the connection parameter to sqlsrv_query. While you're at it, you might as well use bind variables instead of string substitution to help guard against SQL-Injection attacks.
$serverName = "serverName\instancename";
$connectionInfo =
array( "Database"=>"dbName", "UID"=>"username", "PWD"=>"password" );
$conn = sqlsrv_connect( $serverName, $connectionInfo);
$params = array($username, $password);
$stmt = sqlsrv_query
($conn,
'select * from sessions where password=? AND username=?',
$params);

Related

PHP: Why is my PDO select not working? [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 5 years ago.
Improve this question
I'm learning PDO. Why is the below not working? Where is my mistake / error?
I just want the query to retrieve the highest number from seconds column
..................................................
if ($_SERVER['HTTP_X_FORWARD_FOR']) {
$ipaddress = $_SERVER['HTTP_X_FORWARD_FOR'];
} else {
$ipaddress = $_SERVER['REMOTE_ADDR'];
}
$user = "open";
$password = "r23sSF32";
$database_name = "open2";
$hostname = "localhost";
$dsn = 'mysql:dbname=' . $database_name . ';host=' . $hostname;
$conn = new PDO($dsn, $user, $password);
$sql = "SELECT MAX( seconds ) AS seconds FROM `opentill` WHERE ipaddress='$ipaddress'";
$conn->query($sql) as $row
$largests = $row['seconds'];
Try using prepared statements instead. Here's an example (untested):
$stmt = $conn->prepare("SELECT MAX( seconds ) AS seconds FROM `opentill` WHERE ipaddress = :ipaddress");
$stmt->bindParam(":ipaddress", $ipaddress); // Note: bindParam binds to the REFERENCE of the variable passed, only evaluated when execute() is called
$stmt->execute();
$result = $stmt->fetchColumn();
$result now contains that column value.

PHP (If not existant in records.) [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 6 years ago.
Improve this question
So basically I got this code right here:
<?php
include_once 'dbconfig2.php';
$con = new DB_con();
$table = "users";
if(isset($_GET['profile_id']))
{
$sql=mysql_query("SELECT * FROM users WHERE user_id=".$_GET['profile_id']);
$result=mysql_fetch_array($sql);
}
?>
I am clueless as to how I would make it so if the user_id is not existent in the records, they cannot view their profile but it leads them to another messsage or piece of code.
If the user_id doesn't exist, there won't be any rows in the result. When you try to read a row with mysql_fetch_array(), it returns FALSE. So you can simply test $result:
if (!$result) {
die("Invalid profile ID");
}
Try to use prepared statements using mysqli, in order to avoid sql injection.
By way of example:
$mysqli = new mysqli("localhost", "root", "root", "test");
if ($mysqli->connect_errno) {
echo "connect_error". $mysqli->connect_error;
}
$id = $_GET['profile_id'];
$result = $mysqli->prepare('SELECT name FROM users WHERE user_id = ?');
$result->bind_param("i", $id);
$result->execute();
$result->bind_result($col1);
$result->fetch();
$is_valid_profile = (!$col1) ? 'Invalid profile' : 'Valid profile';
echo $is_valid_profile;
$result->close();
http://php.net/manual/en/mysqli.prepare.php

print a column from Mysql database [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 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.
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.
Improve this question
I am trying to print out one column called Language1 from my Table that is called Mull, in a database called v6e.
At the moment i am getting a blank white screen.
<?php
session_start();
$servername = "localhost";
$user = "xxxx";
$password = "xxxx";
$dbname = "v6e";
// Create connection
$conn = mysqli_connect($servername, $user, $password, $dbname);
// Check connection
if (!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}
else
{
$query = "SELECT Language1 FROM Mull WHERE username = 'Mull'";
$result = mysqli_query($query);
$row = mysqli_fetch_arrary($result);
echo $row['Language1'];
}
mysqli_close($conn);
?>
You have a typo issue. Change the line:
$row = mysqli_fetch_arrary($result);
With:
$row = mysqli_fetch_array($result);
Plus, you're also not connecting to DB with your query
$result = mysqli_query($conn, $query);
Reference:
http://php.net/manual/en/mysqli.query.php
You should also check for errors:
http://php.net/manual/en/mysqli.error.php

PHP check if value exists in MySQL [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 tried to copy How to Check if value exists in a MySQL database and make my own, but for some reason it wont work...
This is what I got:
<?php
$host = '127.0.0.1';
$username = 'root';
$password = '';
$dbname = 'multiplayer';
$con=mysqli_connect($host, $username, $password, $dbname);
$check_player_ip=mysqli_query($con, 'SELECT `player_ip` FROM `playerdata` WHERE username = "remco" AND active = 0');
if (mysqli_num_rows($check_player_ip) == 0) {
//didnt find anything
} else{
//found something
}
?>
I get this error:
Parse error: syntax error, unexpected '$check_player_ip' (T_VARIABLE) in C:\xampp\htdocs\test.php on line 1
Solution
If you get the T_VARIABLE error, check the varriable before this rule. You may forgot the place the ';' xD
Thanks for all support!
You are mixing MySQL APIs, they do not mix together. mysql_num_rows
Use mysqli_num_rows()
Also make sure your DB connection is also mysqli_* and not mysql_*
EDIT after you've edited your question.
You need to pass DB connection to your query:
$check_player_ip=mysqli_query($con,'SELECT...`
$con being your DB connection. Change accordingly.
Plus WHERE username = remco - the word remco needs to be wrapped in quotes, it's a string and not an int.
WHERE username = 'remco'
Sidenote:
Your present code is open to SQL injection.
Use prepared statements, or PDO with prepared statements.
Edit 2:
Try inverting the quotes:
$check_player_ip=mysqli_query($con, "SELECT `player_ip` FROM `playerdata` WHERE username = 'remco' AND active = 0");
if (mysqli_num_rows($check_player_ip) == 0) {
//didnt find anything
}
Try this:
<?php
$host = '127.0.0.1';
$username = 'root';
$password = '';
$dbname = 'multiplayer';
$con = new mysqli( $host, $username, $password, $dbname );
/* Check Connection */
if ( $con->connect_error ) {
printf( "Connect failed: %s\n", $con->connect_error );
exit();
}
/* Query - Returns a resultset */
if ( $result = $con->query('SELECT `player_ip` FROM `playerdata` WHERE username = "remco" AND active = 0 ') ) {
if ( $result->num_rows <= 0 ) {
//didnt find anything
printf("No player");
} else {
//found something
printf("Select returned %d rows.\n", $result->num_rows);
}
/* free result set */
$result->close();
}
/* close connection */
$con->close();
?>
you should execute that mysqli query...
while($rows = mysql_arrayAssoc($ursql)){
$data[]=$rows;
}
if($something== $data['attribute']) //attribute(id,name...)
echo "ok some data is in"
else
echo "no matching data"
I hope it help you :)

what is the use of $rows=array() [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to know what to do with $rows=array(). why it is used and how i could use it further. would like to get some suggestions and answers
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbdb = "yumyum";
$connect = mysql_connect($dbhost, $dbuser, $dbpass) or die("connection error");
mysql_select_db($dbdb) or die("database selection error");
$id = $_POST['id'];
$query1=mysql_query("SELECT Quantity,id FROM `yumyum`.`food` where `food`.`id` LIKE $id");
$rows = array();
while($r = mysql_fetch_assoc($query1)) {
$output = $r['Quantity'];
echo $output;
$query2=mysql_query("UPDATE food SET Quantity = Quantity - 1 where `food`.`id` LIKE ".$r["id"]);
}
?>
In your script it does nothing but initialize the variable $row with an array type. It would probably be intended to be used in your while loop ($r):
...
$query1=mysql_query("SELECT Quantity,id FROM `yumyum`.`food` where `food`.`id` LIKE $id");
$r = array();
while($r = mysql_fetch_assoc($query1)) {
...
See PHP basics:
It is not necessary to initialize variables in PHP however it is a very good practice.
You should also stop using mysql_ functions. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which.

Categories