Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
here's a part of my code that AFTER you submit your login credentials checks your username, password, etc...:
mysql_select_db("robur_mike") or die ("Could not find DB!");
$query = mysql_query("SELECT * FROM Bx1_Users WHERE Username='$username'");
$numrows = mysql_num_rows($query);
if ($numrows!=0)
{
while ($row =mysql_fetch_assoc($query))
{
$dbusername = $row['Username'];
$dbpassword = $row['Password'];
$dbfirstname = $row['FirstName'];
$dblastname = $row['LastName'];
}
.....
I now need to "translate" that to run under a DB2 database in BlueMix. I am already connected to the database using the code provided here:
How to connect to a SQL Database-s2 from a .php application in BlueMix
The query should be OK since it is basic SQL. What you should change is the way you run it, since in your old code you are using the mysql library.
Looking at the other question, I assume that you are able to connect doing something like:
$conn = db2_connect($conn_string, '', '');
Now to execute the query you can use db2_exec 'translating' your code to something like:
$sql = "SELECT * FROM <schemaName>.Bx1_Users WHERE Username='$username'";
if ($conn) {
$stmt = db2_exec($conn, $sql, array('cursor' => DB2_SCROLLABLE));
while ($row = db2_fetch_assoc($stmt)) {
$dbusername = $row['Username'];
$dbpassword = $row['Password'];
$dbfirstname = $row['FirstName'];
$dblastname = $row['LastName'];
}
}
db2_close($conn);
As you can see I've added a placeholder for the schema name in the SQL query. You can retrieve it within your SQL Database dashboard (Manage/Work with tables).
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
My SQL query is not working. Below is my code and note that in that table query worked fine and gives output. But in PHP by using mysqli_num_rows(), mysqli_fetch_assoc() and mysqli_fetch_array() all doesn't works for me.
My DB connection is :
$conn = mysqli_connect($servername, $username, $password, $db);
Note : My DB Connectivity is fine.
$username = mysqli_real_escape_string($conn, $_POST['username']);
$password = mysqli_real_escape_string($conn, $_POST['password']);
$query = mysqli_query($conn, "SELECT * FROM `admin` WHERE `username`='$username'");
This query results true in PHPMyAdmin and returns false in PHP with the above functions. Can anyone answer is I made a mistake?
And I am tried that query to execute in following methods :
$row = mysqli_fetch_assoc(); // Results No Data
$data = mysqli_fetch_array($query); // Results No Data
$num = mysqli_num_rows($query); // Results 0 Data
you may execute the query before use it :
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = mysqli_connect("localhost", "my_user", "my_password", "world");
$query = "SELECT Name, CountryCode FROM City ORDER BY ID DESC";
$result = mysqli_query($conn , $query);
/* fetch associative array */
while ($row = mysqli_fetch_assoc($result)) {
printf("%s (%s)\n", $row["Name"], $row["CountryCode"]);
}
You mast to run mysqli_query function for receive result from DB:
$username = mysqli_real_escape_string($conn, $_POST['username']);
$query = "SELECT * FROM `admin` WHERE `username`='$username'";
$result = mysqli_query($mysqli, $query);
$row = mysqli_fetch_assoc($result);
print_r($row);
PHP MySQL sandbox here
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a string called $searchquery. $searchquery contains this SQL command:
SELECT * FROM `videos` WHERE (`Title` LIKE "%query%" OR `Tags` LIKE "%query%")
How can I make MySQL execute this command from a PHP page then display the results on PHP page??
I have tried
$sql = ($searchquery)
But dosent seem to work?
Firstly you need to create database connection
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// then execute your query
$sql = "SELECT * FROM `videos` WHERE (`Title` LIKE '%query%' OR `Tags` LIKE '%query%')";
$result = mysqli_query($conn, $sql);
// and fetch result set
while($row = mysqli_fetch_assoc($result)) {
print_r($row);
}
?>
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a table called "users" which has columns 'username', 'password' and 'permission'. In the permission column is either 'browse' or 'edit'.
Say I have a user logged into my site, I want to select select their permission using their username (which I have stored in a session variable). I want to then set a variable equal to either 'browse' or 'edit' based on their permission, to then use in further logic.
Assuming I have connected to and selected the appropriate database I am pretty sure the php code and query should go something like:
$u = $_SESSION['username'] ;
$sql = "SELECT permission FROM users WHERE username = '$u' " ;
$result = mysqli_query($sql);
But Im unsure how to then set a variable equal to 'browse' or 'edit' accordingly.
Any ideas?
Say you have a connection $con, for using session you have to start your session.
$u = $_SESSION['username'] ;
$sql = "SELECT `permission` FROM `users` WHERE username='$u'";
$result = mysqli_query($con, $sql);
$rows = mysqli_fetch_object($result);
//now its time to set the permission to the variable
echo $permission = $rows->permission;
mysqli_close($con);
you can also set the $permission to a $_SESSION.
$_SESSION['permission'] = $permission;
You have to do it like this:
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$u = $_SESSION['username'] ;
$sql = "SELECT `permission` FROM `users` WHERE username='$u'";
$result = mysqli_query($conn, $sql);
$rows = mysqli_fetch_object($result);
//now its time to set the permission to the variable
echo $permission = $rows->permission;
mysqli_close($conn);
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
someone please help me to set mysql database connection variable on below php scripts but I couldn't fixed it.
$sql = "SELECT DISTINCT r.itemID, r2.ratingValue - r.ratingValue
as rating_difference
FROM rating r, rating r2
WHERE r.userID=$userID AND
r2.itemID=$itemID AND
r2.userID=$userID;";
$db_result = mysql_query($sql, $connection);
$num_rows = mysql_num_rows($db_result);
while ($row = mysql_fetch_assoc($db_result)) {
$other_itemID = $row["itemID"];
$rating_difference = $row["rating_difference"];
if (mysql_num_rows(mysql_query("SELECT itemID1
FROM dev WHERE itemID1=$itemID AND itemID2=$other_itemID",
$connection)) > 0) {
$sql = "UPDATE dev SET count=count+1,
sum=sum+$rating_difference WHERE itemID1=$itemID
AND itemID2=$other_itemID";
mysql_query($sql, $connection);
if ($itemID != $other_itemID) {
$sql = "UPDATE dev SET count=count+1,
sum=sum-$rating_difference
WHERE (itemID1=$other_itemID AND itemID2=$itemID)";
mysql_query($sql, $connection);
}
}
You didn't connect to any database use some thing like this
$link = mysql_connect('dbhost', 'dbusername', 'dbpassword');
$connection = mysql_select_db('tablename', $link);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm sure it's a kickself-obvious typo, but I can't see it. I'm trying to INSERT data taken from a HTML form using POST into a MySQL database using PHP. The POST works successfully, but the query fails; I've checked the table to make sure nothing new has been inserted.
Here's the PHP code intended to run the query:
if ($_POST) {
$username = "root";
$password = "root"; //ssh don't tell
$hostname = "localhost";
$dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
$dbname = "asoiaf";
$tablename = "charlist";
$id = '3';
$bookIntroduced = $_POST['bookIntroduced'];
$pageIntroduced = $_POST['pageIntroduced'];
$forename = $_POST['forename'];
$surname = $_POST['surname'];
$oldSurname = $_POST['oldSurname'];
$alias = $_POST['alias'];
$title = $_POST['title'];
$pageIntroduced = $_POST['regnalNumber'];
// Below is the query that fails to execute.
$query = "INSERT INTO $tablename (
$id, $bookIntroduced, $pageIntroduced, $title, $forename, $surname, $oldSurname, $alias, $regnalNumber
)";
mysql_query($query) or die("Nah, I don't feel like being helpful.");
mysql_close($dbhandle);
}
And here is the structure of the table given by the DESCRIBE command:
Can anyone help me to identify the problem?
Also, if it wasn't clear, I'm new to PHP and SQL.
Doing a SQL query like this is bad practice in many ways, not least because it's extremely fragile and insecure, but I think it will work if you add VALUES and quote the strings.
$query = "INSERT INTO $tablename VALUES (
'$id', '$bookIntroduced', '$pageIntroduced', '$title', '$forename', '$surname', '$oldSurname', '$alias', '$regnalNumber'
)";
I advise against doing this though, and I'm giving this answer just because it's the shortest path to working code. Always name your table and columns (INSERT INTO mytable (col1, col2) VALUES (:val1, :val2)), and use prepared statements with mysqli.